iOS 7: How to setup invitation handler for Game Center matchmaker

自作多情 提交于 2019-11-30 09:07:14

问题


What is the proper way to handle invitations from other players in iOS 7?

After my view did load on my root View Controller, I'm calling a game center authentication method, after that I'm setup a invitation handler like so:

[[GKLocalPlayer localPlayer] registerListener:self];

My view controller adopt GKLocalPlayerListener and GKInviteEventListener protocols, by the way, what is the best place to register a listener AppDelegate? Maybe or maybe my custom Game Center singleton?

I add a method described in 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];

}

But, game center matchmaker class have such topic: Receiving Invitations From Other Players and method – matchForInvite:completionHandler: I don't understand how to use it.

So, what I must use and how?


回答1:


I think you're doing things correctly. I did things the same way, and it works for me. -matchForInvite:completionHandler is deprecated in iOS 7, so I don't do anything with it.

First I set the authentication handler. This is called when my app loads for the first time, you can set it anywhere you like, but it is best to set the handler as soon as possible.

-(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];
        }
    };
}

If authentication is successful, then I call this method:

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

Then I implemented the two listener methods:

(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];
}


来源:https://stackoverflow.com/questions/21166617/ios-7-how-to-setup-invitation-handler-for-game-center-matchmaker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!