在 iPhone 锁定和休眠时测距和监控信标

Ranging and Monitoring Beacons while iPhone is locked and sleeping

所以我正在创建一个 iOS 应用程序,我正在后台搜索信标。它在我的 iPhone 唤醒后运行良好,即使 iPhone 被锁定,它也能继续工作...但是 iPhone 必须仍处于唤醒状态。一旦 iPhone 进入睡眠状态,我的应用程序会再运行大约 10 次,然后停止。如果您唤醒 iPhone,它会再次开始测距。

我也尝试过监控,但没有成功。

谁能告诉我这是否可能?我到处搜索,找不到答案!请在下面找到我的信标方法(在 AppDelegate 中)

 - (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];
}

感谢任何帮助 谢谢 索尼娅

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

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

以上方法被自动调用。所以你需要放一些像"You are near to beacon zone"这样的本地通知来唤醒phone。 但是你必须重新进入该区域。

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

[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");
}

希望这对你有用。