Game Center not authenticating using Swift

后端 未结 3 528
轻奢々
轻奢々 2020-12-06 03:09

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 th

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-06 03:43

    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.

提交回复
热议问题