iOS后台模式借助位置更新实现

2,762 阅读7分钟

需求:iOS系统下使我们的app在后台下(点击Home键进入后台)仍能继续运行任务.


阅读前提:

  • 了解后台任务机制
  • 了解获取位置基本原理

GitHub地址(附代码) : iOS后台模式借助位置更新实现

简书地址 : iOS后台模式借助位置更新实现

博客地址 : iOS后台模式借助位置更新实现

掘金地址 : iOS后台模式借助位置更新实现


原理

iOS下默认app中所有线程在进入后台后(点击Home键或上滑退出)所有线程处于挂起状态,即不支持后台运行程序,当再次点击进入app后,所有线程恢复运行,因此,如果要实现后台模式,即所有线程在进入后台后仍处于活跃状态。

苹果官方提供了打开后台模式开关的操作,但是需要说明使用后台模式的场景,如下项目配置图1中所示,但我们不能平白无故使用例如后台播放音乐,VOIP等等功能(否则会上架被拒),综合考虑,我们可以选用后台实时更新位置以实现支持后台模式,因为位置可以作为app的数据分析统计等等。

流程

  • 实现获取地理位置信息(必须给予app始终允许的位置权限)
  • 开启后台模式-更新地理位置信息
  • 进入后台后轮循开始后台任务(1个后台任务有效时间为3分钟)

项目配置

实现后台模式需要借助例如位置更新,后台播放音乐,VOIP等等功能以实现后台app处于活跃状态

  • 开启后台模式:Project -> Capabilities -> Background Modes -> Location updates

    Background_Mode

  • plist文件中添加位置权限

	<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
	<string>Give me location authority</string>
	<key>NSLocationWhenInUseUsageDescription</key>
	<string>Give me location authority</string>

location_authority

  • 导入静态库CoreLocation.framework, 然后在需要的文件中导入头文件#import <CoreLocation/CoreLocation.h>与#import <CoreLocation/CLLocation.h>
  • 在启动app时给予始终允许的权限(否则无法支持后台模式)

Note : 因为我们是通过后台任务来实时获取地理位置以实现后台模式,所以如果不给予app始终允许的位置权限则无法实时在后台获取地理位置,也无法实现我们的需求。

实现

1.实现获取地理位置(CoreLocation)

1.1 初始化并配置locationManager对象

遵循CLLocationManagerDelegate协议,按照如下设置完成后即可在回调函数中获取经纬度等位置信息

@property (nonatomic, strong) CLLocationManager   *locationManager;

    _locationManager = [[CLLocationManager alloc] init];
    _locationManager.delegate                           = self;
    _locationManager.desiredAccuracy                    = kCLLocationAccuracyBestForNavigation;
    _locationManager.pausesLocationUpdatesAutomatically = NO;
    self.locationManager.distanceFilter                 = kCLDistanceFilterNone; // 不移动也可以后台刷新回调
    
    if([[UIDevice currentDevice].systemVersion floatValue]>= 8.0) {
        [self.locationManager requestAlwaysAuthorization];
    }
    
    [self.locationManager startUpdatingLocation];

1.2 回调函数中获取地理位置信息

如果开启后台模式,我们需要使用_isCollectLocation标记当前是否正在定位,如果当前停止定位我们则需要重启定位功能,我们在这里设置120秒后开启一个后台任务并在10秒后停止一个后台任务,因为一个后台任务的有效时间为3分钟,我们就让前一个后台任务执行到2分钟左右时停止掉(以防时间控制本身不精确所以设置2分钟),然后开启一个新的后台任务,不断循环改过程,即可实现后台模式。

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    CLLocation *location = [locations lastObject];    //当前位置信息
    
    self.longitude = location.coordinate.longitude;
    self.latitude  = location.coordinate.latitude;
    
    if (self.isSupportBGMode) {
        //如果正在10秒定时收集的时间,不需要执行延时开启和关闭定位
        if (_isCollectLocation) {
            return;
        }
        [self performSelector:@selector(restartLocation) withObject:nil afterDelay:120];
        [self performSelector:@selector(stopLocation)    withObject:nil afterDelay:10];
        _isCollectLocation = YES;//标记正在定位
    }
}

-(void)restartLocation {
    self.locationManager.delegate       = self;
    self.locationManager.distanceFilter = kCLDistanceFilterNone; // 不移动也可以后台刷新回调
    if ([[UIDevice currentDevice].systemVersion floatValue]>= 8.0) {
        [self.locationManager requestAlwaysAuthorization];
    }
    [self.locationManager startUpdatingLocation];
    [self.bgModeManager beginNewBackgroundTask];
}

-(void)stopLocation {
    log4cplus_debug("XDXBGModeManager", "%s - Stop Background Mode Location service !",ModuleName);
    _isCollectLocation = NO;
    [self.locationManager stopUpdatingLocation];
}

1.3 开启与关闭后台模式

停止后台模式即停止位置更新,delegate也失效同时停止当前所有的后台任务,停止监听app进入后台的通知 开启后台模式即重新设置代理并开始位置更新,同时注册用户进入后台的通知以便在用户进入后台后开启轮循的后台任务

- (void)openBGMode {
    self.isSupportBGMode = YES;
    // Note : You need to open background mode in the project setting, Otherwise the app will crash.
    _locationManager.allowsBackgroundLocationUpdates = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
}

- (void)closeBGMode {
    self.isSupportBGMode = NO;
    [self.bgModeManager endAllBGTask];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
}

-(void)applicationEnterBackground {
    [self startBGLocationService];
    [self.bgModeManager beginNewBackgroundTask];
}

- (void)startBGLocationService {
    if ([CLLocationManager locationServicesEnabled] == NO) {
        log4cplus_error("XDXBGModeManager", "%s - You currently have all location services for this device disabled", ModuleName);
    }else {
        CLAuthorizationStatus authorizationStatus = [CLLocationManager authorizationStatus];
        
        if(authorizationStatus == kCLAuthorizationStatusDenied || authorizationStatus == kCLAuthorizationStatusRestricted) {
            log4cplus_error("XDXBGModeManager", "%s - AuthorizationStatus failed",ModuleName);
        }else {
            log4cplus_info("XDXBGModeManager", "%s - Start Background Mode Location service !",ModuleName);
            self.locationManager.distanceFilter = kCLDistanceFilterNone;
            
            if([[UIDevice currentDevice].systemVersion floatValue]>= 8.0) {
                [self.locationManager requestAlwaysAuthorization];
            }
            [self.locationManager startUpdatingLocation];
        }
    }
}

1.4 处理获取地理位置失败的情况

当用户拒绝提供位置权限或当前网络故障时,应该给予用户提示

- (void)locationManager: (CLLocationManager *)manager didFailWithError: (NSError *)error {
    log4cplus_error("XDXBGModeManager", "%s - locationManager error:%s",ModuleName, [NSString stringWithFormat:@"%@",error].UTF8String);
    
    self.latitude  = 0;
    self.longitude = 0;
    
    switch([error code])
    {
        case kCLErrorNetwork:
            log4cplus_error("XDXBGModeManager", "%s - %s : Please check the network connection !",ModuleName, __func__);
            
            static dispatch_once_t onceToken;
            dispatch_once(&onceToken, ^{
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Location Warning" message:
                                            @"Please check the network connection" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"OK" style:
                                               UIAlertActionStyleCancel handler:nil];
                [alert addAction:cancelAction];
                UIViewController *vc = [UIApplication sharedApplication].windows[0].rootViewController;
                [vc presentViewController:alert animated:YES completion:nil];
            });
            
            break;
        case kCLErrorDenied:
        {
            log4cplus_error("XDXBGModeManager", "%s - %s : Please open location authority on the setting if you want to use our service!",ModuleName, __func__);
            
            static dispatch_once_t onceToken2;
            dispatch_once(&onceToken2, ^{
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Location Warning" message:
                                            @"Please allow our app access the location always on the setting->Privacy->Location Services !" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"OK" style:
                                               UIAlertActionStyleCancel handler:nil];
                [alert addAction:cancelAction];
                UIViewController *vc = [UIApplication sharedApplication].windows[0].rootViewController;
                [vc presentViewController:alert animated:YES completion:nil];
            });
            
        }
            break;
        default:
            break;
    }
}

2.实现后台任务的轮循

2.1 基本配置

使用bgTaskIdList记录当前所有后台任务的列表,使用masterTaskId记录当前正在执行的后台任务

@property (nonatomic, strong)   NSMutableArray             *bgTaskIdList;
@property (assign)              UIBackgroundTaskIdentifier masterTaskId;

2.2 开启后台任务

beginBackgroundTaskWithExpirationHandler调用此方法可实现开启一个后台任务,我们需要将此后台任务放入我们记录的数组中并将其设置为masterTaskId,当然,如果上次记录的后台记录已经失效,就记录新任务为主任务,如果上次开启的后台任务还没结束,就提前关闭,使用最新的后台任务

-(void)restartLocation {
    [self beginNewBackgroundTask];
}

-(UIBackgroundTaskIdentifier)beginNewBackgroundTask {
    UIApplication *application = [UIApplication sharedApplication];
    __block UIBackgroundTaskIdentifier bgTaskId = UIBackgroundTaskInvalid;
    if([application respondsToSelector:@selector(beginBackgroundTaskWithExpirationHandler:)]) {
        bgTaskId = [application beginBackgroundTaskWithExpirationHandler:^{
            log4cplus_warn("XDXBGModeManager", "%s - bg Task (%lu) expired !",ModuleName,bgTaskId);
            [self.bgTaskIdList removeObject:@(bgTaskId)];//过期任务从后台数组删除
            bgTaskId = UIBackgroundTaskInvalid;
            [application endBackgroundTask:bgTaskId];
        }];
    }
    //如果上次记录的后台任务已经失效了,就记录最新的任务为主任务
    if (_masterTaskId == UIBackgroundTaskInvalid) {
        self.masterTaskId = bgTaskId;
        log4cplus_warn("XDXBGModeManager", "%s - Start bg task : %lu",ModuleName,(unsigned long)bgTaskId);
    }else { //如果上次开启的后台任务还未结束,就提前关闭了,使用最新的后台任务
        //add this id to our list
        log4cplus_warn("XDXBGModeManager", "%s - Keep bg task %lu",ModuleName,(unsigned long)bgTaskId);
        [self.bgTaskIdList addObject:@(bgTaskId)];
        [self endInvalidBGTaskWithIsEndAll:NO];//留下最新创建的后台任务
    }
    return bgTaskId;
}

2.3 结束后台任务

如果仅仅结束其他后台任务,只保留当前主任务即将数组中除了最后一个元素外都删除,并通过遍历结束所有无效后台任务

- (void)endAllBGTask {
    [self endInvalidBGTaskWithIsEndAll:YES];
}

-(void)endInvalidBGTaskWithIsEndAll:(BOOL)isEndAll
{
    UIApplication *application = [UIApplication sharedApplication];
    //如果为all 清空后台任务数组
    //不为all 留下数组最后一个后台任务,也就是最新开启的任务
    if ([application respondsToSelector:@selector(endBackGroundTask:)]) {
        for (int i = 0; i < (isEndAll ? _bgTaskIdList.count :_bgTaskIdList.count -1); i++) {
            UIBackgroundTaskIdentifier bgTaskId = [self.bgTaskIdList[0]integerValue];
            log4cplus_debug("XDXBGModeManager", "%s - Close bg task %lu",ModuleName,(unsigned long)bgTaskId);
            [application endBackgroundTask:bgTaskId];
            [self.bgTaskIdList removeObjectAtIndex:0];
        }
    }
    
    ///如果数组大于0 所有剩下最后一个后台任务正在跑
    if(self.bgTaskIdList.count > 0) {
        log4cplus_debug("XDXBGModeManager", "%s - The bg task is running %lu!",ModuleName,(long)[_bgTaskIdList[0]integerValue]);
    }
    
    if(isEndAll) {
        [application endBackgroundTask:self.masterTaskId];
        self.masterTaskId = UIBackgroundTaskInvalid;
    }else {
        log4cplus_debug("XDXBGModeManager", "%s - Kept master background task id : %lu",ModuleName,(unsigned long)self.masterTaskId);
    }
}

注意

后台模式在特定情况下会app被系统自动杀死 例如:息屏后放置较长时间或app占用大量CPU资源

结果

运行Demo程序,打开后台开关,进入后台观察控制台仍有打印,证明后台模式启用成功