Can we lock the app while app goes to sleep mode i

2020-06-06 07:28发布

问题:

I am working an app where lock option is included.My app starts with passcode screen.If I enter correct code then it navigates to next screen.If I dont use the app for long time it goes to sleep mode.When the user wants to run the app now, the passcode screen should appear and the user has to enter the code again.Is it possible?Is there any tutorial for this?Please dont mind to post the related code if you have done it.Thanks in advance.

回答1:

Yes ofcourse it is possible. You must open the screen in a method called applicationDidBecomeActive in your Application Delegate. This method is called every time the application is opened from background.

So whenever the user starts the already running app, this method will be called and from this you can first show the Password screen, and after that the respective screen.



回答2:

You can detect when your app goes to the background using the UIApplicationDidEnterBackgroundNotification. When it does, record the date and time. When the user opens the app back up, you will receive UIApplicationWillEnterForegroundNotification. When you receive that, compare the recorded date and time with the current date and time. If that's too old, display the passcode screen.



回答3:

check in app delegate class there the methods applicationDidEnterForeground and applicationDidEnterBackground are available do your coding there



回答4:

I have developed same type of apps, where I have implemented this things, For this I made a one Class like this

@interface CommonUIClass:NSObject

+(void)setCurrentViewController:(id)controller;

+(void)openPassWordProtectedScreen;

@end

And

@implementation CommonUIClass

static id currentViewControllerObj;

+(void)setCurrentViewController:(id)controller{ 

  currentViewControllerObj = controller;

}

+(void)openPassWordProtectedScreen{

PROTECTED_CONTROLLER *view = [[PROTECTED_CONTROLLER alloc]init];



if ([currentViewControllerObj respondsToSelector:@selector(presentModalViewController:animated:)]) {
        [currentViewControllerObj presentModalViewController:patternLock animated:NO];
}

}


@end

Just import this class to every ViewController And put this code to

-(void)viewWillApear{

[CommonUIClass setCurrentViewController:self];
[super viewWillApear];
}

And When Application Goes in Background

-(void)applicationWillResignActive:(UIApplication *)application{

[CommonUIClass openPassWordProtectedScreen];

}

Thanks..