iOS - 后台保活(后台持续运行代码)

iOS有两种后台运行保活方式,第一种叫无声音乐保活(即在后台开启音频播放,只不过不需要播放出音量且不能影响其他音乐播发软件),第二种叫Background Task,但是这种方法在iOS 13以后只能申请短短的30秒钟时间,但是在iOS7-iOS13以前是可以申请到3分钟的保活时间的,当然我们也可以经过处理来申请到更多的保活时间。

无声音乐保活

1.打开应用的Target页面Signing & Cabailities,添加Capability(Background Modes)勾选Audio,AirPlay,and Picture in Picture选项


201610954086_.pic.jpg

2.在info.plist文件的【Required background modes】中,添加对应的key:【App plays audio or streams audio/video using AirPlay】

181610954014_.pic.jpg

3.在AppDelegate.m中添加监听
UIApplicationWillEnterForegroundNotification(应用进入前台通知)UIApplicationDidEnterBackgroundNotification(应用进入后台通知)

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];

4.编写音乐播放类

.h文件

#import 
#import 

@interface BackgroundPlayer : NSObject 
{
    AVAudioPlayer* _player;
}
- (void)startPlayer;

- (void)stopPlayer;

@end

.m文件

#import "BackgroundPlayer.h"

@implementation BackgroundPlayer

- (void)startPlayer
{
    if (_player && [_player isPlaying]) {
        return;
    }
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [[AVAudioSession sharedInstance] setMode:AVAudioSessionModeDefault error:nil];

    NSString* route = [[[[[AVAudioSession sharedInstance] currentRoute] outputs] objectAtIndex:0] portType];

    if ([route isEqualToString:AVAudioSessionPortHeadphones] || [route isEqualToString:AVAudioSessionPortBluetoothA2DP] || [route isEqualToString:AVAudioSessionPortBluetoothLE] || [route isEqualToString:AVAudioSessionPortBluetoothHFP]) {
        if (@available(iOS 10.0, *)) {
            [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord
                                             withOptions:(AVAudioSessionCategoryOptionMixWithOthers | AVAudioSessionCategoryOptionAllowBluetooth | AVAudioSessionCategoryOptionAllowBluetoothA2DP)
                                                   error:nil];
        } else {
            // Fallback on earlier versions
        }
    }else{
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord
                                         withOptions:(AVAudioSessionCategoryOptionMixWithOthers | AVAudioSessionCategoryOptionDefaultToSpeaker)
                                               error:nil];
    }

    [session setActive:YES error:nil];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"wav"];
    NSURL *url = [NSURL fileURLWithPath:path isDirectory:NO];

    _player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    [_player prepareToPlay];
    [_player setDelegate:self];
    _player.numberOfLoops = -1;
    BOOL ret = [_player play];
    if (!ret) {
        NSLog(@"play failed,please turn on audio background mode");
    }
}

- (void)stopPlayer
{
    if (_player) {
        [_player stop];
        _player = nil;
        AVAudioSession *session = [AVAudioSession sharedInstance];
        [session setActive:NO error:nil];
        NSLog(@"stop in play background success");
    }
}

@end

将使用到的音乐文件放入项目:


WechatIMG17.jpeg

5.在应用进入后台时开启保活
在AppDelegate.m文件中,编写如下代码:

// 语音播报
#import 
#import 

@property (nonatomic, strong) BackgroundPlayer* player;
- (void)appWillEnterForeground {
    if (self.player) {
        [self.player stopPlayBackgroundAlive];
    }
}

- (void)appDidEnterBackground {
    if (_player == nil) {
        _player = [[BackgroundPlayer alloc] init];
    }
    [self.player startPlayer];
}

Background Task保活

1.在info.plist文件的【Required background modes】中,添加对应的key:【App registers for location updates】

191610954027_.pic.jpg

2.同样我们需要监听
UIApplicationWillEnterForegroundNotification(应用进入前台通知)和UIApplicationDidEnterBackgroundNotification(应用进入后台通知)

在AppDelegate.m文件中编写如下代码:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
- (void)appWillEnterForeground {}

- (void)appDidEnterBackground {}

3.使用Background Task申请保活时间,在应用进入后台时开启保活,在应用进入前台时关闭保活:
在AppDelegate.m文件中编写如下代码:

@property (assign, nonatomic) UIBackgroundTaskIdentifier backgroundId;
- (void)appWillEnterForeground {
   [self stopKeepAlive];
}

- (void)appDidEnterBackground {
    _backgroundId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        //申请的时间即将到时回调该方法
        NSLog(@"BackgroundTask time gone");
        [self stopKeepAlive];
    }];
}

- (void)stopKeepAlive{
  if (_backgroundId) {
        [[UIApplication sharedApplication] endBackgroundTask:_backgroundId];
        _backgroundId = UIBackgroundTaskInvalid;
    }
}

4.使用NSTimer循环申请保活时间,但是建议不要无限申请保活时间,因为系统如果发现该应用一直在后台运行时,是可能会直接crash掉你的应用的 ,错误码0x8badf00d.

将需要在后台一直运行的代码,写在AppDelegate.m中运行:
例如:像后台发送手机定位:

// 高德地图
#import 
#import 
//定位
@property (nonatomic,strong) AMapLocationManager *locationManager;
//注册定位信息
@property (nonatomic,strong) NSMutableDictionary * locationDic;

// 定位
    self.locationDic = [@{} mutableCopy];
    [self locationPresent];
    //开启定时器(后台每五分钟上传一次定位)
    [self StartTimer];

// 开启倒计时效果
- (void)StartTimer{
    __block NSInteger time = 10; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(timer,DISPATCH_TIME_NOW,1.0*NSEC_PER_SEC, 0);//每秒执行
    dispatch_source_set_event_handler(timer, ^{
        if(time <= 0){ //倒计时结束,关闭
            dispatch_source_cancel(timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                [self StartTimer];
                //上传定位信息
                [self locationPresent];

            });
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{

            });
            time--;
        }
    });
    dispatch_resume(timer);
}

//定位
-(void)locationPresent{

    self.locationManager = [[AMapLocationManager alloc] init];

    [self.locationManager setDelegate:self];

    @try {
       self.locationManager.allowsBackgroundLocationUpdates = YES;
    } @catch (NSException *exception) {
        NSLog(@"异常:%@", exception);
    } @finally {


    }

    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.locationManager.distanceFilter = kCLDistanceFilterNone;


    // 带逆地理信息的一次定位(返回坐标和地址信息)
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
    // 设置不允许系统暂停定位
    [self.locationManager setPausesLocationUpdatesAutomatically:NO];
    // 定位超时时间,最低5s,此处设置为5s
    self.locationManager.locationTimeout = 5;
    // 逆地理请求超时时间,最低5s,此处设置为5s
    self.locationManager.reGeocodeTimeout = 5;


    [self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error){
        if (error){
            LLLog(@"%@",error);
            NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
            if (error.code == AMapLocationErrorLocateFailed){
                return;
            }
        }

        NSLog(@"location:%@", location);
        if (location) {
            [self.locationDic setValue:@(location.coordinate.latitude) forKey:@"latitude"];
            [self.locationDic setValue:@(location.coordinate.longitude) forKey:@"longitude"];
            // 上传定位信息
            [self UpdateLocationPlace];
        }

    }];
}


//上传当前位置
-(void)UpdateLocationPlace{

    NSString *Url = [NSString stringWithFormat:@"%@%@",BaseUrl,UploadForDriver];
    LLLog(@"UpdateLocationPlace===%@===%@",Url,self.locationDic);
    [SendHttpRequest PostNSMutableDictionary:self.locationDic withNsstring:Url result:^(NSDictionary *result, NSError *error) {
        LLLog(@"%@",result);

    }];
}


转载自:https。//blog.csdn.net/qq_38520096/article/details/102626210

你可能感兴趣的:(iOS - 后台保活(后台持续运行代码))