2013-04-28 112 views
4

由于iOS 6中GameKit API的更新,我终于能够按照它应该的方式实现我的回合制棋盘游戏,完成转换超时和更好的程序化匹配创建。但是,我遇到了一个我似乎无法解决的问题。我的愿望是让Game Center对最终用户完全不可见,这样一切都是程序化的,并使用我自己的自定义界面。因此,我使用自己的自定义表格视图来显示匹配项,而不是默认的GKTurnBasedMatchmakerViewController。现在,我使用-loadMatchesWithCompletionHandler:方法显示打开的匹配没有问题。我还使用自定义屏幕来创建一个匹配,直接创建自动匹配(而不是问题)和一个表格视图,用于加载localPlayer的Game Center朋友以获得邀请。由于playersToInvite属性现在可以用playerID填充,所以在iOS 6中可以这样做。GKMatchRequest邀请没有在其他设备上显示

我的主要问题是在收件人一方处理邀请。我使用以下代码发送邀请

-(void)invitarAmigoMio:(NSArray*)losAmigos 
{ 

    GKMatchRequest *request = [[GKMatchRequest alloc] init]; 
    request.minPlayers = 2; 
    request.maxPlayers = 2; 
    request.defaultNumberOfPlayers=2; 
    request.playersToInvite = losAmigos; 
    request.inviteMessage = @"Your Custom Invitation Message Here"; 



    request.inviteeResponseHandler = ^(NSString *playerID, GKInviteeResponse 
             response) 
    { 

     if (response==GKInviteeResponseAccepted) 
     { 
      NSLog(@"INVITACION ACEPTADA"); 
     } 
     else 
     { 
      NSLog(@"INVITACION RECHAZADA"); 

     } 
    }; 

} 

我有一个处理通知的委托。下面的代码是当用户通过认证

-(void)registroNotificaciones 
{ 
    NSLog(@"registroNotificaciones"); 

    [GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite* acceptedInvite, NSArray *playersToInvite) 
    { 
     if(acceptedInvite != nil) 
     { 
      // Get a match for the invite we obtained... 
      [[GKMatchmaker sharedMatchmaker] matchForInvite:acceptedInvite completionHandler:^(GKMatch *match, NSError *error) 
      { 
       if(match != nil) 
       { 
        NSLog(@"match != nil: "); 

       } 
       else if(error != nil) 
       { 
        NSLog(@"ERROR: From matchForInvite: %@", [error description]); 
       } 
       else 
       { 
        NSLog(@"ERROR: Unexpected return from matchForInvite..."); 
       } 
      }]; 
     } 
    }; 
    NSLog(@"FIN registroNotificaciones"); 

} 

为什么通知不发呢,还是没有收到通知的推出?是否有另一种方式来发送通知,邀请玩游戏? 我检查了我的沙箱帐户的游戏中心允许邀请,我不知道发生了什么

+0

为什么你使用'.inviteHandler',如果它不赞成iOS 7?如何通过另一种方式获得'GKInvite * acceptedInvite'? – 2014-01-14 23:09:45

回答

0

我知道你正在寻找使用自定义显示,但我没有为此做我自己的视图控制器,而是我使用GKMatchmakerViewController来处理我发送的'发送'邀请,我认为这就是你的代码中缺少的东西。

这里是我的代码,也许你可以尝试,并检查GKMatchmakerViewController的内部工作原理(如果可能):

GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease]; 
request.minPlayers = minPlayers;  
request.maxPlayers = maxPlayers; 
request.playersToInvite = pendingPlayersToInvite; 
GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithMatchRequest:request] autorelease]; 
mmvc.matchmakerDelegate = self; 
[myPresentingViewController presentModalViewController:mmvc animated:YES]; 

我有几乎相同的代码,你有一个处理的通知(registroNotificaciones)和只要用户使用GameCenter进行身份验证,它就会立即执行,并且是应用程序在启动时首先执行的操作之一,就像您说的那样,它看起来像这样的代码正常工作。希望能帮助到你!

+0

游戏中心的邀请今天不能工作,我不知道游戏中心SandBox服务器正在发生什么。我读了关于在iOS开发者论坛 – Aitul 2013-05-02 07:53:19

+0

看看这个主题http://stackoverflow.com/a/4641232/941490 – 2013-05-03 18:19:10

3

您几乎完成了创建邀请所需的一切;你只需要发送它与这个代码:

[[GKMatchmaker sharedMatchmaker] findMatchForRequest:request withCompletionHandler:^(GKMatch* match, NSError *error) { 
     if (error) 
     { 
      //Invite has not been sent 
     } 
     else if (match != nil) 
     { 
      //whatever you want to do when the receiver accepts the invite 
     } 
}]; 
相关问题