ios – 使用MCNearbyServiceBrowser时找不到广告商

前端之家收集整理的这篇文章主要介绍了ios – 使用MCNearbyServiceBrowser时找不到广告商前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个MultipeerService类,用于启动广告和浏览会话.出于某种原因,我不确定为什么我无法看到任何广告客户.

MultipeerService.m

-(void) startAdvertising
{
    NSString *name = [[UIDevice currentDevice] name];

    MCPeerID *peerId = [[MCPeerID alloc] initWithDisplayName:name];
    self.session = [[MCSession alloc] initWithPeer:peerId];
    self.session.delegate = self;

    self.advertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:peerId discoveryInfo:nil serviceType:kServiceType];
    self.advertiser.delegate = self;

    [self.advertiser startAdvertisingPeer];
}

-(void) startBrowsing
{
    NSString *name = [[UIDevice currentDevice] name];

    MCPeerID *peerId = [[MCPeerID alloc] initWithDisplayName:name];
    self.session = [[MCSession alloc] initWithPeer:peerId];
    self.session.delegate = self;

    self.browser = [[MCNearbyServiceBrowser alloc] initWithPeer:peerId serviceType:kServiceType];
    self.browser.delegate = self;

    [self.browser startBrowsingForPeers];
}

我启动广告客户,如下所示:

_multipeerConnectivityService = [[MultipeerConnectivityService alloc] init];
[_multipeerConnectivityService startAdvertising];

我创建了一个用于浏览的multipeerConnectivityService的新实例,并调用了startBrowsing方法.

当我在multipeerConnectivityService中检查foundPeer方法时,我看不到任何调用.我究竟做错了什么?

解决方法

您应该实现浏览器:didNotStartBrowsingForPeers:delegate方法.如果调用它,您收到的NSError对象将帮助您诊断问题.
- (void)browser:(MCNearbyServiceBrowser *)browser didNotStartBrowsingForPeers:(NSError *)error
{
    NSLog( @"Unable to start browsing for peers. Error: %@",error );
}

猜你在找的iOS相关文章