0

In application, invitationDidFail is being called, some time it connects properly, but sometime it doesn't...

what can be possible reasons of denying the connection?

// Display an alert sheet indicating a failure to connect to the peer.
- (void) invitationDidFail:(SessionManager *)session fromPeer:(NSString *)participantID
{
NSString *str;
if (alertView.visible) {
    // Peer cancelled invitation before it could be accepted/rejected
    // Close the invitation dialog before opening an error dialog
    [alertView dismissWithClickedButtonIndex:0 animated:NO];
    [alertView release];
    str = [NSString stringWithFormat:@"%@ is busy.\nPlease again", participantID];
    //[peerList removeAllObjects];
    [self peerListDidChange:nil];
    [self.tableData reloadData];
    //[self TwoPlayer:self];
} else {
    // Peer rejected invitation or exited app.
    str = [NSString stringWithFormat:@"%@ is busy.\nPlease try again", participantID];
    //[peerList removeAllObjects];
    [self peerListDidChange:nil];
    [self.tableData reloadData];
    //[self TwoPlayer:self];
  }
}

Even it doesn't call this method, It is sure that device is not paired to any other device, then what are reasons that sometime it do accept and call didReceivedInvitation method, or some time it does deny by calling invitationDidFail.

// Invitation dialog due to peer attempting to connect.
- (void) didReceiveInvitation:(SessionManager *)session fromPeer:(NSString     *)participantID;
{
[alertView dismissWithClickedButtonIndex:1 animated:NO];

NSString *str = [NSString stringWithFormat:@"Incoming Invite from %@", participantID];
if (alertView.visible) {
    [alertView dismissWithClickedButtonIndex:0 animated:NO];
    [alertView release];
}
alertView = [[UIAlertView alloc] 
             initWithTitle:str
             message:@"Do you wish to accept?" 
             delegate:self 
             cancelButtonTitle:@"Decline" 
             otherButtonTitles:nil];
[alertView addButtonWithTitle:@"Accept"]; 
[alertView show];
}
Chatar Veer Suthar
  • 15,541
  • 26
  • 90
  • 154

1 Answers1

1

When I was recently writing an application using connections, I used GKSession. I spent weeks trying to debug connection issues with it and in the end I gave up and stopped using it. There seems to be a number of issues with GKSession when connecting especially if you disconnect and then attempt to reconnect within a short time (a short time could be 1 minute or more). It seems that the connection doesn't properly get dropped and then it doesn't recreate the connection properly.

In the end I took out all the GKSession code and used this instead. Worked a treat - no more connection issues.

GCD Async Socket

Nick Bull
  • 4,276
  • 1
  • 18
  • 25
  • I am using a game which is sending and receiving data on every click, so will it be in support of this theme? – Chatar Veer Suthar Jun 28 '11 at 09:03
  • Once you have created the connection using the GCD Async Socket, then you can use it just as you would GKSession - you can send and receive data as often as you want. In my testing, I found it to be quicker than the GKSession for transmitting data too. But that was quite limited testing as the GKSession was so intermittent. – Nick Bull Jun 28 '11 at 09:57
  • can u give me any code like chat system or data sending and receiving for iOS, just for guide? – Chatar Veer Suthar Jun 28 '11 at 11:52
  • All of my code is wrapped up inside projects that I can't easily strip out. There are examples with the GCD Async Socket code that should be sufficient to get things working (that's all I used). Depending on how you are going to do it, you might need to have a quick read of some Bonjour basics, but it took me one evening to get a connection working with it. – Nick Bull Jun 28 '11 at 11:58
  • Nick: it seems like you have to start a new session after you disconnect. Other than that though, I have to say that I really love the GKSession API. I believe you may be doing yourself and your users a disservice by not using it, as it will let you do ad-hoc multiplayer via bluetooth (which 4ms latency). – Mark Pauley Dec 14 '12 at 22:23
  • @MarkPauley Perhaps it has been improved since I used it over a year ago, but I was trying to start new sessions, etc. It just wouldn't reconnect regularly. Like I say, perhaps it is better now. But GCD Async Socket allowed me to have much easier access to everything and it allows ad-hoc bluetooth just like GKSession – Nick Bull Dec 14 '12 at 23:00
  • Interesting: I wonder what API this GCDAsync library uses to create an ad-hoc bluetooth connection. – Mark Pauley Dec 14 '12 at 23:23