简体   繁体   中英

iOS Multipeer Connectivity didReceiveInvitationFromPeer did not

I know this question has been asked many times, but after reading each of them more than a few times, I still can't get my Multipeer Connectivity to work. I am sending but not receiving the invitation. Here is the code:

@implementation MPCManager

- (id)init {
  self = [super init];

  if (self) {
    _myPeerID = nil;
    _session = nil;
    _browser = nil;
    _advertiser = nil;
  }

  return self;
}

- (void)automaticBrowseAndAdvertiseWithName:(NSString *)displayName {
  _myPeerID = [[MCPeerID alloc] initWithDisplayName:displayName];
  _session = [[MCSession alloc] initWithPeer:_myPeerID];
  _session.delegate = self;

  _advertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:_myPeerID
                                                  discoveryInfo:nil
                                                    serviceType:@"trm-s"];
  _advertiser.delegate = self;
  [_advertiser startAdvertisingPeer];

  _browser = [[MCNearbyServiceBrowser alloc] initWithPeer:_myPeerID
                                              serviceType:@"trm-s"];
  _browser.delegate = self;
  [_browser startBrowsingForPeers];
}

- (void)session:(MCSession *)session
    didReceiveCertificate:(NSArray *)certificate
                 fromPeer:(MCPeerID *)peerID
       certificateHandler:(void (^)(BOOL accept))certificateHandler {
  certificateHandler(YES);
}

- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser
    didReceiveInvitationFromPeer:(MCPeerID *)peerID
                     withContext:(NSData *)context
               invitationHandler:(void (^)(BOOL,
                                           MCSession *))invitationHandler {
  NSLog(@"This is NOT getting called");
}

- (void)browser:(MCNearbyServiceBrowser *)browser
    didNotStartBrowsingForPeers:(NSError *)error {
  NSLog(@"%@", [error localizedDescription]);
}
- (void)browser:(MCNearbyServiceBrowser *)browser
            foundPeer:(MCPeerID *)peerID
    withDiscoveryInfo:(NSDictionary *)info {
  NSLog(@"This IS getting called");
}

- (void)invitePeer:(MCPeerID *)peerID {
  NSLog(@"This IS getting called");
  [_browser invitePeer:peerID toSession:_session withContext:nil timeout:30];
}

I am running it on two simulators, and it was working for some time, but stopped suddenly. Any ideas on how or where to look for the problem?

Make sure that you are serializing and reusing your MCPeerID objects whenever possible. Each time you call - (instancetype)initWithDisplayName:(NSString *)myDisplayName it returns a unique instance.

What often happens in a dev environment is that you end up with a flood of advertisers and browsers and a ton of ghost duplicates in the Bonjour advertising space. This can cause everything to just go wonky.

If you are using simulators then resetting them may help. On hardware you can restart or toggle airplane mode.

Take a look at this year's WWDC session on Multipeer named "Cross Platform Nearby Networking". It has some good best practices to follow that will help immensely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM