Mar*_*ark 5 objective-c gamekit ios game-center ios9
我正在尝试邀请附近的玩家参加比赛,但邀请函要么从未发送过,要么从未收到过.
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;
}
Run Code Online (Sandbox Code Playgroud)
我将我的视图控制器注册为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
}
}];
}
}
Run Code Online (Sandbox Code Playgroud)
我有GKLocalPlayerListener设置的委托方法,但从不调用它们:
- (void)player:(GKPlayer *)player didRequestMatchWithRecipients:(NSArray<GKPlayer *> *)recipientPlayers
{
NSLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)player:(GKPlayer *)player didAcceptInvite:(GKInvite *)invite
{
NSLog(@"%s", __PRETTY_FUNCTION__);
}
Run Code Online (Sandbox Code Playgroud)
有没有人知道如何在没有 GKMatchmakerViewController和iOS9的情况下使用它?我能找到的唯一示例包含已弃用的-inviteHandler方法.
如果您知道如何将其转换为 Objective-C 并尝试一下,则此代码可以在 Swift 中运行。
GKMatchmaker.sharedMatchmaker().findMatchForRequest(
request,
withCompletionHandler: {(match : GKMatch!, error: NSError!) -> Void in
NSLog("This works")
})
Run Code Online (Sandbox Code Playgroud)