美文网首页
iOS 后台持续定位

iOS 后台持续定位

作者: 赵哥窟 | 来源:发表于2018-11-30 09:24 被阅读199次

    在日常的工作开发中,有时会遇到需要在后台持续运行的需求。对于这个需求,安卓实现起来比较简单,而iOS来说就比较复杂了。我们公司就有后台持续定位并且上传上传地理坐标的需求, 现在总结一下

    实现方式一

    因为项目用的百度定位SDK,而百度定位就自带后台定位功能所以可以直接用。

    这里说明一下为什么要用百度SDK,因为项目要用定位获取的经纬度、城市信息请接口,而CLLocationManager返回的经纬度和百度是有差别的需要转换,有可能同一位置百度和苹果定位返回的定位信息有差别。为了统一iOS和Android都统一使用百度。

    百度SDK后台定位

    1.引入头文件

    在调用定位功能的类中引入 BMKLocationComponent.h 这个头文件。

    #import <BMKLocationkit/BMKLocationComponent.h>
    
    2.配置AK

    在调用定位时,需要添加AK,需要注意的是请在 SDK 任何类的初始化以及方法调用之前设置正确的 AK。设置AK的方式如下:

    [[BMKLocationAuth sharedInstance] checkPermisionWithKey:@"输入AK" authDelegate:self];
    
    3.设置期望定位精度

    由于苹果系统的首次定位结果为粗定位,其可能无法满足需要高精度定位的场景。
    百度提供了 kCLLocationAccuracyBest 参数,设置该参数可以获取到精度在10m左右的定位结果,但是相应的需要付出比较长的时间(10s左右),越高的精度需要持续定位时间越长。

    推荐使用kCLLocationAccuracyHundredMeters,一次还不错的定位,偏差在百米左右,超时时间设置在2s-3s左右即可。

    //初始化实例
    _locationManager = [[BMKLocationManager alloc] init];
    //设置delegate
    _locationManager.delegate = self;
    //设置返回位置的坐标系类型
    _locationManager.coordinateType = BMKLocationCoordinateTypeBMK09LL;
    //设置距离过滤参数
    _locationManager.distanceFilter = kCLDistanceFilterNone;
    //设置预期精度参数
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    //设置应用位置类型
    _locationManager.activityType = CLActivityTypeAutomotiveNavigation;
    //设置是否自动停止位置更新
    _locationManager.pausesLocationUpdatesAutomatically = NO;
    //设置是否允许后台定位
    _locationManager.allowsBackgroundLocationUpdates = YES;
    //设置位置获取超时时间
    _locationManager.locationTimeout = 10;
    //设置获取地址信息超时时间
    _locationManager.reGeocodeTimeout = 10;
    

    更多详细介绍及其余精度阈值说明,请查看参考手册。

    4.开启持续定位

    调用BMKLocationManager提供的startUpdatingLocation方法实现。代码如下:

    [self.locationManager startUpdatingLocation];
    

    如果需要持续定位返回地址信息(需要联网),请设置如下:

    [self.locationManager setLocatingWithReGeocode:YES];
    [self.locationManager startUpdatingLocation];
    
    5.接收位置更新

    实现BMKLocationManagerDelegate代理的BMKLocationManager: didUpdateLocation: orError:方法,处理位置更新。代码如下:

    - (void)BMKLocationManager:(BMKLocationManager * _Nonnull)manager didUpdateLocation:(BMKLocation * _Nullable)location orError:(NSError * _Nullable)error
    
    {
        if (error)
        {
            NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);  
        } if (location) {//得到定位信息,添加annotation
            if (location.location) {
                NSLog(@"LOC = %@",location.location);
            }
            if (location.rgcData) {
                NSLog(@"rgc = %@",[location.rgcData description]);
            }   
        }
    }
    
    6.停止持续定位

    当不再需要定位时,调用BMKLocationManager提供的stopUpdatingLocation方法停止定位。代码如下:

    [self.locationManager stopUpdatingLocation];
    
    7.更改info.plist

    将info.plist的字段改成NSLocationWhenInUseUsageDescription,NSLocationAlwaysUsageDescription,NSLocationAlwaysAndWhenInUseUsageDescription三项。

    在左侧目录中选中工程名,开启 TARGETS->Capabilities->Background Modes
    在Background Modes中勾选 Location updates,如下图所示:


    屏幕快照 2018-11-30 09.21.59.png
    实现方式二

    使用CLLocationManager 定位

    - (void)applicationDidEnterBackground:(UIApplication *)application {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
        [self keepBackgroundTask];
    }
    
    - (void)keepBackgroundTask
    {
         [self startBgTask];
        
        if (!self.locationManager) {
            self.locationManager = [[CLLocationManager alloc] init];
            self.locationManager.delegate = self;
            [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
            [self.locationManager requestAlwaysAuthorization];
            self.locationManager.allowsBackgroundLocationUpdates = YES;
            self.locationManager.pausesLocationUpdatesAutomatically = NO;
        }
        
         [self.locationManager startUpdatingLocation];  
    }
    
    - (void)startBgTask
    {
        UIApplication *application = [UIApplication sharedApplication];
        __block UIBackgroundTaskIdentifier bgTask;
        bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
            
        }];
        
    }
    
    屏幕快照 2018-11-30 09.21.59.png

    开启后台定位审核被拒参考:
    IOS开启后台定位之审核被拒

    相关文章

      网友评论

          本文标题:iOS 后台持续定位

          本文链接:https://www.haomeiwen.com/subject/wrjccqtx.html