Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Game Center not authenticating using Swift

I'm trying to authenticate the local player using swift, but every time I get a false value for the .authenticated property. Here is the code I'm using, it is called by the main view controller when the app starts.

func authenticateLocalPlayer(){
  var localPlayer = GKLocalPlayer()
  localPlayer.authenticateHandler = {(viewController, error) -> Void in
    if viewController {
      self.presentViewController(viewController, animated: true, completion: nil)
    }else{
      println((GKLocalPlayer().authenticated))
    }
  }
}

It brings up the log in view just fine, but when I enter a test account login, it just returns the GKLocalPlayer().authenticatedas false. The bundle identifier in iTunes Connect and the info.plist are exactly the same, as is the version and the app name. Everything is enabled for Game Center on iTunes Connect and in Xcode, but I have a feeling it's not a coding error, it's a setup error in the app record somewhere but I can't for the life of me find where.

After further tinkering, I'm getting this error:

Error Domain=GKErrorDomain Code=15 "The requested operation could not be completed because this application is not recognized by Game Center." UserInfo=0x17006b300 {NSLocalizedDescription=The requested operation could not be completed because this application is not recognized by Game Center.}

I have no idea why this is the case, the bundle ID, name and versions all match...

Any help would be greatly appreciated.

like image 607
Andy Heard Avatar asked Jun 04 '14 18:06

Andy Heard


People also ask

How do you authenticate Game Center?

Game Center also checks whether you configured your game for Game Center. To authenticate the user, set the authentication handler ( authenticateHandler ) on the shared instance of GKLocalPlayer that represents the player of your game as in: GKLocalPlayer. local.

How do you fix Game Center on iPhone?

Fo the issue with Game Center, restart iPhone first. Go to Settings > Game Center > Sign out and sign back in. If it continues, try connecting to a different Wi-Fi network or try from cell network.

What happened to Game Center on iOS?

The Game Center app was introduced in iOS 4.1. Apple discontinued the app in iOS 10 and moved some of its features to the iOS.

Is Game Center tied to Apple ID?

Game Center uses the Apple ID that's linked to your iPhone or iPad by default. Since Game Center accounts are tied to Apple accounts, you may have been under the notion that you cannot use a different account unless you completely sign out of your device.


2 Answers

This issue has been resolved by Apple - just call:

GKLocalPlayer.localPlayer()

Previously, the issue was that GKLocalPlayer() does not return the GKLocalPlayer singleton, but instead returns a new GKLocalPlayer instance.

If you were on the Xcode 6 BETA, you could add a C function or Objective-C method that returns the real GKLocalPlayer singleton, then use this in Swift. This is the gist of my workaround (with bad naming conventions):

In an Objective-C header:

GKLocalPlayer *getLocalPlayer(void);

In an Objective-C implementation:

GKLocalPlayer *getLocalPlayer(void) {
    return [GKLocalPlayer localPlayer];
}

In your bridging header:

#import "ThatHeader.h"

Then whenever you need to access the GKLocalPlayer singleton in Swift, you can just use getLocalPlayer() instead of GKLocalPlayer(). It's probably a better idea to stick that in an method of a GKLocalPlayer category.

However, this is no longer necessary as detailed above.

like image 99
marmph Avatar answered Nov 16 '22 03:11

marmph


Even with Xcode 6 Beta 6, on a device using iOS 8 beta 5, making GKLocalPlayer.localPlayer() available, I was still getting the error:

"NSLocalizedDescription=The requested operation could not be completed because this application is not recognized by Game Centre"

The solution (discovered through Apple's Dev forum) was to go to "Settings" on the device, and then into "Game Centre" and enable "Sandbox" under the developer section.

like image 23
Raz Avatar answered Nov 16 '22 02:11

Raz