Search code examples
iosswiftgame-center

Incoming GameCenter invite contains no players


I’m trying to get my game to allow 1 device (iPhone) to use GameCenter to invite a friend to play (iPad). I’m using the standard/default MatchMaker interface. The iPhone sends the invitation to the iPad which presents a notification.

When I press this notification the iPad’s ‘player(GKPlayer, didAccept: GKInvite)’ routine DOES get called.

  @objc func player(_ playerMe: GKPlayer, didAccept invite: GKInvite) {
    print("\n\n\t\tplayer \(playerMe.displayName)(\(playerMe.playerID)) did accept INVITE sent by \(invite.sender.displayName)(\(invite.sender.playerID))")
    GKMatchmaker.shared().match(for: invite, completionHandler: {(InvitedMatch, error) in

      print("\t\tplayers.count = \(InvitedMatch!.players.count)")

      if error != nil {
        print("INVITE ERROR: \(error.debugDescription)")
      }

      if InvitedMatch != nil {
        print("\t\tSetting current match. (\(InvitedMatch.debugDescription))")
        self.currentMatch = InvitedMatch
        self.currentMatch?.delegate = self
//        self.prepareMatch()
      }
    })
  }

Output:

        player Me(G:25139341913) did accept INVITE sent by ‎“-----”(G:12453976)
        players.count = 0
        Setting current match. (Optional(<GKMatch 0x282d39970 expected count: 1 seqnum: 0
    G:12453976:unknown
reinvitedPlayers:(
)>))

The players array is EMPTY! Shouldn’t it at least have the inviter in there? The ‘expectedPlayerCount’ properly reflects 2 person matchRequest where 1 player (the inviter) is already a participant)

At no point was the ‘player(GKPlayer, didRequestMatchWithRecipients: [GKPlayer])’ called by either end.

So the iPad doesn’t have access to the players to setup the match, but the iPhone sees that the invitation was accepted, has 2 players, and moves on. The iPhone code:

  func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFind match: GKMatch) {
    print("\n\n\t\tMATCH FOUND\n\n")
    viewController.dismiss(animated: true, completion: nil)

    GKMatchmaker.shared().stopBrowsingForNearbyPlayers()
    currentMatch = match
    match.delegate = self

    if Globals.gameState?.currentState is StateWaitingForMatch {
      if currentMatch?.expectedPlayerCount == 0 {
        prepareMatch()
      }
    }
  }

So how do I get the iPad (recipient of the invitation) to see/include the players?


Solution

  • In the player(GKPlayer, invite: GKInvite) method create a GKMatchMakerViewController via

    let mmvc = GKMatchmakerViewController(invite: invite!)

    and then present it:

    viewController.present(mmvc!, animated: true, completion: nil)