http://www.cocoachina.com/articles/896173
https://www.jianshu.com/p/d7215a9fc69f
https://www.jianshu.com/p/d7215a9fc69f
前段時間,筆者和GY哥一起吃飯聊天的時候,GY哥問了筆者一個問題,iOS App 可以后台保活嗎?是如何做到后台保活的?當時筆者只想到了可以在后台播放靜音的音樂,對於喚醒App,可以考慮使用推送的方式。GY哥提到播放靜音文件會影響上線嗎?這我就不大知道了…...由於沒有相關實踐,筆者后來在網上查了相關資料,總結了本文。
筆者查詢了相關資料后發現,iOS App可以實現后台保活。
短時間保活的方式有beginBackgroundTaskWithName;
App長時間保活的方式有:播放無聲音樂、后台持續定位、后台下載資源、BGTaskScheduler等;
喚醒App的方式有:推送、VoIP等;
本文分為如下幾部分:
-
App 運行狀態、及狀態變化
-
App 后台保活方式簡介
-
短時間App后台保活
-
Background Modes AVAudio,AirPlay,and Picture in Picture
-
Background Modes Location updates
-
BGTaskScheduler (iOS13.0+)
App 運行狀態、及狀態變化
不低於iOS13.0的設備端App 運行狀態
iOS13.0+的設備,支持多場景,共有上圖中的Unattached、Foreground Inactive、Foreground Active、Forground Inactive、Background、Suspended 6種狀態。
Unattached:多個場景的情況,如果創建的場景不是當前顯示的場景,那么場景處於Unattached狀態;
Foreground Inactive:應用啟動后,顯示啟動圖的過程中,處於Foreground Inactive狀態;
Forground Active:應用啟動后,顯示出來我們設置的rootViewController之后,場景處於Forground Active;
Foreground Inactive:應用啟動后,場景處於顯示狀態,數據加載完畢,且用戶和App沒有交互過程中,處於Forground Inactive狀態;
Background:用戶點擊Home鍵、或者是切換App后、鎖屏后,應用進入Background狀態;
Suspended:進入Background后,應用的代碼不執行后,應用進入Suspended狀態;(代碼是否在運行,可以在應用中寫定時器,定時輸出內容,從Xcode控制台,或Mac端控制台查看是否有輸出內容來判斷)
低於iOS13.0的設備端App 運行狀態
上圖是低於iOS13.0的設備端App的運行狀態,分別是Not Running、Foreground Inactive、Foreground Active、Forground Inactive、Background、Suspended 6種狀態。
Not Running:指用戶沒有啟動App,或用戶Terminate App 后,App處於的狀態;其他的五種狀態和不低於iOS13.0的設備端App的運行狀態意義相同。
App 進入后台狀態變化
筆者寫了個定時器,定時輸出“普通定時器進行中”,可以看到,應用進入后台后,基本上立刻,就沒有內容輸出了。筆者認為可以認為此時App 已經進入Suspended的狀態。
下邊筆者介紹下,嘗試的App后台保活方式。
iOS App 后台保活方式簡介
短時間App后台保活
beginBackgroundTaskWithName
和 endBackgroundTask
筆者嘗試過使用相關API,測試過2款手機。
對於系統版本低於iOS13(iOS 12.3)的設備(iPhone6 Plus)后台運行時間約3分鍾(175秒);
對於系統版本不低於iOS13(iOS 13.0)的設備(iPhone6 Plus)后台運行時間約31秒;
播放無聲音樂
App 進入后台后,播放無聲音樂,適用於音視頻類App。
筆者對逆向不了解,從iOS項目技術還債之路《一》后台下載趟坑中得知,騰訊視頻、愛奇藝采用了播放無聲音樂保活的方式。
后台持續定位
對於定位類App,持續定位App,可以實現App后台保活。定位類App需要后台保活,像系統的地圖應用,在導航的時候切換App的時候,就需要后台保活。
后台下載資源
對於需要下載資源的App,需要后台下載資源,如我們在某App下載資源的時候,我們希望在切換App時候,或者App退出后台后,資源仍然繼續下載,這樣當我們打開App的時候,資源已經下載好了。
BackgroundTasks
BackgroundTasks.framework 是iOS13新增的framework,筆者認為此framework中的API可以在信息流類的App中發揮作用。
短時間App后台保活
系統版本低於iOS13.0的設備
系統版本低於iOS13.0的設備,在應用進入后台的時候,開始后台任務([[UIApplication sharedApplication] beginBackgroundTaskWithName:)。在應用進入前台時或后台任務快過期的回調中,終止后台任務([[UIApplication sharedApplication] endBackgroundTask:)。
示例代碼如下:
- (void)applicationDidEnterBackground:(UIApplication *)application {
self.backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithName:kBgTaskName expirationHandler:^{
if (self.backgroundTaskIdentifier != UIBackgroundTaskInvalid) {
[[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskIdentifier];
self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
}
}];
}
復制代碼
- (void)applicationWillEnterForeground:(UIApplication *)application {
[[UIApplication sharedApplication] endBackgroundTask: self.backgroundTaskIdentifier];
}
復制代碼
添加相關代碼后,筆者在iOS12.4的6 Plus上測試結果如下,應用在進入后台后,大概還運行了175秒。
2019-12-29 19:06:55.647288+0800 QiAppRunInBackground[1481:409744] -[AppDelegate applicationDidEnterBackground:]:應用進入后台DidEnterBackground
2019-12-29 19:06:56.256877+0800 QiAppRunInBackground[1481:409744] 定時器運行中
….
2019-12-29 19:09:50.812460+0800 QiAppRunInBackground[1481:409744] 定時器運行中
系統版本不低於iOS13.0的設備
- (void)sceneDidEnterBackground:(UIScene *)scene API_AVAILABLE(ios(13.0)){
self.backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithName:kBgTaskName expirationHandler:^{
if (self.backgroundTaskIdentifier != UIBackgroundTaskInvalid) {
[[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskIdentifier];
self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
}
}];
}
復制代碼
- (void)sceneWillEnterForeground:(UIScene *)scene API_AVAILABLE(ios(13.0)){
[[UIApplication sharedApplication] endBackgroundTask: self.backgroundTaskIdentifier];
}
復制代碼
添加相關代碼后,筆者在iOS13.0的6s上測試結果如下,應用在進入后台后,大概還運行了31秒。
Xs·H 提到過,如果持續后台播放無聲音頻或是使用后台持續定位的方式實現iOS App后台保活,會浪費電量,浪費CPU,所以一般情況下,使用這種短時間延長App 后台保活的方式,應該夠開發者做需要的操作了。
Background Modes AVAudio,AirPlay,and Picture in Picture
對於音視頻類App,如果需要后台保活App,在App 進入后台后,可以考慮先使用短時間保活App的方式,如果后台保活App方式快結束后,還沒處理事情,那么可以考慮使用后台播放無聲音樂。相關示例代碼如下。
- (AVAudioPlayer *)player {
if (!_player) {
NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"SomethingJustLikeThis" withExtension:@"mp3"];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
audioPlayer.numberOfLoops = NSUIntegerMax;
_player = audioPlayer;
}
return _player;
}
復制代碼
[self.player prepareToPlay];
復制代碼
系統版本低於iOS13.0的設備
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"%s:應用進入后台DidEnterBackground", __FUNCTION__);
self.backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithName:kBgTaskName expirationHandler:^{
if ([QiAudioPlayer sharedInstance].needRunInBackground) {
[[QiAudioPlayer sharedInstance].player play];
}
if (self.backgroundTaskIdentifier != UIBackgroundTaskInvalid) {
[[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskIdentifier];
self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
}
}];
}
復制代碼
- (void)applicationWillEnterForeground:(UIApplication *)application {
NSLog(@"%s:應用將進入前台WillEnterForeground", __FUNCTION__);
if ([QiAudioPlayer sharedInstance].needRunInBackground) {
[[QiAudioPlayer sharedInstance].player pause];
}
[[UIApplication sharedApplication] endBackgroundTask: self.backgroundTaskIdentifier];
}
復制代碼
系統版本不低於iOS13.0的設備
- (void)sceneDidEnterBackground:(UIScene *)scene API_AVAILABLE(ios(13.0)){
NSLog(@"%s:應用已進入后台DidEnterBackground", __FUNCTION__);
self.backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithName:kBgTaskName expirationHandler:^{
if (self.backgroundTaskIdentifier != UIBackgroundTaskInvalid) {
if ([QiAudioPlayer sharedInstance].needRunInBackground) {
[[QiAudioPlayer sharedInstance].player play];
}
NSLog(@"終止后台任務");
[[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskIdentifier];
self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
}
}];
}
復制代碼
- (void)sceneWillEnterForeground:(UIScene *)scene API_AVAILABLE(ios(13.0)){
if ([QiAudioPlayer sharedInstance].needRunInBackground) {
[[QiAudioPlayer sharedInstance].player pause];
}
[[UIApplication sharedApplication] endBackgroundTask: self.backgroundTaskIdentifier];
NSLog(@"%s:應用將進入前台WillEnterForeground", __FUNCTION__);
}
復制代碼
Background Modes Location updates
開啟后台定位持續更新配置,添加了位置隱私申請后,在應用使用持續定位的情況下,可以實現后台保活App。
對於定位類App,如果需要后台保活App,在用戶使用了定位功能后,App 進入后台后,App自動具備后台保活能力,部分示例代碼如下。
self.locationManager = [CLLocationManager new];
self.locationManager.delegate = self;
[self.locationManager requestAlwaysAuthorization];
@try {
self.locationManager.allowsBackgroundLocationUpdates = YES;
} @catch (NSException *exception) {
NSLog(@"異常:%@", exception);
} @finally {
}
[self.locationManager startUpdatingLocation];
復制代碼
如果遇到如下異常信息:
2019-12-29 19:57:46.481218+0800 QiAppRunInBackground[1218:141397] 異常:Invalid parameter not satisfying: !stayUp || CLClientIsBackgroundable(internal->fClient) || _CFMZEnabled()
-
檢查:Signing&Capablities 的 backgounrd Modes 中 Location updates是否勾選;
后台下載資源
當需要實現下載資源類的App在進入后台后,持續下載資源的需求時。我們可能需要使用后台如下示例示例代碼。
創建指定標識的后台NSURLSessionConfiguration,配置好
NSURL *url = [NSURL URLWithString:@"https://images.pexels.com/photos/3225517/pexels-photo-3225517.jpeg"];
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.qishare.ios.wyw.backgroundDownloadTask"];
// 低於iOS13.0設備資源下載完后 可以得到通知 AppDelegate.m 文件中的 - (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)(void))completionHandler
// iOS13.0+的設備資源下載完后 直接在下載結束的代理方法中會有回調
sessionConfig.sessionSendsLaunchEvents = YES;
// 當傳輸大數據量數據的時候,建議將此屬性設置為YES,這樣系統可以安排對設備而言最佳的傳輸時間。例如,系統可能會延遲傳輸大文件,直到設備連接充電器並通過Wi-Fi連接到網絡為止。 此屬性的默認值為NO。
sessionConfig.discretionary = YES;
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:url];
[downloadTask resume];
復制代碼
BGTaskScheduler(iOS13.0+)
如果我們的App是信息流類App,那么我們可能會使用到BGTaskScheduler.framework中的API,實現后台保活App,幫助用戶較早地獲取到較新信息。
筆者嘗試使用BGTaskScheduler 做了一個獲取到App調度的時候。更新首頁按鈕顏色為隨機色並且記錄調度時間的Demo。
Demo示意圖
項目配置
為了App 支持 BGTaskScheduler,需要在項目中配置Background fetch,及Background Processing;
需要在Info.plist文件中添加 key 為Permitted background task scheduler identifiers,Value為數組的內容。
Value的數組填寫,刷新的任務標識和清理的任務標識。
注冊后台任務
在應用啟動后,注冊后台任務。
- (void)registerBgTask {
if (@available(iOS 13.0, *)) {
BOOL registerFlag = [[BGTaskScheduler sharedScheduler] registerForTaskWithIdentifier:kRefreshTaskId usingQueue:nil launchHandler:^(__kindof BGTask * _Nonnull task) {
[self handleAppRefresh:task];
}];
if (registerFlag) {
NSLog(@"注冊成功");
} else {
NSLog(@"注冊失敗");
}
} else {
// Fallback on earlier versions
}
if (@available(iOS 13.0, *)) {
[[BGTaskScheduler sharedScheduler] registerForTaskWithIdentifier:kCleanTaskId usingQueue:nil launchHandler:^(__kindof BGTask * _Nonnull task) {
[self handleAppRefresh:task];
}];
} else {
// Fallback on earlier versions
}
}
復制代碼
調度App 刷新
應用進入后台后,調度App 刷新。
- (void)sceneDidEnterBackground:(UIScene *)scene API_AVAILABLE(ios(13.0)){
[self scheduleAppRefresh];
}
- (void)scheduleAppRefresh {
if (@available(iOS 13.0, *)) {
BGAppRefreshTaskRequest *request = [[BGAppRefreshTaskRequest alloc] initWithIdentifier:kRefreshTaskId];
// 最早15分鍾后啟動后台任務請求
request.earliestBeginDate = [NSDate dateWithTimeIntervalSinceNow:15.0 * 60];
NSError *error = nil;
[[BGTaskScheduler sharedScheduler] submitTaskRequest:request error:&error];
if (error) {
NSLog(@"錯誤信息:%@", error);
}
} else {
// Fallback on earlier versions
}
}
復制代碼
得到后台任務調度的時候,調用App刷新的方法,筆者在這個方法中做了發送更新首頁按鈕顏色的通知,並且記錄了當前更新時間的記錄。
- (void)handleAppRefresh:(BGAppRefreshTask *)appRefreshTask API_AVAILABLE(ios(13.0)){
[self scheduleAppRefresh];
NSLog(@"App刷新====================================================================");
NSOperationQueue *queue = [NSOperationQueue new];
queue.maxConcurrentOperationCount = 1;
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
[[NSNotificationCenter defaultCenter] postNotificationName:AppViewControllerRefreshNotificationName object:nil];
NSLog(@"操作");
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
NSString *timeString = [dateFormatter stringFromDate:date];
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"QiLog.txt"];
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSData *data = [timeString dataUsingEncoding:NSUTF8StringEncoding];
[[NSFileManager defaultManager] createFileAtPath:filePath contents:data attributes:nil];
} else {
NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];
NSString *originalContent = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString *content = [originalContent stringByAppendingString:[NSString stringWithFormat:@"
時間:%@
", timeString]];
data = [content dataUsingEncoding:NSUTF8StringEncoding];
[data writeToFile:filePath atomically:YES];
}
}];
appRefreshTask.expirationHandler = ^{
[queue cancelAllOperations];
};
[queue addOperation:operation];
__weak NSBlockOperation *weakOperation = operation;
operation.completionBlock = ^{
[appRefreshTask setTaskCompletedWithSuccess:!weakOperation.isCancelled];
};
}
復制代碼
經過測試,發現App 在退到后台,沒有手動Terminate App的情況下。蘋果有調用過App調度任務的方法。現象上來看就是隔一段時間,我們再打開App 的時候可以發現,首頁的按鈕顏色改變了,相應的日志中追加了,調起相關方法的時間記錄。
手動觸發后台任務調度
Xcode運行我們的App
-> App 退到后台
-> 打開App 進入前台
-> 點擊下圖中藍框中的Pause program execution,輸入如下內容
e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier: @"com.qishare.ios.wyw.background.refresh"]
復制代碼
-> 再次點擊Continue program execution,就可以模擬后台啟動任務,調用我們的App。
查看日志記錄小提示
之前記得聽沐靈洛提過怎么便於查看日志,正好我這里也用到了。便於我們可以直接在File App中查看寫入到我們App的Documents中的文件,可以在Info.plist文件中添加key為LSSupportsOpeningDocumentsInPlace ,value為YES的鍵值對App 接入 iOS 11 的 Files App。
經過我們操作后,就可以打開File App -> 瀏覽 -> 我的iPhone -> 查看選擇我們的App -> 查看我們的日志記錄文件。
示例Demo
參考學習網址
iOS后台保活按時間可分為短時保活和長時間保活
-
短時保活的方式
通過beginBackgroundTaskWithName來實現。在iOS7-iOS13可以申請到大約3分鍾的保活時間,在iOS 13以后只能申請30秒左右的時間。- 先通過監聽UIApplicationWillEnterForegroundNotification(應用進入前台通知)和UIApplicationDidEnterBackgroundNotification(應用進入后台通知)。
[[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 {}
- 使用Background Task在應用進入后台時開啟保活,進入前台時關閉保活。
@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; } }
如果想申請多一點時間,可以使用NSTimer循環申請保活時間,但是建議不要無限申請保活時間,因為系統如果發現該app一直在后台運行,可能會直接殺掉app。
//開啟定時器 不斷向系統請求后台任務執行的時間 NSTimer *_timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(applyForMoreTime) userInfo:nil repeats:YES]; [_timer fire]; //在這里我判斷了申請次數,加上第一次申請保活時間的次數一共6次。 @property(nonatomic,assign) int applyTimes; -(void)applyForMoreTime { if ([UIApplication sharedApplication].backgroundTimeRemaining < 10) { _applyTimes += 1; NSLog(@"Try to apply for more time:%d",_applyTimes); [[UIApplication sharedApplication] endBackgroundTask:_backIden]; _backIden = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ [self stopKeepAlive]; }]; if(_applyTimes == 5){ [_timer invalidate]; _applyTimes = 0; [self stopKeepAlive]; } } }
- 長時間保活
App長時間保活的方式有:播放無聲音樂、后台持續定位、后台下載資源、BGTaskScheduler等,這些需要在蘋果后台開通后台權限,並且在xcode中也開啟相關權限。
- 播放無聲音樂,適用於音樂類app。像騰訊視頻、愛奇藝等用了播放無聲音樂保活的方式。
在app進入后台時開啟無聲音樂,進入前台后停止無聲音樂。(更好的處理方式是先獲取短時保活,短時快過時再播放無聲音樂)示例如下:
監聽進入前后台:
@property (nonatomic, strong) BackgroundPlayer* player; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil]; - (void)appWillEnterForeground { if (self.player) { [self.player stopPlayBackgroundAlive]; } } - (void)appDidEnterBackground { if (_player == nil) { _player = [[BackgroundPlayer alloc] init]; } [self.player startPlayer]; }
編寫音樂播放類:
#import <Foundation/Foundation.h> #import <AVFoundation/AVFoundation.h> @interface BackgroundPlayer : NSObject <AVAudioPlayerDelegate> { AVAudioPlayer* _player; } - (void)startPlayer; - (void)stopPlayer; @end #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]; NSURL *url = [[NSBundle bundleWithPath:WECAST_CLOUD_BUNDLE_PATH]URLForResource:@"你的音樂資源" withExtension:nil]; _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
-
后台持續定位
-
后台下載資源
創建指定標識的后台NSURLSessionConfiguration,配置好。
NSURL *url = [NSURL URLWithString:@"https://images.pexels.com/photos/3225517/pexels-photo-3225517.jpeg"]; NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.qishare.ios.wyw.backgroundDownloadTask"]; // 低於iOS13.0設備資源下載完后 可以得到通知 AppDelegate.m 文件中的 - (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)(void))completionHandler // iOS13.0+的設備資源下載完后 直接在下載結束的代理方法中會有回調 sessionConfig.sessionSendsLaunchEvents = YES; // 當傳輸大數據量數據的時候,建議將此屬性設置為YES,這樣系統可以安排對設備而言最佳的傳輸時間。例如,系統可能會延遲傳輸大文件,直到設備連接充電器並通過Wi-Fi連接到網絡為止。 此屬性的默認值為NO。 sessionConfig.discretionary = YES; NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil]; NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:url]; [downloadTask resume];
作者:Cherry_06
鏈接:https://www.jianshu.com/p/d7215a9fc69f
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。