后台定位更新

来源:互联网 发布:渡边麻友ins事件 知乎 编辑:程序博客网 时间:2024/06/06 08:45

关于定位

官方推荐的有三种
  • The significant-change location service (Recommended)
  • Foreground-only location services
  • Background location services

一般就前两种混合用的, 在 - (void)applicationDidEnterBackground:(UIApplication *)application 里面作出判断(把gps的关掉, 打开信号定位), 前提是你需要程序在后台的时候也定位的话

如何能够在后台间隔获取信息并不显示定位图标

因为apple一般情况下只允许程序在后台最多运行600秒, 所以, 我目前能想到的解决方案就是:

在plist里面设置允许后台定位

An app that provides continuous location updates to the user (even when in the background) can enable background location services by including the UIBackgroundModes key (with the location value) in its Info.plist file.

在applicationDidEnterBackground中设置后台需要运行的代码

- (void)applicationDidEnterBackground:(UIApplication *)application{    self.inBackground = YES;    [self.locationManager stopUpdatingLocation];    UIBackgroundTaskIdentifier __block bgTask;    bgTask = [application beginBackgroundTaskWithExpirationHandler:^{        // Clean up any unfinished task business by marking where you.        // stopped or ending the task outright.        [application endBackgroundTask:bgTask];        bgTask = UIBackgroundTaskInvalid;    }];        // Start the long-running task and return immediately.    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{        while (self.inBackground) {            [self startLocationServices];            [NSThread sleepForTimeInterval:(550)];        }                [application endBackgroundTask:bgTask];        bgTask = UIBackgroundTaskInvalid;    });}

在这里开关定位, 不然的话会一直显示图标..

- (void)startLocationServices{    [self.locationManager startUpdatingLocation];    DLOG(@"timer start location service here");    DLOG(@"%f",[UIApplication sharedApplication].backgroundTimeRemaining);}

结束语

我怎么感觉每次我要做一个功能的实现方式都这么蛋疼…

不知道这么做能不能通过审核, 目前来说我还是倾向于用苹果推荐的后台信号监控定位的方式..

0 0
原创粉丝点击