iOS配景保活

源码 2024-9-22 07:19:51 18 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
您需要登录后才可以回帖 登录 | 立即注册

Powered by CangBaoKu v1.0 小黑屋藏宝库It社区( 冀ICP备14008649号 )

GMT+8, 2024-11-22 00:25, Processed in 0.132677 second(s), 32 queries.© 2003-2025 cbk Team.

快速回复 返回顶部 返回列表