iOS

iOS后台保活

2022-12-17  本文已影响0人  玉思盈蝶

问题描述:

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

上一篇下一篇

猜你喜欢

热点阅读