Can we lock the app while app goes to sleep mode in IPhone?

若如初见. 提交于 2019-12-03 09:16:06
Kanan Vora

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.

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.

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

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..

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