I'm implementing GameCenter for my game. I followed tutorials and created my ids for leaderboards and achievements.
Authenticating and achievements are working well. But GameCenter does not show scores for my levels.
I'm using the code below for sending score to GameCenter servers. Sending operation always returns success. But score not showing up.
GKScore *scoreReporter = [[GKScore alloc] initWithLeaderboardIdentifier: identifier];
scoreReporter.value = score;
scoreReporter.context = 0;
[GKScore reportScores:#[scoreReporter] withCompletionHandler:^(NSError *error) {
if (error == nil) {
NSLog(#"Score reported successfully!");
} else {
NSLog(#"Unable to report score!");
}
}];
I've read some of the other threads and they are not consistent enough. My ids right and the code is working well. With iOS 9 sandbox option is not available. So selecting sandbox from device's settings is not the issue.
Some of the reasons I can think of from my research why it is not working are these;
Scores will take time to sync (It has been 5 hours since my first try)
Small scores might not be sent to server sometimes (this is a very silly bug if this is happening but it was mentioned in the other threads)
Some kind of problem with testing (It's not the issue I think because achievements is working)
Any help will be appreciated.
After a day passed it started working. It think I had bad luck about the time passed this shouldn't be that long.
I'm developing a turn based game for iOS with a custom interface and i discovered a very odd problem with my matchmaking interface. The following code is used to display a list of active matches, i authenticate the user, then get the list of matches and the last step is to load the match so i can display all the info.
The problem appears when i build the app, go to the matchmaking view controller and leave it alone for 5 minutes; then when i try deleting a match i get an error in loadingMatchWithID:
Error Domain=NSCocoaErrorDomain Code=4097 "The operation couldn’t be completed. (Cocoa error 4097.)
The code works fine every time, deleting, creating matches, refreshing, but if i leave the view controller alone for 5 minutes and then try to delete i get the error. The odd part is the that the localPlayer passes an authentication test, and loads correctly the matches array then stops in loading the match.
One other thing happens, if the error appears and i push the home button and open back the app, everything words again and the matches are loaded correctly.
I think i a problem with the authentication, but where is the mistake ?
UPDATE: The issue appears in iOS7, but in iOS6 it works !
[localPlayer authenticateWithCompletionHandler:^(NSError *error)
{
if (error)return;
[GKTurnBasedMatch loadMatchesWithCompletionHandler:^(NSArray *matches, NSError *error)
{
for (int i = 0; i < matchesArray.count; i++)
{
[GKTurnBasedMatch loadMatchWithID:[[matchesArray objectAtIndex:i]matchID] withCompletionHandler:^(GKTurnBasedMatch *updatedMatch, NSError *error)
{
if (error != nil)
{
NSLog(#"Error: %#",error.description);
}
}];
}
}];
}];
I found what was the issue after many days of searching and testing everything. The problem was with quitting and then removing a match, the removeWithCompletionHandler: was inside the participantQuitInTurnWithOutcome: and somehow these actions would log out the player from game center without any notice, and the odd thing was that all the .isAuthenticated test would succeed.
Probably the most frustrating part was that the code worked for the most part, and worked every time on devices below iOS 7.
I'm trying to get local matchmaking working in GameKit using [[GKMatchmaker sharedMatchmaker] startBrowsingForNearbyPlayersWithReachableHandler:]. Essentially, I'm trying to implement interface-less local matches: as long as there's a player in my local vicinity, I want to connect and start a match. Importantly, I only want to do this for local players: I never want to match automatically over the internet.
I've enabled Game Center for my app in iTunes connect and signed up for a different sandbox account on every device I'm using to test.
I've tried both matchmaking with GKMatchmakerViewController (after authenticating the local player) and programmatic matchmaking with startBrowsingForNearbyPlayersWithReachableHandler:, running the same code on an iPhone 4 and an 4th gen iPod Touch sitting next to each other on my desk. Neither ever finds the other; when using GKMatchmakerViewController the interface for finding nearby players remains at the
Finding Players...
spinner, and when using startBrowsingForNearbyPlayersWithReachableHandler:, the notification block never gets called.
Here's my current block of testing code:
-(void)connectLocal
{
if( ![GKLocalPlayer localPlayer].isAuthenticated )
{
// authenticateLocalPlayer calls connectLocal again after authentication is complete
[ self authenticateLocalPlayer ];
return;
}
[[GKMatchmaker sharedMatchmaker] startBrowsingForNearbyPlayersWithReachableHandler:^(NSString *playerID, BOOL reachable) {
NSLog(#"Reachability changed for player %#", playerID );
} ];
}
The docs are a little sparse & confusing on the subject, especially when it comes to the difference between local mulitplayer and matches over the internet. For instance, it seems to be necessary to authenticate the local player and create a match before finding players to join that match (Creating Any Kind of Match Starts with a Match Request). However this little nugget seems to suggest otherwise:
The standard user interface allows players to discover other nearby players, even when neither player is currently connected to Game Center directly.
Additionally, in the flow described in Searching For Nearby Players, a match request isn't created until step 3, after finding players via the notification block passed to startBrowsingForNearbyPlayersWithReachableHandler:. Unfortunately, I've never got that far.
So, the questions:
1) Am I right in thinking I can call startBrowsingForNearbyPlayersWithReachableHandler: before authenticating the local player? GameKit doesn't throw an error, so I'm assuming it's OK. This may be a rash assumption. Whether I authenticate or not doesn't seem to make much difference.
2) Has anyone successfully implemented local auto-matching using [GKMatchmaker sharedMatchmaker] startBrowsingForNearbyPlayersWithReachableHandler:? Is there good example code anywhere that illustrates the complete flow, from browsing for players to starting a match, all programmatically?
3) There seem to be conflicting reports on the web over whether GameKit-enabled apps can be tested in the iOS Simulator. General consensus seems not, and it's better to test on iOS hardware. I've been using a iPhone 4 & an 4th gen iPod Touch. For those who have successfully tested local multiplayer, what testing setup & methodology did you use?
1) Am I right in thinking I can call startBrowsingForNearbyPlayersWithReachableHandler: before authenticating the local player?
No. startBrowsingForNearbyPlayersWithReachableHandler works by both advertising the existing player and browsing for other players but, most importantly, the information it uses to identify players is the playerID... which won't be available until the player authenticates.
3) There seem to be conflicting reports on the web over whether GameKit-enabled apps can be tested in the iOS Simulator. General consensus seems not, and it's better to test on iOS hardware
GameCenter authentication, achievements, and leaderboards work in the simulator, everything else should be tested on real hardware. I actually recommend the simulator for authentication testing, since it avoids the sandbox/production switch which can make detailed auth testing on devices somewhat confusing. Everything else can only be tested on devices. The simulator doesn't have great support for receiving push notifications which breaks match setup and the general hardware configuration is different enough that match communication isn't unlikely to work right anyway.
You need to do these things in this order:
Authenticate the local player
Install an invite handler
Start browsing for nearby players
Authentication is required - this registers your app with Game Center and logs the player in. In most cases, you won't even need internet access to do this.
Installing the invitation handler is also required, and I think this is the step you're missing. This lets your app know what to do when an inbound invitation is received. If you don't do this, a device won't register as being nearby.
Only start browsing once you've done the above two.
Here's some sample code to get you going. Call this method after the app launches:
- (void) authenticateLocalPlayer
{
static BOOL gcAuthenticationCalled = NO;
if (!gcAuthenticationCalled) {
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
void (^authenticationHandler)(UIViewController*, NSError*) = ^(UIViewController *viewController, NSError *error) {
NSLog(#"Authenticating with Game Center.");
GKLocalPlayer *myLocalPlayer = [GKLocalPlayer localPlayer];
if (viewController != nil)
{
NSLog(#"Not authenticated - storing view controller.");
self.authenticationController = viewController;
}
else if ([myLocalPlayer isAuthenticated])
{
NSLog(#"Player is authenticated!");
//iOS8 - register as a listener
[localPlayer unregisterAllListeners];
[localPlayer registerListener:self];
[[GKLocalPlayer localPlayer] loadFriendPlayersWithCompletionHandler:^(NSArray *friendPlayers, NSError *error) {
//Do something with the friends
}];
//iOS7 - install an invitation handler
[GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite) {
// Insert game-specific code here to clean up any game in progress.
if (acceptedInvite)
{
//This player accepted an invitation.
//If doing programmatic matchmaking, call GKMatchmaker's matchForInvite:completionHandler
//to get a match for the invite. Otherwise you need to allocate a GKMatchmakerViewController
//instance and present it with the invite.
}
else if (playersToInvite)
{
//Your game was launched from the GameCenter app to host a match.
}
};
//Now you can browse. Note this is the iOS8 call. The iOS7 call is slightly different.
[[GKMatchmaker sharedMatchmaker] startBrowsingForNearbyPlayersWithHandler:^(GKPlayer *player, BOOL reachable) {
NSLog(#"Player Nearby: %#", player.playerID);
}];
}
else
{
//Authentication failed.
self.authenticationController = nil;
if (error) {
NSLog([error description], nil);
}
}
};
localPlayer.authenticateHandler = authenticationHandler;
gcAuthenticationCalled = YES;
}
}
* IMPORTANT *
If you're using iOS8, you don't install the invitation handler. You instead register an object as listening for the protocol GKLocalPlayerListener, and implement these methods:
-player:didAcceptInvite:
-player:didRequestMatchWithRecipients:
If you don't implement these methods on iOS8, it won't work!
You then link GKMatchmaker to that object by calling this after authenticating the local player:
[localPlayer registerListener:self];
Make sure the object that's implementing the protocol is declared like so in the .h file:
#interface MyImplementingObject : NSObject <GKMatchDelegate, GKLocalPlayerListener>
If you do all this and it still isn't working, make sure that you have your bundle ID set correctly in your app (Click the app, click 'Targets', make sure Bundle Identifier and Version are filled in), then click the 'Capabilities' tab (XCode 6), and make sure Game Center is on.
Go to the Member Center and make sure that the app using that bundle ID also has Game Center enabled for its Provisioning Profile. Download and reapply your Provisioning Profile if necessary.
Make sure the sandbox switch is ON in your Settings under GameCenter, and also make sure that 'Allow Invites' and 'Nearby Players' switches are turned ON.
Finally, make sure you go to iTunes Connect and verify that Game Center is enabled for your app there as well.
So, keep in mind the differences between iOS7 and iOS8. This code will work on either version and call 'updateNearbyPlayer' in turn.
if ( IS_IOS8 )
{
[[GKMatchmaker sharedMatchmaker] startBrowsingForNearbyPlayersWithHandler:^(GKPlayer *gkPlayer, BOOL reachable)
{
NSLog(#"PLAYER ID %# is %#",gkPlayer.playerID,reachable?#"reachable" : #"not reachable");
[self updateNearbyPlayer:gkPlayer reachable:reachable];
}];
} else {
/*
* iOS 7...
*/
[[GKMatchmaker sharedMatchmaker] startBrowsingForNearbyPlayersWithReachableHandler:^(NSString *playerID, BOOL reachable)
{
NSLog(#"PLAYER ID %# is %#",playerID,reachable?#"reachable" : #"not reachable");
[GKPlayer loadPlayersForIdentifiers:#[playerID] withCompletionHandler:^(NSArray *players, NSError *error) {
NSLog(#"Loaded: %#, error= %#",players,error);
GKPlayer *gkPlayer = [players objectAtIndex:0];
[self updateNearbyPlayer:gkPlayer reachable:reachable];
}];
}];
}
With some delay due to the underlying Bonjour services, this mechanism works great. However, it needs to be balanced with an appropriate call to:
[[GKMatchmaker sharedMatchmaker] stopBrowsingForNearbyPlayers];
Whenever you use one of the GKPlayers/PlayerIDs reported to establish a match or to add it to an existing match, you should stop browsing. Once the match has been finished, closed or cancelled, start browsing again. Otherwise, on the second attempt to connect to a nearby device, you'll fail.
I am developing an app that reports a score to Game Center using the code below (as suggested by Apple).
My problem is that even when my iPhone is in Airplane mode, the app does not trigger any score reporting error. It just goes to the "Submission ok" section of the code.
Any idea why?
Thank you!
GKScore *scoreReporter = [[[GKScore alloc] initWithCategory:category] autorelease];
scoreReporter.value = score;
[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
if (error != nil)
{
// handle the reporting error
NSLog(#"Error Descr %#",error.localizedDescription);
NSLog(#"Error Code %#",error.code);
NSLog(#"Error Domain %#",error.domain);
}
else {
NSLog(#"Submission ok");
}
}];
Starting with iOS 5.0, any network errors arising out of reportScoreWithCompletionHandler are handled internally by GameKit. This means that developers no longer have to worry about resubmitting scores pending due to network failures. If you're building with iOS 5.0 and later, the completion handler of reportScoreWithCompletionHandler will not receive any network-related errors.
I would suggest using Apple's reachability flags to detect an active connection yourself. If a connection isn't available, store your Game Center requests for future submission and submit them when network becomes available again. More on reachability can be found here
So far I have had success in implementing Game Center for my app. Authorizing players is OK, so is reporting Achievements.
My issue is when I wanted to test app behavior with my iPad in flight mode.
The player will not get authorized (as I expected, so no issue) with this code.
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
if ([localPlayer isAuthenticated] == YES){
NSLog(#"The local player has already authenticated.");
return;
} else {
[localPlayer authenticateWithCompletionHandler:^(NSError *error) {
if (error == nil){
NSLog(#"Successfully authenticated the local player.");
NSLog(#"Player Alias = %#", [localPlayer alias]);
} else {
NSLog(#"Failed to authenticate the player with error = %#", error);
}
}];
}
But when I later on in a UIView check if the player is authorized (so I know if I shall enable my show achievement button) with this code [achievementButton setEnabled:[localPlayer isAuthenticated]];I always get a YES as long as a user was logged in to Game Center before entering Flight Mode.
It seems like even if there is no connection to Game Center servers a previous authorized player is still seen as authorized.
This leads to that my button is shown but of course Game Center reports that it can not connect.
So, what would be the best way to check that a true connection to Game Center is available?
Cheers
I don't think there is an API to check that, but you can always make a call to one of the API that is going to return results, like getting leaderboards. It will fail if you're not connected.
Have a look at the SCNetworkReachability interfaces. Search in the docs for SCNetworkReachability, and there are other SO questions that reference this. Basically, you can use the Reachability APIs to continuously keep updated a flag that you can check to know whether or not you have a network connection.