屏幕关闭时iBeacon事件

时间:2015-05-07 12:26:03

标签: ios objective-c core-location ibeacon

我正在尝试触发基于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>

2 个答案:

答案 0 :(得分:3)

LocationManager的 pausesLocationUpdatesAutomatically 属性设置为“NO”,将此属性设置为NO位置服务永远不会关闭。但是你必须要小心,因为将此属性设置为NO会显着增加设备的功耗。

答案 1 :(得分:1)

虽然信标监控(var string = "12" var intVersion = string.toInt() let intMultipied = intVersion! * 2 label.text= "\(intMultipied)" didEnterRegion:)在后台运行,但信标范围(didExitRegion:)仅在应用处于前台时有效,并在后台有限的时间内。这些背景限制包括应用因事件而被唤醒到后台后五秒钟(例如由于您的设置导致锁定屏幕didRangeBeacons:inRegion:

你可以采取一些技巧来获得额外的背景测距时间。在这里阅读:

http://developer.radiusnetworks.com/2014/11/13/extending-background-ranging-on-ios.html