1

I have a GKSession between two apps. One is the server, the other the client. The server causes the client app to launch another app with a urlscheme command.

What I WISH would happen is that when the client app shuts down, the session ends, the other app opens up, and we start a new session.

What is actually happening is a bit of a mystery to me. Basically, the new app opens up and begins communicating with the server. A good number of packets are sent back and forth between the two... but after a short period of time (3-5 seconds), the GKPeerStateDisconnected case happens in the session:peer:didChangeState: with the previous app that was connected. This stops all communication between the applications.

I was originally using the peer picker for this and it worked just fine (except there were lots of manual button pressing you needed to do). Now I need to remove the peer picker and have this process happen automatically.

-(void)session:(GKSession *)session 
          peer:(NSString *)peerID 
didChangeState:(GKPeerConnectionState)state
{
    BOOL peerChanged = NO;
    NSLog(@"peer:%@ didChangeState: %d", peerID, state);

switch(state) {
    case GKPeerStateAvailable:
            [session connectToPeer:peerID withTimeout:1000];
            peerChanged = YES;
        break;

    case GKPeerStateUnavailable:
            peerChanged = YES;
        break;

    case GKPeerStateConnected:
        [self.peerList addObject:peerID];
        [self setupConnectionWithPeer:peerID toSession:session];
        break;

    case GKPeerStateDisconnected:        
        [self.peerList removeObject:peerID];
        if(self.peerList.count == 0) self.isConnected = NO;
        break;

    }
    NSLog(@"Number of peers: %d", self.peerList.count);
}
C4 - Travis
  • 4,502
  • 4
  • 31
  • 56
Andy
  • 38
  • 4

1 Answers1

0

It sounds like a limitation on Multitasking. Peer Picker may have additional system-level access that allows your backgrounded app to continue communicating with the server. Without using that code, your backgrounded app is given the traditional 3-5 seconds to finishing doing its operations and is then put into its background state, disconnecting it from the server.

Thomas Hajcak
  • 476
  • 3
  • 12
  • That all makes sense... but when the app goes to the background, I don't really need to communicate with that one anymore. I need to communicate with the new app that has opened up and sent the previous one into the background. The problem is that it STARTS communicating... and stops when the previous app is disconnected... so for a short time there are two apps connected. Wierdness – Andy Apr 28 '12 at 16:52
  • Have you checked your server code to see if it's accidentally disconnecting all connected clients when the "server" app (the one that launches the one you're having problems with) on the phone disconnects? – Thomas Hajcak Apr 29 '12 at 12:58
  • I made sure there weren't any calls that would set session.available = NO. I also made sure I wasn't calling [session disconnectFromAllPeers]. I also made sure I wasn't accidentally initializing a new session somehow, but that wasn't happening either. One thing I think I need to double check though... All the session delegate methods pass in a session object. What do we do with this? Isn't this the same object we initialized to start the session? Do I need to do anything with this passed in session object? – Andy Apr 30 '12 at 13:13