iOS MCBrowserViewController 卡在 "Searching..."

标签 ios p2p multipeer-connectivity

我正在尝试使用广告商助手和浏览器 View Controller 来实现 iOS Multipeer Connectivity 框架。这看起来很简单,但浏览器从未找到我的其他设备。这是代码:

@property (nonatomic, strong) MCSession *peerSession;

- (void)startSession {
    MCPeerID *peerId = [[MCPeerID alloc] initWithDisplayName:[[UIDevice currentDevice] name]];
    self.peerSession = [[MCSession alloc] initWithPeer:peerId];
    self.peerSession.delegate = self;

    MCAdvertiserAssistant *advertiser = [[MCAdvertiserAssistant alloc] initWithServiceType:@"myapp" discoveryInfo:nil session:self.peerSession];
    [advertiser start];
}

- (void)openBrowser {
    MCBrowserViewController *peerBrowser = [[MCBrowserViewController alloc] initWithServiceType:@"myapp" session:self.peerSession];
    peerBrowser.delegate = self;
    [self.appDelegate.mainViewController presentViewController:peerBrowser animated:TRUE completion:nil];
}

这在两台 iOS 9.3 设备上运行,都打开了蓝牙,并连接到同一个 Wi-Fi 网络。浏览器窗口打开但显示“正在搜索...”并且从不显示其他设备。

我尝试在我尝试浏览的设备上只运行广告商代码。我还尝试直接使用 MCNearbyServiceAdvertiser 和 MCNearbyServiceBrowser。两种变化都没有影响。

我还没有找到任何讨论这个问题的网页,所以希望我遗漏了一些简单明了的东西!

最佳答案

您需要保留对您的 MCAdvertiserAssistant 的引用。它在 startSession 结束时超出范围并被释放。

@property (nonatomic, strong) MCAdvertiserAssistant *advertiser;

- (void)startSession {
    MCPeerID *peerId = [[MCPeerID alloc] initWithDisplayName:[[UIDevice currentDevice] name]];
    self.peerSession = [[MCSession alloc] initWithPeer:peerId];
    self.peerSession.delegate = self;

    self.advertiser = [[MCAdvertiserAssistant alloc] initWithServiceType:@"myapp" discoveryInfo:nil session:self.peerSession];
    [self.advertiser start];
}

关于iOS MCBrowserViewController 卡在 "Searching...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37448995/

相关文章:

javascript - PeerJS - connection.on ('open' )未执行

networking - 有人可以解释一下什么是线路级协议(protocol)吗?

cocoa-touch - 多点连接 : getting an invitation accepted (using built-in browser VC)

iphone - 带分隔线的 uitableview

iOS 帮助 : math. h?这是哪里?

ios - 使用来自 API 的 JSON 填充单元格

javascript - 使用 WebRTC/替代方案的点对点 1080p 直播?

macos - 有没有办法根据型号获取 Mac 的图标?

ios - 在 iOS 中,Gamekitframework 在发送消息方面是否比 multipeerconnectivity 框架更快?

ios - 异常 'Call must be made on main thread' iOS