GameCenter Invitation Handler

南楼画角 提交于 2019-12-05 07:27:47

As stated in the docs

Your application should set the invitation handler as early as possible after your application is launched; an appropriate place to set the handler is in the completion block you provided that executes after the local player is authenticated.

Somewhere in your code, you should have authenticated the local player with something like this

[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) {
    if (error == nil) {
        // Insert your piece of code here
    } else {
        // Handle the error
    }
}];

Hope that helps

idea2real

My code is below, and it works very well. In the authenticateLocalUser, add the code below:

[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) {
    [GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite) {  // Add for invite handler
        // Insert application-specific code here to clean up any games in progress.
        if (acceptedInvite) {
            GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc] initWithInvite:acceptedInvite] ;
            mmvc.matchmakerDelegate = self;
            // [self presentModalViewController:mmvc animated:YES];
            [_delegate matchStart];
        } else if (playersToInvite) {
            GKMatchRequest *request = [[GKMatchRequest alloc] init] ;
            request.minPlayers = 2;
            request.maxPlayers = 2;
            request.playersToInvite = playersToInvite;

            GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc] initWithMatchRequest:request] ;
            mmvc.matchmakerDelegate = self;
            // [self presentModalViewController:mmvc animated:YES];
            [_delegate matchStart];
        }
    };

    [self callDelegateOnMainThread:@selector(processGameCenterAuth:) withArg:NULL error:error];
}];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!