【发布时间】:2015-05-07 12:26:03
【问题描述】:
我正在尝试基于 iBeacons 触发事件
当应用程序在前台、后台运行但未挂起(通过电源按钮关闭屏幕)时,它可以正常工作
我可以在锁定屏幕上看到 NSLog 消息,但在设备屏幕关闭时看不到。
有没有办法做到这一点?
AppDelegate.m:
- (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>
【问题讨论】:
-
您可以尝试在您的 plist 文件中添加“隐私 - 位置使用说明”键(如果您尚未添加)。
标签: ios objective-c core-location ibeacon