How can I prevent iOS apps from resetting after changing Camera permissions?

霸气de小男生 提交于 2019-12-18 03:23:10

问题


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 follow these steps exactly:

  1. Open an app that uses the camera permission.
  2. Navigate to some screen within the app (so you can visibly see the refresh later)
  3. Go to the Settings app, navigate to the app's settings, and toggle the camera permission
  4. 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.


回答1:


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
{
 [super viewDidLoad];
 [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(applicationWillTerminate:)
    name: UIApplicationWillTerminateNotification object:nil];
}

- (void)applicationWillTerminate:(NSNotification *)notification
{
 // store your data here
}

Hope this will help you:)




回答2:


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:

func enteringBackground(sender:AnyObject){
    // Save application state here
}


来源:https://stackoverflow.com/questions/31706447/how-can-i-prevent-ios-apps-from-resetting-after-changing-camera-permissions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!