【问题标题】:CLRegion background monitoring not occuringCLRegion 后台监控未发生
【发布时间】:2023-03-14 10:01:01
【问题描述】:

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

AppDelegate

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

故事控制器

+ (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 公里范围且应用程序处于后台时,我没有收到任何通知。

我该如何进行这项工作?当我注销我的监控区域时,我确实看到 lat & lng 是正确的。

【问题讨论】:

    标签: ios core-location clregion region-monitoring


    【解决方案1】:

    两件事你需要确保你正在做。

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2017-11-27
      • 2020-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-10
      • 2016-02-21
      • 2014-07-16
      • 1970-01-01
      相关资源
      最近更新 更多