iOS應(yīng)用進入后臺后計時器和位置更新停止問題的解決辦法
由于iOS系統(tǒng)為“偽后臺”運行模式,當(dāng)按下HOME鍵時,如程序不做任何操作,應(yīng)用會有5秒的執(zhí)行緩沖時間,隨機程序被掛起,所有任務(wù)終端,包括計時器和位置更新等操作,但程序打開后臺模式開關(guān)后,部分任務(wù)可以再后臺執(zhí)行,如音頻,定位,藍牙,下載,VOIP,即便如此,程序的后臺運行最多可以延長594秒(大概是10分鐘)。不幸的是,程序在聲明后臺模式后很有可能在app上架時被拒?;诖耍已芯砍隽瞬挥蒙昝骱笈_模式就能讓計時器和定位在app進入前臺時繼續(xù)運行的方法。
實現(xiàn)原理如下:
利用iOS的通知機制,在程序進入后臺和再次回到前臺時發(fā)送通知,并記錄進入后臺的當(dāng)前時間和再次回到前臺的當(dāng)前時間,算出兩者的時間間隔,在程序任何需要的地方添加通知監(jiān)聽者,在監(jiān)聽方法中執(zhí)行代碼塊,代碼塊內(nèi)參數(shù)為通知對象和計算出的時間間隔。以計時器為例,程序再進入后臺后,計時器停止運行,此時運用上述方法,在程序再次回到前臺時執(zhí)行代碼塊中內(nèi)容,將程序進入后臺時計時器的當(dāng)前時間間隔加上代碼塊的時間間隔參數(shù)就能使計時器準(zhǔn)確無誤地計時。廢話不多說,上代碼:
在AppDelegate.m實現(xiàn)文件中:
- (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. [[NSNotificationCenter defaultCenter]postNotificationName:UIApplicationDidEnterBackgroundNotification object:nil]; } - (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. [[NSNotificationCenter defaultCenter]postNotificationName:UIApplicationWillEnterForegroundNotification object:nil]; }
代碼說明:程序進入后臺后,利用系統(tǒng)通知機制通知程序進入后臺和再次回到前臺,監(jiān)聽對象為所有對象。
之后定義一個處理程序進入后臺的類YTHandlerEnterBackground
// // YTHandlerEnterBackground.h // 分時租賃 // // Created by 柯其譜 on 17/2/24. // Copyright © 2017年 柯其譜. All rights reserved. // #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> /** 進入后臺block typedef */ typedef void(^YTHandlerEnterBackgroundBlock)(NSNotification * _Nonnull note, NSTimeInterval stayBackgroundTime); /** 處理進入后臺并計算留在后臺時間間隔類 */ @interface YTHandlerEnterBackground : NSObject /** 添加觀察者并處理后臺 */ + (void)addObserverUsingBlock:(nullable YTHandlerEnterBackgroundBlock)block; /** 移除后臺觀察者 */ + (void)removeNotificationObserver:(nullable id)observer; @end
在YTHandlerEnterBackground.m實現(xiàn)文件中:
// // YTHandlerEnterBackground.m // 分時租賃 // // Created by 柯其譜 on 17/2/24. // Copyright © 2017年 柯其譜. All rights reserved. // #import "YTHandlerEnterBackground.h" @implementation YTHandlerEnterBackground + (void)addObserverUsingBlock:(YTHandlerEnterBackgroundBlock)block { __block CFAbsoluteTime enterBackgroundTime; [[NSNotificationCenter defaultCenter]addObserverForName:UIApplicationDidEnterBackgroundNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) { if (![note.object isKindOfClass:[UIApplication class]]) { enterBackgroundTime = CFAbsoluteTimeGetCurrent(); } }]; __block CFAbsoluteTime enterForegroundTime; [[NSNotificationCenter defaultCenter]addObserverForName:UIApplicationWillEnterForegroundNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) { if (![note.object isKindOfClass:[UIApplication class]]) { enterForegroundTime = CFAbsoluteTimeGetCurrent(); CFAbsoluteTime timeInterval = enterForegroundTime-enterBackgroundTime; block? block(note, timeInterval): nil; } }]; } + (void)removeNotificationObserver:(id)observer { if (!observer) { return; } [[NSNotificationCenter defaultCenter]removeObserver:observer name:UIApplicationDidEnterBackgroundNotification object:nil]; [[NSNotificationCenter defaultCenter]removeObserver:observer name:UIApplicationWillEnterForegroundNotification object:nil]; } @end
該類實現(xiàn)了用來添加通知監(jiān)聽者并處理后臺和移除通知監(jiān)聽者的方法,需要注意的是,在addObserverUsingBlock方法中,必須有if (![note.object isKindOfClass:[UIApplication class]])的判斷,否則addObserverForName方法中的代碼塊會執(zhí)行多次,此代碼執(zhí)行了兩次。addObserverUsingBlock方法是在viewWillAppear方法中調(diào)用添加通知監(jiān)聽者,在viewWillDisappear方法中調(diào)用移除通知監(jiān)聽者。
例如,在使用了計時器NSTimer控制器中:
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [YTHandlerEnterBackground addObserverUsingBlock:^(NSNotification * _Nonnull note, NSTimeInterval stayBackgroundTime) { self.rentTimerInterval = self.rentTimerInterval-stayBackgroundTime; }]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self.timer invalidate]; [YTHandlerEnterBackground removeNotificationObserver:self]; }
我定義了一個倒計時5分鐘的計時器對象timer屬性,并定義了一個計時器當(dāng)前倒計時時間間隔rentTimerInterval屬性,在添加通知監(jiān)聽者代碼塊中,rentTimerInterval等于進入后臺時的倒計時時間間隔減去程序停留在后臺的時間間隔,當(dāng)計時器再次回到前臺時,計時器此時的時間間隔是持續(xù)的。雖然計時器并未在后臺持續(xù)運行,但是使用了此方法,同樣實現(xiàn)了計時器的正確即時。
同樣的,當(dāng)程序存在位置更新功能時,當(dāng)程序進入后臺,位置服務(wù)對象會自動停止更新,此時的作法依然是調(diào)用上述兩個處理進入后臺的方法,使得程序進入后臺后,再次開始定位:
在需要位置更新的類中:
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.locService.delegate = self; [self.locService startUserLocationService]; //進入后臺再進入前臺重新開始定位 [YTHandlerEnterBackground addObserverUsingBlock:^(NSNotification * _Nonnull note, NSTimeInterval stayBackgroundTime) { [self.locService startUserLocationService]; }]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; //停止定位 self.locService.delegate = nil; [self.locService stopUserLocationService]; //移除后臺監(jiān)聽 [YTHandlerEnterBackground removeNotificationObserver:self]; }
此處使用的是百度地圖SDK
利用這種方法,像是計時器和位置更新等需要在后臺運行的任務(wù)都可以實現(xiàn)相應(yīng)的需求,只是麻煩的是,在任何需要的類中都要調(diào)用這兩種方法,你可以根據(jù)自己的需求,在程序進入后臺和再次回到前臺時添加別的參數(shù)(通知對象參數(shù)是必須的),例如保存進入后臺前的操作等等?;蚴嵌x不同的添加通知監(jiān)聽者的方法以實現(xiàn)不同的需求。
以上所述是小編給大家介紹的iOS應(yīng)用進入后臺后計時器和位置更新停止問題的解決辦法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
使用objc runtime實現(xiàn)iOS閉環(huán)的懶加載功能
利用objc runtime的動態(tài)性實現(xiàn)懶加載可以實現(xiàn)即可增加又可刪除功能,也可以避免污染類型。這篇文章主要介紹了使用objc runtime實現(xiàn)iOS閉環(huán)的懶加載功能,需要的朋友可以參考下2019-06-06IOS 頭文件導(dǎo)入-@class注意事項總結(jié)
這篇文章主要介紹了IOS 頭文件導(dǎo)入-@class注意事項總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-05-05Objective-C的緩存框架EGOCache在iOS App開發(fā)中的使用
這篇文章主要介紹了Objective-C的緩存框架EGOCache在iOS App開發(fā)中的使用,重點講解了EGOCache對緩存過期時間的檢測及處理,需要的朋友可以參考下2016-05-05iOS UICollectionView刷新時閃屏的解決方法
本篇文章主要介紹了iOS UICollectionView刷新時閃屏的解決方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11改變iOS應(yīng)用中UITableView的背景顏色與背景圖片的方法
這篇文章主要介紹了改變iOS應(yīng)用中UITableView的背景顏色與背景圖片的方法,將UITableView的header、footer設(shè)成clearColor時要注意實際效果是否真的變透明,需要的朋友可以參考下2016-03-03