通过Multipeer连接发送和接收邀请

我知道这个问题之前已经被问过了,但是我只是想知道为什么这个问题不适合我的情况。

我试图从一个视图控制器发送multipeer连接的邀请,并在另一个视图控制器上接收。 我的代码发送它是:

[self invitePeer:selectedPeerID toSession:self.mySession withContext:nil timeout:timeInterval ]; 

和方法只是空白:

  - (void)invitePeer:(MCPeerID *)peerID toSession:(MCSession *)session withContext:(NSData *)context timeout:(NSTimeInterval)timeout { } 

我的代码接收和邀请是:

  - (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"); } } 

我有所有委托方法正确设置以及相同的服务types和。 但是当我尝试启动会话时,我点击的tableviewcell仍然突出显示…

我想我必须把一些东西在invitePeer toSession方法,但我不知道…

我直接从苹果的wwdc谈话中复制这个MultiPer连接引用我的代码…正如你可以看到它是我自己的代码实现,我不使用广告客户助理或mcbrowserviewcontroller。

有没有人有任何build议,我怎么能得到这个工作?

invitePeer: toSession: withContext: timeOut:方法由MCNearbyServiceBrowser实现,所以您应该在浏览器上调用它,而不是在self

[browser invitePeer:selectedPeerID toSession:self.mySession withContext:nil timeout:timeInterval ];

但是,如果您尝试解决接受邀请问题,则会暂时忽略警报视图,只需立即在didReceiveInvitation:广告客户didReceiveInvitation:callback中接受即可。

编辑

我原先说过,来自Multipeer Connectivity类的委托callback是在私有队列中进行的,但是@Juguang指出这只是MCSessionDelegatecallback的情况。

对于任何有兴趣的人,我创build了MCSessionP2P ,这是一个演示应用程序,演示了MCSession的ad-hocnetworkingfunction。 SessionController符合MCSessionDelegateMCNearbyServiceBrowserDelegateMCNearbyServiceAdvertiserDelegate并充当UITableView的数据源。 该应用程序通过Wi-Fi或蓝牙宣传自己,并以编程方式连接到可用的对等点,build立一个对等networking。