ios - CLRegion 后台监控未发生

标签 ios core-location clregion region-monitoring

在我的项目设置 > 目标 > 功能中,我打开了后台模式并选中了“位置更新”。

enter image description here

enter image description here

应用委托(delegate)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.storyController = [StoryController sharedController];
    self.storyController.locationManager.delegate = self;

 ... etc initializing VC ...

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
    NSLog(@"entered region, %@", region.identifier);
    [self handleRegionEvent:region];
}

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
    NSLog(@"exited region, %@", region.identifier);
    [self handleRegionEvent:region];
}

- (void)handleRegionEvent:(CLRegion *)region {
    NSLog(@"handle region");
    if ([UIApplication sharedApplication].applicationState ==
        UIApplicationStateActive) {
        NSLog(@"handle region local");
        UILocalNotification *n = [[UILocalNotification alloc] init];
        n.alertBody = @"LOCAL";
        n.soundName = @"Default"; //Coffee Man?
        [[UIApplication sharedApplication] presentLocalNotificationNow:n];
    } else {
        if ([[StoryController sharedController] readyForNextChapter]) {
            UILocalNotification *n = [[UILocalNotification alloc] init];
            n.alertBody = @"New ☕ Story";
            n.soundName = @"Default"; //Coffee Man?
            [[UIApplication sharedApplication] presentLocalNotificationNow:n];
        }
    }
}

故事 Controller

+ (id)sharedController {
    static StoryController *myController = nil;
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        myController = [[StoryController alloc] init];
    });

    return myController;
}

- (id)init {
    self = [super init];
    self.locationManager = [[CLLocationManager alloc] init];
    NSLog(@"monitored regions: %@", self.locationManager.monitoredRegions);

稍后我将监视该区域:

CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:g.coordinate radius:1000 identifier:"My Store"];
region.notifyOnEntry = true;
region.notifyOnExit = true;

NSLog(@"%d", [CLLocationManager authorizationStatus]);
[[[StoryController sharedController] locationManager] startMonitoringForRegion:region];

当我在后台运行应用程序时离开或进入 1 公里范围时,我没有收到任何通知。

我如何使它起作用?当我注销我的监控区域时,我确实看到经纬度是正确的。

最佳答案

您需要确保自己在做两件事。

  1. 检查区域监控的可用性

  2. 确保您的 locationManager.authorizationStatus == .authorizedAlways

有些系统根本不支持区域监控,除非你的authorizationStatus为.authorizedAlways,否则区域监控永远不会工作,只有这样它才能在后台监控。

https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/LocationAwarenessPG/RegionMonitoring/RegionMonitoring.html

关于ios - CLRegion 后台监控未发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34884933/

相关文章:

iphone - 嵌入 Segue 不向后兼容

ios - 在 iPhone 模拟器上测试 CoreLocation

iOS : alert user for specific Geo location area (lat, 长)

ios - 是否可以在框架内定义 NSLocationWhenInUseUsageDescription ?

ios - CoreLocation 的文档中有 "geoid"信息吗?

ios - 是否可以在后台运行 CLLocationManager

ios - 为什么不调用 deinit?

iOS 将颜色转换为 RGB

ios - Phonegap 和 Apache cordova 之间的不明确区别