iOS 7:如何为Game Center matchmaker设置邀请处理程序

时间:2014-01-16 15:56:08

标签: ios objective-c game-center

在iOS 7中处理来自其他玩家的邀请的正确方法是什么?

我的视图在我的根视图控制器上加载后,我正在调用游戏中心身份验证方法,之后我设置了一个这样的邀请处理程序:

[[GKLocalPlayer localPlayer] registerListener:self];

我的视图控制器采用GKLocalPlayerListenerGKInviteEventListener协议,顺便说一句,注册监听器AppDelegate的最佳位置是什么?也许或者也许是我自定义的Game Center单身人士?

我添加了GKInviteEventListener

中描述的方法
-(void)player:(GKPlayer *)player didAcceptInvite:(GKInvite *)invite
{
    NSLog(@"Invite accepted!");
    GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc] initWithInvite:invite];
    mmvc.matchmakerDelegate = self;
    [self presentViewController:mmvc animated:YES completion:nil];

}

但是,游戏中心媒人类有这样的主题:接收来自其他玩家的邀请和方法– matchForInvite:completionHandler: 我不明白如何使用它。

那么,我必须使用什么以及如何使用?

1 个答案:

答案 0 :(得分:7)

我认为你正确地做事。我以同样的方式做事,对我有用。 - 在iOS 7中不推荐使用matchForInvite:completionHandler,因此我不会对其执行任何操作。

首先,我设置了身份验证处理程序。当我的应用程序第一次加载时调用它,您可以在任何地方设置它,但最好尽快设置处理程序。

-(void)authenticateLocalPlayer
{
    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
    __weak GKLocalPlayer *blockLocalPlayer = localPlayer;

    //Block is called each time GameKit automatically authenticates
    localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error)
    {
        [self setLastError:error];
        if (viewController)
        {
            self.authenticationViewController = viewController;
            [self disableGameCenter];
        }
        else if (blockLocalPlayer.isAuthenticated)
        {
            [self authenticatedPlayer:blockLocalPlayer];
        }
        else
        {
            [self disableGameCenter];
        }
    };
}

如果验证成功,我会调用此方法:

-(void)authenticatedPlayer:(GKLocalPlayer*)localPlayer
{
    self.isAuthenticated = YES;
    [[NSNotificationCenter defaultCenter]postNotificationName:AUTHENTICATED_NOTIFICATION object:nil];
    [[GKLocalPlayer localPlayer]registerListener:self];
}

然后我实现了两个监听器方法:

(void)player:(GKPlayer *)player didAcceptInvite:(GKInvite *)invite
{
       //.... insert some cleanup code here to manage view controllers etc before presenting the matchmakerviewcontroller.
        [self presentMatchmakerViewControllerWithInvite:invite];
}


-(void)player:(GKPlayer *)player didRequestMatchWithPlayers:(NSArray *)playerIDsToInvite
{
    //......insert some cleanup code for managing view controllers 
    GKMatchRequest *match = [[GKMatchRequest alloc]init];
    match.playersToInvite = playerIDsToInvite;

    GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc]initWithMatchRequest:match];
    mmvc.matchmakerDelegate = root.viewControllers[0];
    [[[[[UIApplication sharedApplication]delegate]window]rootViewController]presentViewController:mmvc animated:YES completion:nil];
}