我想使用多点连接框架连接 2 个设备,其中一个设备在后台运行应用程序,就像 Firechat 一样(我无法确认这是否有效,我已将其安装在 iPhone 5S 上,并且4,但他们就是找不到对方 - 但我在某处读过这有效)。
实现这一目标的最佳方法是什么?
我使用示例代码中的以下两种方法:
-(void)setupPeerAndSessionWithDisplayName:(NSString *)displayName{
_peerID = [[MCPeerID alloc] initWithDisplayName:displayName];
_session = [[MCSession alloc] initWithPeer:_peerID];
_session.delegate = self;
}
-(void)setupMCBrowser{
_browser = [[MCBrowserViewController alloc] initWithServiceType:@"chat-files" session:_session];
}
-(void)advertiseSelf:(BOOL)shouldAdvertise{
if (shouldAdvertise) {
_advertiser = [[MCAdvertiserAssistant alloc] initWithServiceType:@"chat-files"
discoveryInfo:nil
session:_session];
[_advertiser start];
}
else{
[_advertiser stop];
_advertiser = nil;
}
}
当我在前台运行该应用程序时,它可以完美地找到其他设备并进行连接。但是,如果我将其中一个应用程序置于后台,则后台应用程序设备将不再可见。
我已经读过这个:Can I have a multipeer connectivity session run in the background? - 但我不敢相信如果没有任何解决方法这是不可能的。
仅供引用,我的应用程序也在使用后台位置更新,如果相关的话......
谢谢!
编辑: 还有其他方法可以做到这一点吗?基本上我只想向另一台设备(向在后台运行应用程序的设备)发送一条消息 - 由于多点连接在后台不起作用,我可以直接通过蓝牙进行连接吗?
最佳答案
Apple 已确认“多点连接无法在后台运行”。原因是,如果您的应用程序被挂起,那么它的套接字资源可以被回收,一切都会崩溃。
但是,可以在后台监视 iBeacons。本质上,您将应用程序设置为监视与具有特定 ID 的信标的接近程度。然后,位于前台的应用程序将成为具有相同 ID 的信标。这会导致在后台调用应用程序的以下应用程序委托(delegate)方法:
- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region {
UILocalNotification *notification = [[UILocalNotification alloc] init];
if(state == CLRegionStateInside) {
notification.alertBody = NSLocalizedString(@"A nearby app would like to connect", @"");
} else {
return;
}
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
您可以在此处发出本地通知,当用户点击该通知时,您的应用程序将进入前台,此时您可以开始为点对点连接转换广告。
关于ios - 与在后台运行应用程序的一台设备进行多点连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25514737/