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

时间:2020-12-08 15:03:24

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

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

先链一下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

}

转载自:http://www.jianshu.com/p/ada52c520ccb