我們經(jīng)常會在開發(fā)中獲取設(shè)備的各種權(quán)限嘴拢,當(dāng)應(yīng)用未經(jīng)授權(quán)相關(guān)權(quán)限時桩盲,就會提示用戶或者直接跳轉(zhuǎn)到系統(tǒng)的‘設(shè)置’中來讓用戶授權(quán)。
那么問題來了席吴,當(dāng)用戶在更改授權(quán)的時候你會發(fā)現(xiàn)你的程序崩潰了赌结,本以為是代碼的原因,可是在網(wǎng)上查閱了一下發(fā)現(xiàn)這應(yīng)該是系統(tǒng)的一個強(qiáng)制行為(ps:百du就是一坨屎)
先鏈一下cocoachina相關(guān)問題的討論:程序運(yùn)行時設(shè)置里面改變通訊錄訪問權(quán)限程序崩潰
好像遇到類似問題的人真的很多孝冒,其實(shí)柬姚,這是系統(tǒng)的一個強(qiáng)制行為,并不是我們哪里做錯了庄涡。
所以我們只能用一些蹩腳的方法曲線救國量承。
這是在Google上搜到的一篇國外譯文,里邊給出了一個解決方案:我怎么能阻止iOS應(yīng)用程序重置后改變相機(jī)權(quán)限? - How can I prevent iOS apps from resetting after changing?Camera permissions?
下面的翻譯是原博客的機(jī)翻穴店,不是我機(jī)翻的K汉础!泣洞!不是我機(jī)翻的S欠纭!球凰!不是我機(jī)翻的Jㄍ取!呕诉!我只是直接貼過來方便大家閱讀(原博客)缘厢!總有一些高貴的大神想讓人直接送到嘴里,嫌翻譯是機(jī)翻义钉,吧啦吧啦...昧绣,那就請這些高貴的大神直接看英文原文好吧,難道大神們都只看中文么捶闸?還有你來看博文是解決問題的不是來跟我這撕逼的夜畴,你要真想撕我陪你!I咀场贪绘!
以下是原博客內(nèi)容:!Q氲税灌!以下是原博客內(nèi)容:!!菱涤!以下是原博客內(nèi)容:0病!粘秆!
主題:
目前,當(dāng)我更改app的攝像頭權(quán)限設(shè)置,然后導(dǎo)航回到我的應(yīng)用程序,該應(yīng)用程序?qū)⑵仁挂粋€刷新,我將失去我的地方應(yīng)用如迟。我完全遵循這些步驟:打開一個應(yīng)用程序使用相機(jī)的許可。導(dǎo)航到一些屏幕內(nèi)的應(yīng)用程序(稍后可以明顯看到刷新)設(shè)置應(yīng)用,導(dǎo)航到應(yīng)用程序的設(shè)置,和切換相機(jī)允許雙擊回家,回到應(yīng)用程序攻走。幾秒鐘后,它會刷新,帶你回到第一個屏幕注意:我用iPhone6運(yùn)行iOS8.4我注意到相機(jī)上所有應(yīng)用程序都有這種行為的許可殷勘。我的問題是:有一些方法來阻止應(yīng)用程序刷新/重新啟動后(簡歷)改變相機(jī)允許嗎?它似乎沒有發(fā)生當(dāng)你切換位置服務(wù),例如,從可用性的角度來看這是可怕的。用戶場景:如果用戶導(dǎo)航深入你的應(yīng)用程序,那么需要改變相機(jī)許可(因?yàn)樯洗握f他們不小心點(diǎn)擊沒有),他們將被迫返回,返回時屏幕昔搂。這是特別有害的一個應(yīng)用程序試圖賣給你一些東西,或者你簽去一個新的帳戶玲销。他們可能會推出一項新功能,您可以使用相機(jī)采取一個概要文件圖片或掃描你的信用卡。因?yàn)橛脩舨恢肋@個功能,他們可能此前曾否認(rèn)相機(jī)訪問,但是現(xiàn)在要啟用它摘符。試圖使再能之后,他們回到你的應(yīng)用找到他們要花5+分鐘注冊/購買了!之后,即使我可能會放棄贤斜。
原文:
Currently, when I change the camera permissions for my app in Settings, then navigate back to my app, the app will force a refresh and I will lose my place in the app. I followthese stepsexactly:
Open an app that uses the camera permission.
Navigate to some screen within the app (so you can visibly see the refresh later)
Go to the Settings app, navigate to the app's settings, and toggle the camera permission
Double click home and go back to the app. After a few seconds, it will refresh, bringing you back to the first screen
Note: I'm using an iPhone 6 running iOS 8.4
I've noticed this behavior on all apps that have the camera permission. My question is:Is there some way to prevent the app from refreshing/restarting (on resume) after changing the camera permission?It doesn't seem to happen when you toggle location services, for example, and from a usability perspective this is horrible.
User scenario: If a user navigates deep into your app, then needs to change the camera permission (because say they accidentally clicked no last time), they will be forced to navigate back to that screen when they return. This is especially harmful for an app trying to sell you something, or sign you up for a new account. They might try to introduce a new feature where you can use the camera to take a profile picture or scan your credit card. Because the user didn't know about this feature, they might have previously denied camera access, but now want to enable it. After trying to reenable, they come back to your app to find they have to spend 5+ minutes signing up / making a purchase, again! After that, even I would probably give up.
解決方案:
我確信沒有其他方法來防止重啟應(yīng)用。實(shí)際上你會得到一個SIGKILL消息,但沒有崩潰日志切換時設(shè)置议慰。見下文鏈接:https://devforums.apple.com/message/715855https://devforums.apple.com/message/714178來防止這種情況的唯一方法就是保存你的先前狀態(tài)應(yīng)用程序而終止蠢古。應(yīng)用當(dāng)前的數(shù)據(jù)存儲到一個json/plist/NSUserDefaults/檔案用戶模型在applicationWillTerminate:方法和恢復(fù)保存的數(shù)據(jù)在applicationWillEnterForeground:例如,@SignUpViewController注冊UIApplicationWillTerminateNotification將火時,應(yīng)用程序?qū)⒔K止。存儲用戶信息别凹。希望這將幫助你:)
原文:
I'm sure that there is no other ways to prevent restarting app. Actually you will get a SIGKILL message but no Crash log when toggling settings. See below links-
? ??https://devforums.apple.com/message/715855
? ??https://devforums.apple.com/message/714178
The only way to prevent this scenario is to save the previous state of you application while terminating.
Store app your current data into a json/plist/NSUserDefaults/archive user model at applicationWillTerminate: method and
restore saved data at applicationWillEnterForeground:
For example- @SignUpViewController register for UIApplicationWillTerminateNotification which will fire when the app is about to terminate. Store user information there.
- (void)viewDidLoad{?
[superviewDidLoad];?
[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(applicationWillTerminate:)? ? name: UIApplicationWillTerminateNotification object:nil];
}
- (void)applicationWillTerminate:(NSNotification*)notification{
? ? // store your data here
}
Hope this will help you:)
”
網(wǎng)友:applicationWillTerminate甚至不叫通知按照你的建議加入viewDidLoad所以任何想法嗎?請幫助我草讶。
(原文:applicationWillTerminate not called even notification is add in viewDidLoad as you suggest so any idea for this? please help me.)
解決方案:
接受答案是正確的,然而工作的解決方案沒有出現(xiàn)在iOS的當(dāng)前版本(9.2)——應(yīng)用程序似乎終止UIApplicationWillTerminateNotification之前解雇。然而聽UIApplicationDidEnterBackgroundNotification,同樣可以實(shí)現(xiàn)炉菲。e.g在迅速,把它放到viewDidLoad()和有一個這樣的函數(shù):
原文:
The accepted answer is correct, however the workaround does not appear to work in the current version of iOS (9.2) - the application seems to terminate before UIApplicationWillTerminateNotification is fired. However by listening to UIApplicationDidEnterBackgroundNotification, the same can be achieved. e.g in Swift, put this in viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self,selector:"enteringBackground:", name: UIApplicationDidEnterBackgroundNotification, object:nil)
and have a function like this:
funcenteringBackground(sender:AnyObject){
? ? // Save application state here
}
版權(quán)聲明:出自MajorLMJ技術(shù)博客的原創(chuàng)作品 堕战,轉(zhuǎn)載時請注明出處及相應(yīng)鏈接!