后台定位更新

关于定位
官方推荐的有三种
  • 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);
}
结束语

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值