Using applicationwillenterforeground for a passcode screen

♀尐吖头ヾ 提交于 2019-12-03 14:23:35

You can centralize this behavior in your app delegate by implementing

-(void)applicationWillEnterForeground:(UIApplication*)application;

You might implement a singleton that stores the currently appropriate modal view controller, updating it in viewWillAppear in each of your view controllers.

Edit: I was assuming that you already had a series of view controllers that you wanted to show. I doubt you actually need it. If you have one called, say PasscodeInputController, then your applicationWillEnterForeground would look something like:

-(void)applicationWillEnterForeground:(UIApplication*)application {
    UIViewController *currentController =  [window rootViewController];
    PasscodeInputController *passcodeInput = [[PasscodeInputController alloc] init];

    [currentController presentModalViewController:passcodeInput animated:NO];
    [passcodeInput release];
}

I hope that addresses your question more directly.

Your application's behaviour when reentering the foreground should be as similar as possible to when it launches for the first time. With this in mind, you could combine your applicationDidFinishLaunching: with applicationWillEnterForeground: but considering some views might already be loaded. The code is something like this:

id myState = (isReentering) ? [self validateState] : [self loadStateFromSave];
NSArray * keyForObjectToLoad = [self objectsToLoadForState:myState];
for(NSString * key in keyForObjectToLoad)
    if(![self objectForKey:key]) [self loadObjectForKey:key];

Depending on your app's detail it might require initial work, but it has some benefits:

  • It will ensure launching and relaunching are similar, hence the user experience is not "random".
  • You can free many unwanted memory easier when in the background.
  • It's easier to manage your application's state from a central place.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!