我试图邀请附近的玩家参加比赛,但邀请函要么从未发送过,要么从未收到过。
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方法。
答案 0 :(得分:0)
如果您知道如何将其转换为Objective-C并尝试使用此代码,则可以使用Swift。
GKMatchmaker.sharedMatchmaker().findMatchForRequest(
request,
withCompletionHandler: {(match : GKMatch!, error: NSError!) -> Void in
NSLog("This works")
})
答案 1 :(得分:0)
根据SO上的多个问题,Game Center似乎时不时会陷入困境。在最好的情况下,它返回&#34;游戏未被识别&#34;错误。在最糟糕的情况下,它只是愉快地将nil返回到GC调用。有时它会恢复自己的工作,有时它不会。但是,您似乎可以通过登录iTunesConnect再次启动它并执行以下任何操作:
我已将此添加到我的调试例程中。如果GC的某些方面停止工作,或返回nil,我会尝试在继续之前在iTunesConnect中进行上述更改之一。在我的情况下,我得到了#34;游戏无法识别&#34;每周几次,但其他几个人已经注意到了#n; nil返回值。&#34;
答案 2 :(得分:0)
我知道这是一篇较旧的文章,但是在尝试通过Internet在多个应用程序实例之间建立连接时遇到了它。我相信您缺少的部分是注册了侦听器后,您需要使用来获取连接状态
- (void)match:(GKMatch *)match
player:(GKPlayer *)player
didChangeConnectionState:(GKPlayerConnectionState)state
{
NSLog(@">>> did change state");
if (state == GKPlayerStateConnected)
{
NSLog(@">>>> match:%@ did change to Connected for player %@ ",match, player.displayName);
}
else if (state == GKPlayerStateDisconnected)
{
NSLog(@">>>> match:%@ disconnected for player %@ ",match, player.displayName);
}
当我从findMatchForRequest:调用complementHandler时,我发现比赛有0名球员,但是我可以成功地使用didChangeConnectionState中返回的GKMatch和GKPlayer: 希望这对在OP之后阅读了这篇文章的人有所帮助。