2

So I'm creating an iOS app, and I'm ranging for beacons in the background. It works well once my iPhone is awake and it continues to work even when the iPhone is locked...however the iPhone must still be awake. Once the iPhone goes to sleep my app ranges about 10 more times and then stops. If you wake the iPhone up it starts ranging again.

I've tried monitoring as well but no luck.

Can anyone tell me if this if even possible? I've searched everywhere and can't find an answer! Please find my beacon methods (which are in the AppDelegate) below

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];

    self.locationManager = [[CLLocationManager alloc] init];

    if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        [self.locationManager requestAlwaysAuthorization];
    }

    self.locationManager.delegate = self;
    self.locationManager.pausesLocationUpdatesAutomatically = NO;

    [self.locationManager startUpdatingLocation];
    return YES;
}

-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
    if(beacons.count > 0) {
        CLBeacon *nearestBeacon = beacons.firstObject;

        if (nearestBeacon.proximity == CLProximityImmediate || nearestBeacon.proximity == CLProximityNear) {
            NSLog("Beacon detected");
        }
    }
}

- (void)startRangingBeacons {
    self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:@"EBEFD083-70A2-47C8-9837-E7B5634DF525" identifier:@"receptionBeacon"];
    [self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
}

Any help is appreciated Thanks Sonia

4

1 回答 1

1

有一种方法可以唤醒您的手机。如果您的应用程序属于信标区域,那么,

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region

上面的方法被自动调用。因此,您需要放置一些本地通知,例如“您靠近信标区”来唤醒手机。但你必须重新进入该地区。

一旦您的应用进入该区域,请编写以下代码以重新启动信标进程,

[self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
UIBackgroundTaskIdentifier bgTask = [[UIApplication  sharedApplication] 
                                      beginBackgroundTaskWithExpirationHandler:^{
      NSLog(@"End of tolerate time. Application should be suspended now if we do not ask more 'tolerance'");
     [[UIApplication sharedApplication] endBackgroundTask:UIBackgroundTaskInvalid];
}];

if (bgTask == UIBackgroundTaskInvalid)
{
     NSLog(@"This application does not support background mode");
} else {
     //if application supports background mode, we'll see this log.
     NSLog(@"Application will continue to run in background");
}

希望这对你有用。

于 2015-09-03T13:43:38.857 回答