iOS應(yīng)用運(yùn)行時在設(shè)置中更改權(quán)限應(yīng)用崩潰問題

我們經(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)鏈接!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末拍霜,一起剝皮案震驚了整個濱河市嘱丢,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌祠饺,老刑警劉巖越驻,帶你破解...
    沈念sama閱讀 216,372評論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異道偷,居然都是意外死亡缀旁,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評論 3 392
  • 文/潘曉璐 我一進(jìn)店門勺鸦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來并巍,“玉大人,你說我怎么就攤上這事换途“枚桑” “怎么了刽射?”我有些...
    開封第一講書人閱讀 162,415評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長剃执。 經(jīng)常有香客問我誓禁,道長,這世上最難降的妖魔是什么忠蝗? 我笑而不...
    開封第一講書人閱讀 58,157評論 1 292
  • 正文 為了忘掉前任现横,我火速辦了婚禮漓拾,結(jié)果婚禮上阁最,老公的妹妹穿的比我還像新娘。我一直安慰自己骇两,他們只是感情好速种,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著低千,像睡著了一般配阵。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上示血,一...
    開封第一講書人閱讀 51,125評論 1 297
  • 那天棋傍,我揣著相機(jī)與錄音,去河邊找鬼难审。 笑死瘫拣,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的告喊。 我是一名探鬼主播麸拄,決...
    沈念sama閱讀 40,028評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼黔姜!你這毒婦竟也來了拢切?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,887評論 0 274
  • 序言:老撾萬榮一對情侶失蹤秆吵,失蹤者是張志新(化名)和其女友劉穎淮椰,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體纳寂,經(jīng)...
    沈念sama閱讀 45,310評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡主穗,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了烈疚。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片黔牵。...
    茶點(diǎn)故事閱讀 39,690評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖爷肝,靈堂內(nèi)的尸體忽然破棺而出猾浦,到底是詐尸還是另有隱情陆错,我是刑警寧澤,帶...
    沈念sama閱讀 35,411評論 5 343
  • 正文 年R本政府宣布金赦,位于F島的核電站音瓷,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏夹抗。R本人自食惡果不足惜绳慎,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望漠烧。 院中可真熱鬧凤覆,春花似錦、人聲如沸觅闽。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽度液。三九已至厕宗,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間堕担,已是汗流浹背已慢。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評論 1 268
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留霹购,地道東北人佑惠。 一個月前我還...
    沈念sama閱讀 47,693評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像厕鹃,于是被迫代替她去往敵國和親兢仰。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評論 2 353

推薦閱讀更多精彩內(nèi)容