Code:
Appdelegate.m
#import"AppDelegate.h"#import"ViewController.h"@interfaceAppdelegate ()@end@implementationappdelegate-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (NSDictionary *) Launchoptions {//Override point for customization after application launch.//When the program stays in the background for more than 60 minutes, the password is set to empty.//Reset the password after 1 hours[Self timeinterval];ReturnYES;} - (void) Applicationwillresignactive: (UIApplication *) Application {//Sent when the application are about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as a incoming phone call or SMS message) or when the US Er quits the application and it begins the transition to the background state.//Use the This method to the pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.}- (void) Applicationdidenterbackground: (UIApplication *) Application {//Calculate Time Difference[Self backtime];} - (void) Applicationwillenterforeground: (UIApplication *) Application {//Reset the password after 1 hours[Self timeinterval];} - (void) Applicationdidbecomeactive: (UIApplication *) Application {//Restart any tasks this were paused (or not yet started) while the application is inactive. If the application is previously in the background, optionally refresh the user interface.}- (void) Applicationwillterminate: (UIApplication *) Application {//Calculate Time Difference[Self backtime];}#pragma-mark-Password save 1 hours//Calculate the time Difference-(void) timeinterval{NSLog (@"---timeinterval----");//Set the time of a string NSString * datebackstring = [[Nsuserdefaults standarduserdefaults] Objectforkey:@"Backgroundtime"]; NSLog (@"---datebackstring---%@", datebackstring);if ([Datebackstring isequal:[nsnullNULL]] | | Datebackstring==nil | | Datebackstring.length = =0) { }Else{Nsinteger time =[Self gettimeinterval:datebackstring];if (Time >=60) {//Empty the password after 1 hours nsuserdefaults *userinfodefault=[Nsuserdefaults Standarduserdefaults]; [Userinfodefault SetObject:@ "" Forkey:@"Login-password"]; [Userinfodefault Synchronize]; } }}//Reset the password after 1 hours-(void) backtime{NSLog (@"----Backtime-----");//Calculate escalation time difference nsdateformatter *dateformatter =[[NSDateFormatter alloc] init]; [Dateformatter Setdateformat:@"YYYY-MM-DD HH:mm:ss"];//End time NSDate * currentdate =[NSDate Date]; NSString * currentdatestring =[Dateformatter Stringfromdate:currentdate]; Nsuserdefaults *userdefaults =[Nsuserdefaults Standarduserdefaults]; [Userdefaults setobject:currentdatestring Forkey:@"Backgroundtime"]; [Userdefaults synchronize];}//Calculate Time Difference-(Nsinteger) Gettimeinterval: (NSString *) senddatestring{Nsinteger minute;if (senddatestring ==nil| | senddatestring.length==0) { }Else{NSDateFormatter *dateformatter =[[NSDateFormatter alloc] init]; [Dateformatter Setdateformat:@"YYYY-MM-DD HH:mm:ss"];//End time NSDate * currentdate =[NSDate Date]; NSDate * currentdate = [dateformatter Datefromstring:[dateformatter stringfromdate:currentdate]; NSDate * endDate = [dateformatter datefromstring:senddatestring]; // get time difference nstimeinterval time = [currentdate Timeintervalsincedate:enddate]; // int days = ((int) time)/(3600*24); // int hours = ((int) time)% (3600*24)/3600; // minute = ((nsinteger) time)% (3600*24)/3600/60; minute = (Nsinteger) time;} return minute;} @end
"Code Note" ios-password is reset after 1 hours into the background