iOS应用运行时在设置中更改权限应用崩溃问题

我们经常会在开发中获取设备的各种权限,当应用未经授权相关权限时,就会提示用户或者直接跳转到系统的‘设置’中来让用户授权。

那么问题来了,当用户在更改授权的时候你会发现你的程序崩溃了,本以为是代码的原因,可是在网上查阅了一下发现这应该是系统的一个强制行为(ps:百du就是一坨屎)

先链一下cocoachina相关问题的讨论:程序运行时设置里面改变通讯录访问权限程序崩溃

好像遇到类似问题的人真的很多,其实,这是系统的一个强制行为,并不是我们哪里做错了。

所以我们只能用一些蹩脚的方法曲线救国。

这是在Google上搜到的一篇国外译文,里边给出了一个解决方案:我怎么能阻止iOS应用程序重置后改变相机权限? - How can I prevent iOS apps from resetting after changing Camera permissions?


下面的翻译是原博客的机翻,不是我机翻的!!!不是我机翻的!!!不是我机翻的!!!我只是直接贴过来方便大家阅读(原博客)!总有一些高贵的大神想让人直接送到嘴里,嫌翻译是机翻,吧啦吧啦...,那就请这些高贵的大神直接看英文原文好吧,难道大神们都只看中文么?还有你来看博文是解决问题的不是来跟我这撕逼的,你要真想撕我陪你!!!

以下是原博客内容:!!!以下是原博客内容:!!!以下是原博客内容:!!!


主题:

目前,当我更改app的摄像头权限设置,然后导航回到我的应用程序,该应用程序将迫使一个刷新,我将失去我的地方应用。我完全遵循这些步骤:打开一个应用程序使用相机的许可。导航到一些屏幕内的应用程序(稍后可以明显看到刷新)设置应用,导航到应用程序的设置,和切换相机允许双击回家,回到应用程序。几秒钟后,它会刷新,带你回到第一个屏幕注意:我用iPhone6运行iOS8.4我注意到相机上所有应用程序都有这种行为的许可。我的问题是:有一些方法来阻止应用程序刷新/重新启动后(简历)改变相机允许吗?它似乎没有发生当你切换位置服务,例如,从可用性的角度来看这是可怕的。用户场景:如果用户导航深入你的应用程序,那么需要改变相机许可(因为上次说他们不小心点击没有),他们将被迫返回,返回时屏幕。这是特别有害的一个应用程序试图卖给你一些东西,或者你签去一个新的帐户。他们可能会推出一项新功能,您可以使用相机采取一个概要文件图片或扫描你的信用卡。因为用户不知道这个功能,他们可能此前曾否认相机访问,但是现在要启用它。试图使再能之后,他们回到你的应用找到他们要花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.


解决方案:

我确信没有其他方法来防止重启应用。实际上你会得到一个SIGKILL消息,但没有崩溃日志切换时设置。见下文链接:https://devforums.apple.com/message/715855https://devforums.apple.com/message/714178来防止这种情况的唯一方法就是保存你的先前状态应用程序而终止。应用当前的数据存储到一个json/plist/NSUserDefaults/档案用户模型在applicationWillTerminate:方法和恢复保存的数据在applicationWillEnterForeground:例如,@SignUpViewController注册UIApplicationWillTerminateNotification将火时,应用程序将终止。存储用户信息。希望这将帮助你:)

原文:

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:)

网友:applicationWillTerminate甚至不叫通知按照你的建议加入viewDidLoad所以任何想法吗?请帮助我。

(原文:applicationWillTerminate not called even notification is add in viewDidLoad as you suggest so any idea for this? please help me.)


解决方案:

接受答案是正确的,然而工作的解决方案没有出现在iOS的当前版本(9.2)——应用程序似乎终止UIApplicationWillTerminateNotification之前解雇。然而听UIApplicationDidEnterBackgroundNotification,同样可以实现。e.g在迅速,把它放到viewDidLoad()和有一个这样的函数:

原文:

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

}


版权声明:出自MajorLMJ技术博客的原创作品 ,转载时请注明出处及相应链接!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容