GKMatchRequest邀请未在其他设备中显示

时间:2013-04-28 18:14:43

标签: ios6 game-center gamekit invitation

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

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

-(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");

}

为什么不发送通知?或者没有收到通知?是否有其他方式发送邀请来玩游戏? 我检查过,我的游戏中心的沙箱帐户允许邀请,我不知道发生了什么

2 个答案:

答案 0 :(得分: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
        }
}];

答案 1 :(得分: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进行身份验证就会执行该代码,这也是应用程序在启动时所做的第一件事,就像你说的那样看起来这个代码工作正常,只是。希望它有所帮助!