1

I'm sure theres a 'simple' answer to this, but I've been having a difficult time finding it.

I've created an iOS card game that works in game center. At the moment, it determines the online players automatically and works fine.

What I want to do is have the user invite their game center friends to play. The documentation isn't really helping me.

I'm doing my project in swift, and I feel like I'm dealing with deprecated items and old documentation here?

I've managed to get the users list of friends and then I assume I need to instantiate a matchmaker with that list of friends.

What I'm stuck at, is trying to implement the "RecipientResponseHandler" code as per apple's guidelines:

request.recipientResponseHandler = ^(GKPlayer *player, GKInviteeResponse response)
    {
        [self updateUIForPlayer: player accepted: (response == GKInviteeResponseAccepted)];
    }; 

Question 1: First of all, What does the ^ symbol represent in the swift language?

Question 2: XCode doesn't seem to like the * symbol in the arguments...I've tried doing things like...

request.recipientResponseHandler = (player: GKPlayer, response: GKInviteeResponse){
        print("Do codeStuffHere")
};

But, I can't find a way of wording this that XCode will allow...

Question 3: How can I write this response handler to get it to work?

Question 4: Also,to listen for invites, I need to implement the GKLocalPlayerListener protocol...Anything else?

Question 5: I feel like I'm missing something glaring?

I've been learning on my own, and sometimes it takes a while to understand concepts without any direction, so please go easy on me internet. I'm just starting to understand completion handlers and closures...

Thanks in advance for any help.

TylerP
  • 9,600
  • 4
  • 39
  • 43
Dan_Druff
  • 11
  • 4

2 Answers2

1

What does the ^ symbol represent in the swift language?

XCode doesn't seem to like the * symbol in the arguments.

That's because that code is written in Objective-C, not Swift.

I'm assuming you're looking at the documentation for GKMatchRequest (update as of Jan 2023: It seems Apple has removed the code listing from this page at some point).

If so, then yeah, Apple just hasn't gotten around to updating all their documentation to use Swift instead of Objective-C (even on pages where the language selected is Swift, like this one).

Here's the Swift equivalent of that entire code listing:

func invite(friends: [GKPlayer]) {
    let request = GKMatchRequest()
    request.minPlayers = 2
    request.maxPlayers = 4
    request.recipients = friends
    request.inviteMessage = "Your Custom Invitation Message Here"
    request.recipientResponseHandler = { player, response in
        self.updateUI(for: player, accepted: response == .accepted)
    }
}

func updateUI(for player: GKPlayer, accepted: Bool) {
    // update your UI here
}
TylerP
  • 9,600
  • 4
  • 39
  • 43
  • Unfortunately I don't know much about GameKit, so I can't answer your Question 4, but maybe that would be a good candidate for a new separate question (since you're only really supposed to ask one question per post on StackOverflow anyway). – TylerP May 14 '20 at 01:37
0

For question 4, be sure to register the GKLocalPlayerListener and only register once, for example:

GKLocalPlayer.local.register(self)