当应用程序没有运行时,我很难让它工作.我已locationManager:didRangeBeacons:inRegion:实现并在应用程序在前台或后台运行时调用它,但是当我退出应用程序并锁定屏幕时它似乎没有做任何事情.位置服务图标消失了,我永远不知道我进入了信标范围.LocalNotification应该仍然有效吗?
我在背景模式(XCode 5)中选择了位置更新和使用蓝牙LE配件我认为我不需要它们.
任何帮助非常感谢.
-(void)watchForEvents { // this is called from application:didFinishLaunchingWithOptions
id class = NSClassFromString(@"CLBeaconRegion");
if (!class) {
return;
}
CLBeaconRegion * rflBeacon = [[CLBeaconRegion alloc] initWithProximityUUID:kBeaconUUID identifier:kBeaconString];
rflBeacon.notifyOnEntry = YES;
rflBeacon.notifyOnExit = NO;
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager startRangingBeaconsInRegion:rflBeacon];
[self.locationManager startMonitoringForRegion:rflBeacon];
}
-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
if (beacons.count == 0 || eventRanged) { // breakpoint set here for testing
return;
}
eventRanged = YES;
if (backgroundMode) { // this is set in the EnterBackground/Foreground delegate calls
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = [NSString stringWithFormat:@"Welcome to the %@ event.",region.identifier];
notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
// normal processing here...
}
Run Code Online (Sandbox Code Playgroud)
dav*_*ung 11
监控可以启动未运行的应用程序.范围不能.
监控启动应用程序的关键是在您的即使在完全重启手机后,这也可以在区域转换时启动您的应用.但有一些警告:CLBeaconRegion:r 上设置这个记录不佳的标志egion.notifyEntryStateOnDisplay = YES;
NSLog语句applicationDidEnterBackground和其他方法以查看发生了什么.)CLBeaconRegion.我看到它需要长达四分钟.就测距而言,即使你无法唤醒你的应用程序,你也可以让你的应用程序同时进行监控和测距.如果监控唤醒您的应用并将其置于后台几秒钟,则会立即启动各种回调.这使您有机会在应用程序仍在运行时执行任何快速测距操作.
编辑:进一步的调查证明,这notifyEntryStateOnDisplay对后台监控没有影响,所以无论你是否有这个标志,上述都应该有效.请参阅此详细说明和您可能遇到的延迟的讨论
通过使用位置更新,iOS 9的代码在后台使用范围信标:
打开项目设置- >功能- >背景模式- >切换Location Updates和Uses Bluetooth LE accessories向ON.
创建CLLocationManager,请求Always监控授权(不要忘记添加Application does not run in background到NO,并NSLocationAlwaysUsageDescription在应用程序的info.plist),并设置以下属性:
locationManager!.delegate = self
locationManager!.pausesLocationUpdatesAutomatically = false
locationManager!.allowsBackgroundLocationUpdates = true
Run Code Online (Sandbox Code Playgroud)为信标和监控区域开始测距:
locationManager!.startMonitoringForRegion(yourBeaconRegion)
locationManager!.startRangingBeaconsInRegion(yourBeaconRegion)
locationManager!.startUpdatingLocation()
// Optionally for notifications
UIApplication.sharedApplication().registerUserNotificationSettings(
UIUserNotificationSettings(forTypes: .Alert, categories: nil))
Run Code Online (Sandbox Code Playgroud)落实CLLocationManagerDelegate并在didEnterRegion同时发送startRangingBeaconsInRegion()和startUpdatingLocation()消息(可选发送通知为好),并设置stopRangingBeaconsInRegion()和stopUpdatingLocation()在didExitRegion
请注意,此解决方案可以使用,但由于电池消耗和客户隐私,Apple不建议使用此解决方案!
更多信息:https://community.estimote.com/hc/en-us/articles/203914068-Is-it-possible-to-use-beacon-ranging-in-the-background-
以下是您需要遵循的背景范围:
CLBeaconRegion始终保持监控,在后台或前台并保持notifyEntryStateOnDisplay = YESnotifyEntryStateOnDisplaylocationManager:didDetermineState:forRegion:在后台调用,所以实现这个委托调用......像这样:
- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region{
if (state == CLRegionStateInside) {
//Start Ranging
[manager startRangingBeaconsInRegion:region];
}
else{
//Stop Ranging
[manager stopRangingBeaconsInRegion:region];
}
}
Run Code Online (Sandbox Code Playgroud)
我希望这有帮助.
如果您只想在进入信标区域时收到通知,您的应用程序当前应该被唤醒。据我所知,唯一的背景限制是在 iOS 设备上实际托管 iBeacon。在这种情况下,应用程序需要在前台物理打开。对于这种情况,您最好直接执行 CoreBluetoothCBPeripheralManager实现。这样你就可以在后台拥有一些广告能力。
| 归档时间: |
|
| 查看次数: |
26058 次 |
| 最近记录: |