Endre Olah
Endre Olah

Reputation: 955

How to accept Game Center invite in swift

I have some troubles to make the invite accept in swift.

could someone help me with the right coding? Here is mine

GKMatchmaker.sharedMatchmaker().matchForInvite(Invitation!, completionHandler = {(InvitedMatch:GKMatch!, error: NSError!) -> Void in
        if InvitedMatch != nil {
            myMatch=match

            LocalGame=false

            if let scene = GameScene.unarchiveFromFile(environment_Prefix!+"GameScene") as? GameScene {
                // Configure the view.
                let skView = self.view as SKView!
                //skView.showsFPS = true
                //skView.showsNodeCount = true

                /* Sprite Kit applies additional optimizations to improve rendering performance */
                skView.ignoresSiblingOrder = true

                /* Set the scale mode to scale to fit the window */
                scene.scaleMode = .Fill

                skView.presentScene(scene, transition: SKTransition.flipVerticalWithDuration(2.0))

            }
        }
    })

Thanks

Upvotes: 1

Views: 799

Answers (1)

Endre Olah
Endre Olah

Reputation: 955

Finally I have figured out the solution which works. I had to implement the GKLocalPlayerListener like this and call the match for invite within the delegate function.

   func player(player: GKPlayer!, didAcceptInvite invite: GKInvite!) {

    GKMatchmaker.sharedMatchmaker().matchForInvite (invite, {(InvitedMatch, error) in

        if InvitedMatch != nil {
            myMatch=InvitedMatch

            LocalGame=false

            if let scene = GameScene.unarchiveFromFile(environment_Prefix!+"GameScene") as? GameScene {
                // Configure the view.
                let skView = self.view as SKView!
                //skView.showsFPS = true
                //skView.showsNodeCount = true

                /* Sprite Kit applies additional optimizations to improve rendering performance */
                skView.ignoresSiblingOrder = true

                /* Set the scale mode to scale to fit the window */
                scene.scaleMode = .Fill

                skView.presentScene(scene, transition: SKTransition.flipVerticalWithDuration(2.0))

            }
        }
    })
}

To get the player function called I have to register the listener at the local player authenticated block like this:

localPlayer.registerListener(self)

Now game invite works perfectly.

Upvotes: 3

Related Questions