GameKit, GKGameSession how to add players to the session and send data?

296 views Asked by At

I created a session, shared it to another player and now I want to start a game. I can see a session with two players on both devices. So, it looks like we're ready to game but I need to change connected status before the game and I can do this on both devices.

But... when I do this on device A, I see that user A is connected and user B isn't. And after that, when I repeat the process on device B, I see vice versa situation. B is connected and A is not.

Here is my code that connect player and send the data:

session.setConnectionState(.connected) { (error) in
   if let err = error {
      assertionFailure(err.localizedDescription)
   }
   else {

      print("NC:",session.players(with: .notConnected))
      print(" C:",session.players(with: .connected))
            
      let m = MoveTransfer(move:1) // test struct to send/receive data
        
      session.send(m.data(), with: .reliable) { (error) in
         if let err = error {
            assertionFailure(err.localizedDescription)
         }
      }
   }
}

I'm getting error:

The requested operation could not be completed because there are no recipients connected to the session

By the way, I'm unable to change connected state on the simulator (iCloud is logged in).

I forgot to mention, that I'm working on a turn based game.

Edit

Tried again and now after several iterations I got this:

I have both players connected to session. But send data still doesn't work.

here is console output:

NC: [] // not connected array and connected array below
C: [<GKCloudPlayer: 0x17402e700>, id: playerID1, name: Player1,<GKCloudPlayer: 0x17402e900>, id: playerID2, name: Player2]

fatal error: The requested operation could not be completed because there are no recipients connected to the session.

Got this on two real devices.

1

There are 1 answers

0
Dima Deplov On

I was able to send data and receive it on the other device. I used loadSessions function to load all session (I think that loadSession by id would do the trick too). I did the following, D1 is a device 1 and D2 device 2:

1. D1: load all sessions and set player state to connected
2. D2: load all sessions and set player state to connected
3. D1: load all sessions again and set player state to connected
4. D1 || D2: send data
5. D2 || D1: data 256 bytes from player: <GKCloudPlayer: 0x1c022b380>, id: playerID, name: (null)

Although, I wasn't able to transfer data back and forth on both devices since if we'll add step 6. that sends data from device that just received it, we'll get an error: The requested operation could not be completed because there are no recipients connected to the session.

I do not sure what is the reason of it but I stop my efforts on this stage, since I think now, that I don't need to be connected to session to play turn based game.

I found this "new" iOS 10 APIs abandoned. By Apple and by devs consequently. If you one of those who still trying to use it drop me a note at my twitter (link in my bio) if you want to discuss GKGameSession and related topics.