热点新闻
iOS后台保活
2023-07-14 06:32  浏览:1100  搜索引擎搜索“手机晒展网”
温馨提示:信息一旦丢失不一定找得到,请务必收藏信息以备急用!本站所有信息均是注册会员发布如遇到侵权请联系文章中的联系方式或客服删除!
联系我时,请说明是在手机晒展网看到的信息,谢谢。
展会发布 展会网站大全 报名观展合作 软文发布

问题描述:

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

发布人:cc85****    IP:101.229.75.***     举报/删稿
展会推荐
让朕来说2句
评论
收藏
点赞
转发