iOS后台保活

问题描述:

app需要在收到MQTT消息的时候震动30秒和通知栏展示本地通知,一分钟之后移除通知栏消息,这必然涉及到倒计时,出现的问题是当app在后台的时候会出现会出现30秒左右的保活,定时器不走了。。。。。

官网链接:

https://developer.apple.com/documentation/uikit/uiapplication/1623031-beginbackgroundtaskwithexpiratio/

https://developer.apple.com/documentation/backgroundtasks/choosing_background_strategies_for_your_app?language=objc

解决办法:

应用在后台时可以播放声音信息。
可以利用此模式播放无声音乐,App 进入后台后,播放无声音乐,配合beginBackgroundTaskWithName对系统申请后台使用时间,可以使APP在后台长时间保活。

应用提供位置信息 应用场景:在后台时需要不断通知用户位置更新信息。
通过后台持续定位App,可以实现App后台保活。

以下Demo可以实现解决这个问题,但是发现在iOS15以上的系统还是存在问题。

https://github.com/QiShare/QiAppRunInBackground
https://gitee.com/msmasker/back-runing-demo

解决iOS15以上系统该问题:

https://www.jianshu.com/p/4c02230677f3

以上是Swift写的,改写OC如下:
//
//  XTBackRunningManager.m
//  XTBackRunningDemo
//
//  Created by mshi on 2022/1/20.
//

#import "C2AppBackRunningManager.h"
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface C2AppBackRunningManager ()
@property (nonatomic, strong) AVAudioPlayer *audioPlayer;
@property (nonatomic, strong) AVAudioEngine *audioEngine;
@property (nonatomic,assign) UIBackgroundTaskIdentifier backgroundTaskIdentifier;
@property (nonatomic, strong) NSTimer *applyTimer;
@property (nonatomic, strong) NSTimer *taskTimer;

@end

@implementation C2AppBackRunningManager
+ (instancetype)shareManager {
    static C2AppBackRunningManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[C2AppBackRunningManager alloc] init];
    });
    return manager;
}

//- (instancetype)init {
//    self = [super init];
//    if (self) {
////        [self addNoti];
//        // 获取定位权限
//        [self.locationManager requestAlwaysAuthorization];
//        [self.locationManager requestWhenInUseAuthorization];
//    }
//    return self;
//}

- (void)startBackgroundTask: (UIApplication *)app {
    self.backgroundTaskIdentifier = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:self.backgroundTaskIdentifier];
        self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
        [self applyForMoreTime];
    }];
    [self.applyTimer invalidate];
    self.applyTimer = nil;
    [self.taskTimer invalidate];
    self.taskTimer = nil;
    self.taskTimer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector: @selector(doSomething) userInfo:nil repeats:YES];
}

- (void)stopBackgroundTask {
    [self.applyTimer invalidate];
    self.applyTimer = nil;
    [self.taskTimer invalidate];
    self.taskTimer = nil;
}

- (void)doSomething {
    DLog(@"doing some thing: %f", [UIApplication sharedApplication].backgroundTimeRemaining);
}

- (void)applyForMoreTime {
    if ([UIApplication sharedApplication].backgroundTimeRemaining < 30) {
        self.backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
            [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskIdentifier];
            self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
            [self applyForMoreTime];
        }];
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Silence" ofType:@"wav"];
        NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
        if (!fileURL) {
            NSLog(@"playEmptyAudio 找不到播放文件");
        }
        NSError *error = nil;
        
        if (@available(iOS 11.0, *)) {
            [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback mode:AVAudioSessionModeDefault routeSharingPolicy:AVAudioSessionRouteSharingPolicyDefault options:AVAudioSessionCategoryOptionMixWithOthers error:&error];
        } else {
            // Fallback on earlier versions
            [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback mode:AVAudioSessionModeDefault options:AVAudioSessionCategoryOptionMixWithOthers error:&error];
        }
        // 0.0~1.0,默认为1.0
        self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
        [self.audioEngine reset];
        [self.audioPlayer play];
        [self.audioPlayer stop];
    }
}

@end

参考链接:

https://www.jianshu.com/p/311d49195d86

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1. iOS后台运行 iOS后台运行分为三种 后台任务App在进入后台后还有任务没执行完,还需要运行一小段时间,那...
    maskerII阅读 14,461评论 5 15
  • 核心代码仅需要4句,来实现ios后台保活。直接上代码 项目设置中,开启后台服务这里一定要勾选,不然,根本不会进入a...
    gerrywanggang阅读 12,328评论 9 11
  • 首先Apple官方为我们准备了三类后台任务执行的场景: 1.Background Tasks:APP 在前台时启动...
    糊糊涂涂888阅读 11,313评论 2 13
  • iOS有两种后台运行保活方式,第一种叫无声音乐保活(即在后台开启音频播放,只不过不需要播放出音量且不能影响其他音乐...
    Joh蜗牛阅读 10,437评论 6 8
  • 前言 去年底我在公司开始接手几个迭代了五六年的iOS老项目的技术优化工作。互联网公司的闭源N手业务老代码,经过了若...
    njuxjy阅读 5,702评论 3 7

友情链接更多精彩内容