GameCenter Invitation Handler

风流意气都作罢 提交于 2019-12-07 02:37:18

问题


trying to implement a multiplayer. Using the sample from Game Center - Sending and receiving data.

Everything seems okay, but in apple documentation there is also said about invitation handler.

[GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite) {
   // Insert application-specific code here to clean up any games in progress.
   if (acceptedInvite) {
        GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithInvite:acceptedInvite] autorelease];
        mmvc.matchmakerDelegate = self;
        [self presentModalViewController:mmvc animated:YES];
    } else if (playersToInvite) {
        GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];
        request.minPlayers = 2;
        request.maxPlayers = 4;
        request.playersToInvite = playersToInvite;

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

The problem is quite simple: I do not know where to add this code.


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/4639284/gamecenter-invitation-handler

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