我正在尝试使用广告商助手和浏览器 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/