发送和接收具有多对等连接的邀请

时间:2021-12-24 07:39:30

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...

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

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.

我直接从苹果的wwdc上复制了这一点,关于我代码中引用的多节点连接……正如你看到的,这是我自己的代码实现,我没有使用广告商助理或mcbrowserviewcontroller。

Does anyone have any suggestions as to how I can get this to work??

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

2 个解决方案

#1


3  

The invitePeer: toSession: withContext: timeOut: method is implemented by MCNearbyServiceBrowser so you should be calling that on the browser not on self.

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

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

【浏览器invitePeer:selectedPeerID toSession:自我。mySession withContext:零超时: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.

但是,如果您试图排除接受邀请的故障,我将暂时跳过警报视图,直接接受didreceiveinvite委托的回调。

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.

我最初指出,来自多对等连接类的委托回调出现在私有队列中,但正如@Juguang所指出的,这只是MCSessionDelegate回调的情况。

#2


3  

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.

对于任何感兴趣的人,我创建了MCSessionP2P,这是一个演示应用程序,演示了MCSession的特殊网络功能。SessionController遵守MCSessionDelegate、MCNearbyServiceBrowserDelegate和MCNearbyServiceAdvertiserDelegate,并充当UITableView的数据源。该应用通过Wi-Fi或蓝牙为自己做广告,通过编程连接到可用的对等网络,建立对等网络。

#1


3  

The invitePeer: toSession: withContext: timeOut: method is implemented by MCNearbyServiceBrowser so you should be calling that on the browser not on self.

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

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

【浏览器invitePeer:selectedPeerID toSession:自我。mySession withContext:零超时: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.

但是,如果您试图排除接受邀请的故障,我将暂时跳过警报视图,直接接受didreceiveinvite委托的回调。

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.

我最初指出,来自多对等连接类的委托回调出现在私有队列中,但正如@Juguang所指出的,这只是MCSessionDelegate回调的情况。

#2


3  

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.

对于任何感兴趣的人,我创建了MCSessionP2P,这是一个演示应用程序,演示了MCSession的特殊网络功能。SessionController遵守MCSessionDelegate、MCNearbyServiceBrowserDelegate和MCNearbyServiceAdvertiserDelegate,并充当UITableView的数据源。该应用通过Wi-Fi或蓝牙为自己做广告,通过编程连接到可用的对等网络,建立对等网络。