2013-10-21 126 views
3

我知道这个问题以前已经问过,但我只是想知道为什么它不适用于我的具体情况。发送和接收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"); 
      } 
    } 

我都委托方法正确设置以及相同的服务类型和。但是,当我尝试发起会话,我点击刚刚仍然强调tableviewcell ...

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

我直接从苹果公司的wwdc谈话中复制了MultiPer Connectivity在我的代码中引用的连接...正如你可以看到它是我自己的代码实现,我没有使用广告客户助理或mcbrowserviewcontroller。

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

回答

3

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

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

但是,如果你想解决接受邀请我跳过警报视图现在只接受在didReceiveInvitation:回调广告主委托的时候了。

编辑

我倒是原先指出,从Multipeer连接类委托回调排在私人队列但@Juguang指出,这仅仅是MCSessionDelegate回调的情况。

+0

不错!谢谢! – falky

+2

从我的观察来看,只有SessionDelegate方法是从非主队列中调用的(例如com.apple.MCSession.SyncQueue),NearbyServiceBrowser或者Advertiser's是从主队列中调用的。 – Juguang

+0

@Juguang是的,你说得对 - 我在一段时间后注意到了这一点。忘了所有关于这个问题。编辑 - 谢谢。 – ChrisH

3

对于任何有兴趣的人,我创建了MCSessionP2P,这是一个演示应用程序,演示了MCSession的ad-hoc网络功能。 SessionController符合MCSessionDelegate,MCNearbyServiceBrowserDelegateMCNearbyServiceAdvertiserDelegate,并充当UITableView的数据源。该应用程序通过Wi-Fi或蓝牙进行宣传,并以编程方式连接到可用的对等点,从而建立点对点网络。

+0

谢谢Marco!这真的很有帮助! – falky

+1

@Macro:我已经下载了你的代码,它工作正常,但有时连接,有些不是,你能告诉我怎么做吗? –

+0

我也一样。它只连接一些时间。 –