iphone - 未显示游戏中心邀请

标签 iphone ios game-center multiplayer

我一直在开发一款允许多人比赛的游戏。我之前测试过多人邀请,它们都有效。从一台设备发送请求会在另一台设备上显示一个横幅,如果邀请被接受,游戏就会开始。

就在两天前的晚上,在提交应用程序之前,我再次测试了这个功能,结果发现它已经停止工作了。

- (void)authenticateLocalUser:(UIViewController *)viewController :(id<GCHelperDelegate>)theDelegate
{ 
    delegate = theDelegate;
    self.presentingViewController = viewController;

    if (!gameCenterAvailable) {
        // Game Center is not available. 
        userAuthenticated = FALSE;
    } 
    else{
        GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];

        /*
         The authenticateWithCompletionHandler method is like all completion handler methods and runs a block
         of code after completing its task. The difference with this method is that it does not release the 
         completion handler after calling it. Whenever your application returns to the foreground after 
         running in the background, Game Kit re-authenticates the user and calls the retained completion 
         handler. This means the authenticateWithCompletionHandler: method only needs to be called once each 
         time your application is launched. This is the reason the sample authenticates in the application 
         delegate's application:didFinishLaunchingWithOptions: method instead of in the view controller's 
         viewDidLoad method.

         Remember this call returns immediately, before the user is authenticated. This is because it uses 
         Grand Central Dispatch to call the block asynchronously once authentication completes.
         */

        //ios 6
        [localPlayer setAuthenticateHandler:(^(UIViewController* viewcontroller, NSError *error) {
            if (viewcontroller != nil){
                userAuthenticated = FALSE;
                [self.presentingViewController presentViewController: viewcontroller animated: YES completion:nil];
            }
            else if (localPlayer.isAuthenticated){
                // Enable Game Center Functionality
                userAuthenticated = TRUE;


                [self checkForInvite:self.presentingViewController :delegate];

                if (! self.currentPlayerID || ! [self.currentPlayerID isEqualToString:localPlayer.playerID]) {

                    // Current playerID has changed. Create/Load a game state around the new user.
                    self.currentPlayerID = localPlayer.playerID;

                    // get friends of local player
                    [localPlayer loadFriendsWithCompletionHandler:^(NSArray *friends, NSError *error) {
                        if (friends != nil)
                        {
                            [self loadPlayerData: friends];
                        }
                    }];
                }
            }
            else{
                userAuthenticated = FALSE;
            }
            [scoreHandler setGameCentreAvailable:userAuthenticated];
        })];
    }
}


- (void)checkForInvite :(UIViewController *)viewController :(id<GCHelperDelegate>)theDelegate
{
    delegate = theDelegate;
    self.presentingViewController = viewController;
    NSLog(@"Invite handler installed");

    [GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite) {

        // Insert application-specific code here to clean up any games in progress. 
        if (acceptedInvite){
            NSLog(@"Accepted");
            GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithInvite:acceptedInvite] autorelease];
            mmvc.matchmakerDelegate = self;
            [viewController presentViewController: mmvc animated: YES completion:nil];

        } else if (playersToInvite) {
            NSLog(@"Match Request");
            GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];
            request.minPlayers = 2; 
            request.maxPlayers = 2; 
            request.playersToInvite = playersToInvite;
            GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithMatchRequest:request] autorelease];
            mmvc.matchmakerDelegate = self;
            [viewController presentViewController: mmvc animated: YES completion:nil];
        }
    };
}

xcode 中的调试窗口显示如下:

2013-03-27 18:06:20.112 MyApp[791:907] Authentication changed: player authenticated.
2013-03-27 18:06:21.219 MyApp[791:907] Invite handler installed
Mar 27 18:06:21 Neils-iPhone MyApp[791] <Notice>: 18:06:21.356712 com.apple.GameKitServices: -[GKDiscoveryManager startAdvertisingLocalPlayer:discoveryInfo:]: I am [<nil>] [7989F444CF2BDA83] discoveryInfo [{
        e = 2;
        h = A42FD7FD;
    }]

上面几行中的“我是 []...”是否重要?

我什至从 Ray Wenderlich 的网站下载并运行了创建多人游戏的教程,并进行了尝试。这表现出相同的问题,除非它在两个设备的前台运行。即使在前台运行,我的应用程序也不显示邀请请求。

有没有其他人遇到过这个问题或知道发生了什么事?从 applicationDidFinishLaunching 调用 authenticateLocalUser

最佳答案

让按姓名邀请工作的唯一方法是转至 Settings/Notifications/Game Center 并让 Game Center 显示Alerts,不是横幅。

如果你有 GC 显示警报,你会得到一个像这样的弹出框:

enter image description here

这个对话框就像一个大家长。如果用户点击 Accept,那么您的 [GKMatchmaker sharedMatchmaker].inviteHandler 将被调用。

如果用户点击拒绝,那么您的游戏永远不会知道他曾被邀请参加任何聚会。用户点击 Decline 意味着 parent 撕毁了邀请并且根本不告诉他的 child 他被邀请参加游戏。

这是我获得按姓名邀请工作的唯一方式。

关于iphone - 未显示游戏中心邀请,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15666479/

相关文章:

iOS 系统声音未播放

game-center - 游戏中心排行榜分数移除

iphone - 如何创建一个用图片来命名地名的测验应用程序?

ios - 将 objective-c 转换为 swift - GameKit

ios - UIView宽度问题

iPhone -> NSNumberFormatter - 没有货币代码的价格

iphone - 检查 UIViewController 是否有任何文本字段、 TextView 或标签更改..?

ios - 带有插图的 UIButton 标签文本自动收缩

iphone - ARC 循环保留检测

iphone - 在多台 Mac 上开发 iPhone 应用程序