Accepting an Interconnect Invitation

I hope I do not violate the NDA by posting this question.

I use the new connectivity option with many types to send some files via bluetooth to neighboring devices. I managed to send invitations, but it doesn't seem to me like to display a UIAlertView where the user can accept or decline the invitation. Right now, when the user submits, the file is automatically saved and there is no acceptance / rejection warning.

The code:

- (void) advertiser:(MCNearbyServiceAdvertiser *)advertiser didReceiveInvitationFromPeer:(MCPeerID *)peerID withContext:(NSData *)context invitationHandler:(void(^)(BOOL accept, MCSession *session))invitationHandler{ ... save the data context 

but with a warning:

 - (void) advertiser:(MCNearbyServiceAdvertiser *)advertiser didReceiveInvitationFromPeer:(MCPeerID *)peerID withContext:(NSData *)context invitationHandler:(void(^)(BOOL accept, MCSession *session))invitationHandler{ DevicePeer = [MCPeerID alloc]; DevicePeer = peerID; ArrayInvitationHandler = [NSArray arrayWithObject:[invitationHandler copy]]; // ask the user UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil]; [alertView show]; alertView.tag = 2; } 

and alert viewing method:

  - (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { // retrieve the invitationHandler // get user decision BOOL accept = (buttonIndex != alertView.cancelButtonIndex) ? YES : NO; // respond MCSession *session = [ArrayInvitationHandler objectAtIndex:0]; void (^invitationHandler)(BOOL, MCSession *) = [ArrayInvitationHandler objectAtIndex:0]; invitationHandler(accept, session); } 

When the user presses YES, the application crashes and I get an error message:

 [__NSMallocBlock__ nearbyConnectionDataForPeer:withCompletionHandler:]: unrecognized selector sent to instance 0x14d4e3b0' 

I looked at the iOS developer library and there is no such method except

 - (void)nearbyConnectionDataForPeer:(id)arg1 withCompletionHandler:(id)arg2{ } 

which does not work. No information on iOS developer forums. Any ideas?

+8
ios multipeer-connectivity
source share
2 answers

Alessandro is right, this is not explained in the WWDC 2013 video. I myself struggled with this.

I think that you are on the right track, you just have a couple of logical errors. I do not understand these two lines:

 MCSession *session = [ArrayInvitationHandler objectAtIndex:0]; void (^invitationHandler)(BOOL, MCSession *) = [ArrayInvitationHandler objectAtIndex:0]; 

The object stored in your array is your handler. The reason you encounter this failure is because the browser sees that accept is true and tries to connect the peer to the session, but the session that you are returning is zero. To fix this, you want to transfer the newly created session.

At first I was confused by the concept of creating a new session when it was already created by the browser side, but then I realized that we did not get this session from the browser anywhere, and we can not pass it back to the invitation handler if it does not exist!

So yes, do it instead:

 BOOL accept = (buttonIndex != alertView.cancelButtonIndex) ? YES : NO; // respond MCSession *session; if(accept) { session = [[MCSession alloc] initWithPeer:peer]; session.delegate = self; } void (^invitationHandler)(BOOL, MCSession *) = [ArrayInvitationHandler objectAtIndex:0]; invitationHandler(accept, session); 
+9
source share

I suggest you keep an eye on the connection with the Multipleer Connectivity WWDC 2013 network at the Apple Developer Center. Here is an example of this material, and it is very well explained.

PS: Yes, you break the NDA (September 14), but now it's fine :)

+1
source share

All Articles