3

我知道以前有人问过这个问题,但我只是想知道为什么它在我的特定情况下不起作用。

我正在尝试从一个视图控制器发送来自多对等连接的邀请并在另一个视图控制器上接收它。我的发送代码是:

[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 方法中添加一些东西,但我不确定......

我直接从 Apple 在我的代码中引用的关于 Multipeer Connectivity 的 wwdc 演讲中复制了此内容……正如您所见,这是我自己的代码实现,我没有使用广告商助理或 mcbrowserviewcontroller。

有人对我如何让它工作有任何建议吗?

4

2 回答 2

3

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

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

但是,如果您尝试排除接受邀请的问题,我会暂时跳过警报视图,并didReceiveInvitation:在广告客户代表的回调中立即接受。

编辑

我最初说过来自 Multipeer Connectivity 类的委托回调进入私有队列,但正如@Juguang 指出的那样,这只是MCSessionDelegate回调的情况。

于 2013-10-22T14:17:12.513 回答
3

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

于 2013-12-03T19:51:39.413 回答