首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未发生CLRegion后台监控

未发生CLRegion后台监控
EN

Stack Overflow用户
提问于 2016-01-20 03:03:11
回答 1查看 252关注 0票数 0

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

AppDelegate

代码语言:javascript
复制
- (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];
        }
    }
}

StoryController

代码语言:javascript
复制
+ (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);

稍后,我将使用以下内容监控该地区:

代码语言:javascript
复制
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是正确的。

EN

回答 1

Stack Overflow用户

发布于 2016-11-01 21:51:59

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

  1. 检查区域的可用性确保您的locationManager.authorizationStatus == monitoring
  2. make

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34884933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档