Gkmatchmaker Invite Handler Deprecated

GKMatchMaker invite handler deprecated

GKInviteEventHandler to the rescue, and in particular take a look at GKLocalPlayerListener.

Conform to the GKLocalPlayerListener protocol and you should be OK. Below are the protocol methods, which look to be the intended replacement for invitationHandler, but split up in two parts.

- (void)player:(GKPlayer *)player didAcceptInvite:(GKInvite *)invite
- (void)player:(GKPlayer *)player didRequestMatchWithPlayers:(NSArray *)playerIDsToInvite

After you set up some object to conform to that, you just make a call to registerListener:.

[[GKLocalPlayer localPlayer] registerListener:yourObjectHere]

Don't worry about registering it as soon as possible, as the system caches the invites/challenges/turn based stuff, if there's no one to handle those and lets your listener know as soon as you set it up.

GameKit GKMatchMaker inviteHandler deprecated in iOS7, what is the replacement?

It looks like the intended replacement is the GKInviteEventListener protocol. You can see a reference to it in GKLocalPlayer.h; the GKLocalPlayerListener protocol extends it.

However, there's limited documentation on this protocol (you can search for it in the documentation window of Xcode 5, but I don't see it on the web).

Given the lack of documentation, it's probably safest to continue using the deprecated method for now. You'll need to continue using it anyway for iOS6.

How to accept an invitation in Game Center

  1. I register for invites as soon as the game is loaded and call the
    delegate when an invite is received
  2. So inviteReceived calls the match maker for dismissing the game
    center controller and creating the match
  3. And finally, when the match is being found, the method
    connectionStatusChanged takes care of presenting all the game's
    views and players and stuff

Here is the code:

I register for invites as soon as the game is loaded and call the delegate when an invite is received:

- (void)registerInvites {
[GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite) {
self.pendingInvite = acceptedInvite;
self.pendingPlayersToInvite = playersToInvite;
[delegate inviteReceived];
};
}

So inviteReceived calls the match maker for dismissing the game center controller and creating the match:

- (void)inviteReceived {
[[GCMultiplayerHelper sharedInstance] findMatchWithMinPlayers:2 maxPlayers:2 viewController:(UIViewController*)[self.superview nextResponder] delegate:self];
}

- (void)findMatchWithMinPlayers:(int)minPlayers maxPlayers:(int)maxPlayers viewController:(UIViewController *)viewController delegate:(id<GCMultiplayerHelperDelegate>)theDelegate {
if (!gameCenterAvailable) return;

matchStarted = NO;
self.match = nil;
self.presentingViewController = viewController;
delegate = theDelegate;

[presentingViewController dismissModalViewControllerAnimated:YES];
GKMatchmakerViewController *mmvc;

if (pendingInvite != nil) {

mmvc = [[[GKMatchmakerViewController alloc] initWithInvite:pendingInvite] autorelease];

} else {

GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];
request.minPlayers = minPlayers;
request.maxPlayers = maxPlayers;
request.playersToInvite = pendingPlayersToInvite;

mmvc = [[[GKMatchmakerViewController alloc] initWithMatchRequest:request] autorelease];

}

mmvc.matchmakerDelegate = self;
[presentingViewController presentModalViewController:mmvc animated:YES];

self.pendingInvite = nil;
self.pendingPlayersToInvite = nil;
}

And finally, when the match is been found, the method connectionStatusChanged takes care of presenting all the game's views, players and starting the match:

- (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *)theMatch
{
self.match = theMatch;
match.delegate = self;
if (!matchStarted && match.expectedPlayerCount == 0) {
NSLog(@"Ready to start match! - didFindMatch");
[presentingViewController dismissModalViewControllerAnimated:YES];

[self.delegate connectionStatusChanged:CONNECTIONSUCCESS];
}
}

How do I handle an invitation link from a game center game?

I was able to figure it out myself. There is no good answer to this question in Swift and the Apple Docs to this topic are more than confusing. Steps to make this work:

  1. Implement [func player(_ player: GKPlayer, didAccept invite: GKInvite)][1] in the class of your choice and tell the class to implement GKLocalPlayerListener

  2. In the above mentioned method initialize the MatchmakingViewController with the GKInvite instance received from the method.

  3. Try it. MatchmakingViewController will show up und start your game immediately.

For me it was quite hard to figure this out. Hope this post will help others.

Game Center Matchmaking Custom Invites

What does the ^ symbol represent in the swift language?

XCode doesn't seem to like the * symbol in the arguments.

That's because that code is written in Objective-C, not Swift.

I'm assuming you're looking at the documentation for GKMatchRequest (update as of Jan 2023: It seems Apple has removed the code listing from this page at some point).

If so, then yeah, Apple just hasn't gotten around to updating all their documentation to use Swift instead of Objective-C (even on pages where the language selected is Swift, like this one).

Here's the Swift equivalent of that entire code listing:

func invite(friends: [GKPlayer]) {
let request = GKMatchRequest()
request.minPlayers = 2
request.maxPlayers = 4
request.recipients = friends
request.inviteMessage = "Your Custom Invitation Message Here"
request.recipientResponseHandler = { player, response in
self.updateUI(for: player, accepted: response == .accepted)
}
}

func updateUI(for player: GKPlayer, accepted: Bool) {
// update your UI here
}


Related Topics



Leave a reply



Submit