GameCenter Invitation Handler
Asked Answered
B

2

6

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.

Bulb answered 9/1, 2011 at 13:18 Comment(1)
EXACT same question came to my mind ;)\Photogram
G
11

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

Guenon answered 9/1, 2011 at 13:25 Comment(2)
My problem. Have not read everything. Sorry for silly question.Bulb
Hello. Could you please see my question? #10971038Sandarac
E
1

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];
}];
Evangelize answered 31/3, 2013 at 20:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.