Swift - Game Center not available

前端 未结 3 904
名媛妹妹
名媛妹妹 2021-01-14 17:45

I\'m trying to implement Game Center in my Swift game. I have a menu view controller, where the user can press a \"SCORES\" button, which should take them to the Game Center

3条回答
  •  伪装坚强ぢ
    2021-01-14 18:15

    You can use that, I create a Easy Game Center for iOS game center in github https://github.com/DaRkD0G/Easy-Game-Center-Swift

    Message from France, Merry Christmas

    Begin

    (1) Add FrameWork GameKit.framework

    (2) Create two files :

    GKLocalPlayerHack.h

    #import 
    @interface GKLocalPlayerHack : NSObject
    GKLocalPlayer *getLocalPlayer(void);
    @end
    

    GKLocalPlayerHack.m

    #import "GKLocalPlayerHack.h"
    @implementation GKLocalPlayerHack
    GKLocalPlayer *getLocalPlayer(void) {
        return [GKLocalPlayer localPlayer];
    }
    @end
    

    (3) In your Swift Bridging Header.h (Objectic-c import)

    #import "GKLocalPlayerHack.h"
    

    Next

    class GameCenter {
    
    // Game Center
    
    let gameCenterPlayer=GKLocalPlayer.localPlayer()
    var canUseGameCenter:Bool = false {
        didSet{if canUseGameCenter == true {// load prev. achievments form Game Center
            gameCenterLoadAchievements()}
        }}
    var gameCenterAchievements=[String:GKAchievement]()
    
    /**
        builder
    */
    init(uiControlNow : UIViewController) {
    
    
        // Do any additional setup after loading the view.
        self.gameCenterPlayer.authenticateHandler={(var gameCenterVC:UIViewController!, var gameCenterError:NSError!) -> Void in
    
            if gameCenterVC != nil {
                //showAuthenticationDialogWhenReasonable: is an example method name. Create your own method that displays an authentication view when appropriate for your app.
                //showAuthenticationDialogWhenReasonable(gameCenterVC!)
                uiControlNow.presentViewController(gameCenterVC, animated: true, completion: { () -> Void in
                    // no idea
                })
            }
            else if self.self.gameCenterPlayer.authenticated == true {
                self.self.canUseGameCenter = true
            } else  {
                self.canUseGameCenter = false
            }
    
            if gameCenterError != nil
            { println("Game Center error: \(gameCenterError)")}
        }
    
    
    }
    
    
    /**
        Load prev achievement granted to the player
    */
    func gameCenterLoadAchievements(){
        // load all prev. achievements for GameCenter for the user to progress can be added
        var allAchievements=[GKAchievement]()
    
        GKAchievement.loadAchievementsWithCompletionHandler({ (allAchievements, error:NSError!) -> Void in
            if error != nil{
                println("Game Center: could not load achievements, error: \(error)")
            } else {
                for anAchievement in allAchievements  {
                    if let oneAchievement = anAchievement as? GKAchievement {
                        self.gameCenterAchievements[oneAchievement.identifier]=oneAchievement}
                }
            }
        })
    }
    
    
    /**
        Add progress to an achievement
    
        :param: Progress achievement Double
        :param: ID Achievement
    */
    func gameCenterAddProgressToAnAchievement(progress:Double,achievementID:String) {
        if canUseGameCenter == true { // only update progress if user opt-in to use Game Center
            // lookup if prev progress is logged for this achievement = achievement is already know (and loaded) form Game Center for this user
            var lookupAchievement:GKAchievement? = gameCenterAchievements[achievementID]
    
            if let achievement = lookupAchievement {
                // found the achievement with the given achievementID, check if it already 100% done
                if achievement.percentComplete != 100 {
                    // set new progress
                    achievement.percentComplete = progress
                    if progress == 100.0  {achievement.showsCompletionBanner=true}  // show banner only if achievement is fully granted (progress is 100%)
    
                    // try to report the progress to the Game Center
                    GKAchievement.reportAchievements([achievement], withCompletionHandler:  {(var error:NSError!) -> Void in
                        if error != nil {
                            println("Couldn't save achievement (\(achievementID)) progress to \(progress) %")
                        }
                    })
                }
                else {// achievemnt already granted, nothing to do
                    println("DEBUG: Achievement (\(achievementID)) already granted")}
            } else { // never added  progress for this achievement, create achievement now, recall to add progress
                println("No achievement with ID (\(achievementID)) was found, no progress for this one was recoreded yet. Create achievement now.")
                gameCenterAchievements[achievementID] = GKAchievement(identifier: achievementID)
                // recursive recall this func now that the achievement exist
                gameCenterAddProgressToAnAchievement(progress, achievementID: achievementID)
            }
        }
    }
    /**
        Remove One Achievements
    
        :param: ID Achievement
    */
    func resetAchievements(achievementID:String) {
    
        var lookupAchievement:GKAchievement? = gameCenterAchievements[achievementID]
    
        if let achievement = lookupAchievement {
            GKAchievement.resetAchievementsWithCompletionHandler({ (var error:NSError!) -> Void in
                if error != nil {
                    ToolKit.log("Couldn't Reset achievement (\(achievementID))")
                } else {
                    ToolKit.log("Reset achievement (\(achievementID))")
                }
            })
    
        } else {
            println("No achievement with ID (\(achievementID)) was found, no progress for this one was recoreded yet. Create achievement now.")
    
            gameCenterAchievements[achievementID] = GKAchievement(identifier: achievementID)
            // recursive recall this func now that the achievement exist
            self.resetAchievements(achievementID)
        }
    }
    

    }

提交回复
热议问题