我正在尝试邀请附近的球员参加比赛,但是邀请从未发送过或从未收到过。

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;
    }

我将 View Controller 注册为GKLocalPlayerListener的委托(delegate):
    [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设置的委托(delegate)方法,但从未调用过它们:
- (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方法。

最佳答案

如果您知道如何将其转换为Objective-C并进行尝试,则此代码可在Swift中运行。

GKMatchmaker.sharedMatchmaker().findMatchForRequest(
    request,
    withCompletionHandler: {(match : GKMatch!, error: NSError!) -> Void in
        NSLog("This works")
})

关于ios - 从未收到GKMatchmaker findMatchForRequest邀请,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36728503/

10-14 23:24