我试图触发一个基于iBeacons的事件
当应用程序运行在前台、背景而不是挂起时,它工作得很好(屏幕用电源按钮关闭)。
我可以在锁定屏幕上看到NSLog消息,但当设备屏幕关闭时就看不到了。
有办法这样做吗?
附件代表:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog(@"applicationDidFinishLaunching");
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
[_locationManager requestAlwaysAuthorization];
CLBeaconRegion *region;
region = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:@"EBEFD083-70A2-47C8-9837-E7B5634DF524"] major: 9 minor: 103 identifier: @"region1"];
region.notifyEntryStateOnDisplay = YES;
region.notifyOnEntry = YES;
region.notifyOnExit = YES;
[_locationManager startMonitoringForRegion:region];
[_locationManager startRangingBeaconsInRegion:region];
return YES;
}
- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
if(state == CLRegionStateInside) {
NSLog(@"locationManager didDetermineState INSIDE for %@", region.identifier);
}
else if(state == CLRegionStateOutside) {
NSLog(@"locationManager didDetermineState OUTSIDE for %@", region.identifier);
}
else {
NSLog(@"locationManager didDetermineState OTHER for %@", region.identifier);
}
}
- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region
{
if ( beacons.count > 0 )
{
NSLog(@"locationManager didRangeBeacons: %@",beacons.description);
}
}Info.plist (仅适用于相关部分):
<key>NSLocationAlwaysUsageDescription</key>
<string>app location requested</string>
<key>UIBackgroundModes</key>
<array>
<string>location</string>
<string>voip</string>
<string>bluetooth-peripheral</string>
<string>bluetooth-central</string>
<string>external-accessory</string>
</array>发布于 2015-05-07 12:32:02
将pausesLocationUpdatesAutomatically属性LocationManager设置为" NO ",此属性设置为无位置服务永远不会关闭。但是您必须小心,因为将此属性设置为NO会显著增加设备的功耗。
发布于 2015-05-07 13:12:47
信标监视(didEnterRegion:和didExitRegion:)工作在后台,而信标测距只在应用程序位于前台时才能工作,并且在后台有有限的时间。这些背景限制包括应用程序因事件而被唤醒后的5秒钟(比如由于设置region.notifyEntryStateOnDisplay = YES;而启动的锁定屏幕)。
有一些技巧,你可以做,以获得额外的背景测距时间。请在此阅读:
http://developer.radiusnetworks.com/2014/11/13/extending-background-ranging-on-ios.html
https://stackoverflow.com/questions/30101057
复制相似问题