问题
I know this question has been asked before, but I was just wondering why it isn't working in my particular case.
I am trying to send an invitation from multipeer connectivity from one view controller and receive it on another. My code for sending it is:
[self invitePeer:selectedPeerID toSession:self.mySession withContext:nil timeout:timeInterval ];
and method is just blank:
- (void)invitePeer:(MCPeerID *)peerID toSession:(MCSession *)session withContext:(NSData *)context timeout:(NSTimeInterval)timeout
{
}
My code for receiving and invitation is:
- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser didReceiveInvitationFromPeer:(MCPeerID *)peerID withContext:(NSData *)context invitationHandler:(void(^)(BOOL accept, MCSession *session))invitationHandler
{
// http://down.vcnc.co.kr/WWDC_2013/Video/708.pdf -- wwdc tutorial, this part is towards the end (p119)
self.arrayInvitationHandler = [NSArray arrayWithObject:[invitationHandler copy]];
// ask the user
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:peerID.displayName
message:@"Would like to create a session with you"
delegate:self
cancelButtonTitle:@"Decline" otherButtonTitles:@"Accept", nil];
[alertView show];
}
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// retrieve the invitationHandler and check whether the user accepted or declined the invitation...
BOOL accept = (buttonIndex != alertView.cancelButtonIndex) ? YES : NO;
// respond
if(accept) {
void (^invitationHandler)(BOOL, MCSession *) = [self.arrayInvitationHandler objectAtIndex:0];
invitationHandler(accept, self.mySession);
}
else
{
NSLog(@"Session disallowed");
}
}
I have all the delegate methods correctly set up as well as the same service types and that. But when i try to initiate the session, the tableviewcell which i click on just remains highlighted...
I'm thinking I have to put something in the invitePeer toSession method but I'm not sure...
I copied this directly from Apple's wwdc talk on Multipeer Connectivity referenced in my code... As you can see it is my own implementation of the code and I am not using an advertiser assistant or the mcbrowserviewcontroller.
Does anyone have any suggestions as to how I can get this to work??
回答1:
The invitePeer: toSession: withContext: timeOut:
method is implemented by MCNearbyServiceBrowser
so you should be calling that on the browser not on self
.
[browser invitePeer:selectedPeerID toSession:self.mySession withContext:nil timeout:timeInterval ];
However, if you're trying to troubleshoot accepting invitations I'd skip the alert view for now and just accept right away in the didReceiveInvitation:
callback of the advertiser delegate.
Edit
I'd originally stated that the delegate callbacks from Multipeer Connectivity classes came in on private queues but as @Juguang pointed out this is only the case for MCSessionDelegate
callbacks.
回答2:
For anyone interested, I created MCSessionP2P, a demo app that illustrates the ad-hoc networking features of MCSession
. SessionController
conforms to MCSessionDelegate
, MCNearbyServiceBrowserDelegate
and MCNearbyServiceAdvertiserDelegate
and acts as the datasource for a UITableView
. The app advertises itself via Wi-Fi or Bluetooth and programmatically connects to available peers, establishing a peer-to-peer network.
来源:https://stackoverflow.com/questions/19486762/sending-and-receiving-an-invitation-with-multipeer-connectivity