2013-04-28 36 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 
     } 
}]; 
相關問題