【发布时间】:2016-04-19 20:13:04
【问题描述】:
我正在尝试邀请附近的玩家参加比赛,但邀请要么从未发送,要么从未收到。
GKMatchMaker startBrowsingForNearbyPlayersWithHandler 工作并返回在同一 wifi 上的附近玩家,但随后我使用 findMatchForRequest 并返回没有任何玩家的比赛,并且我尝试邀请的玩家永远不会收到邀请通知。这是我的代码。
我首先验证本地播放器:
GKLocalPlayer.localPlayer.authenticateHandler= ^(UIViewController *controller, NSError *error)
{
if (error)
{
NSLog(@"%s:: Error authenticating: %@", __PRETTY_FUNCTION__, error.localizedDescription);
return;
}
if(controller)
{
// User has not yet authenticated
[pViewController presentViewController:controller animated:YES completion:^(void)
{
[self lookForNearbyPlayers];
}];
return;
}
[self lookForNearbyPlayers];
};
-(void)lookForNearbyPlayers
{
if(!GKLocalPlayer.localPlayer.authenticated)
{
NSLog(@"%s:: User not authenticated", __PRETTY_FUNCTION__);
return;
}
我将我的视图控制器注册为 GKLocalPlayerListener 的代理:
[GKLocalPlayer.localPlayer registerListener:self]; // self is a view controller.
// This works. My test local player which is a second device and appleID I setup shows up when this handler is called.
[GKMatchmaker.sharedMatchmaker startBrowsingForNearbyPlayersWithHandler:^(GKPlayer *player, BOOL reachable)
{
NSArray * paPlayers= [NSArray arrayWithObject:player];
_pMatchRequest= [[GKMatchRequest alloc] init];
_pMatchRequest.minPlayers= 2;
_pMatchRequest.maxPlayers= 4;
_pMatchRequest.recipients = paPlayers;
_pMatchRequest.inviteMessage = @"Join our match!";
_pMatchRequest.recipientResponseHandler = ^(GKPlayer *player, GKInviteeResponse response)
{
// This is never called.
NSLog((response == GKInviteeResponseAccepted) ? @"Player %@ Accepted" : @"Player %@ Declined", player.alias);
};
// This returns with a match without any players.
[GKMatchmaker.sharedMatchmaker findMatchForRequest:_pMatchRequest withCompletionHandler:^(GKMatch *match, NSError *error)
{
if(error)
{
NSLog(@"%s:: %@", __PRETTY_FUNCTION__, error.localizedDescription);
return;
}
else if(match != nil)
{
_pMatch= match;
match.delegate = self;
NSLog(@"players count= %lu", (unsigned long)_pMatch.players.count); // Always returns 0
}
}];
}
}
我有 GKLocalPlayerListener 设置的委托方法,但它们从未被调用:
- (void)player:(GKPlayer *)player didRequestMatchWithRecipients:(NSArray<GKPlayer *> *)recipientPlayers
{
NSLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)player:(GKPlayer *)player didAcceptInvite:(GKInvite *)invite
{
NSLog(@"%s", __PRETTY_FUNCTION__);
}
有谁知道如何在没有 GKMatchmakerViewController 和 iOS9 的情况下使其工作?我能找到的唯一示例有已弃用的 -inviteHandler 方法。
【问题讨论】:
-
您已验证您的开发环境已设置所有权利?并且其他设备使用相同的证书等编译?
-
@Larusso,是的,我有,我只是再次确认。有什么想法吗?感谢您的意见!
-
不,对不起。我检查了我上次玩游戏中心时使用的一些代码。它很快并使用了旧的api。由于证书而不是编码本身,我很难让它工作。
-
您可能只是想在一台设备中找到匹配项。基本上这个函数“findMatchRequest”应该在两个不同的设备/模拟器中同时调用,然后你会收到一个成功的响应。
标签: ios objective-c ios9 game-center gamekit