[Code Note] iOS-the password will be reset one hour after entering the background, and ios-Background
Code:
AppDelegate. m
# Import "AppDelegate. h "# import" ViewController. h "@ interface AppDelegate () @ end @ implementation AppDelegate-(BOOL) application :( UIApplication *) application metadata :( NSDictionary *) launchOptions {// Override point for customization after application launch. // when the program stays at the background for more than 60 minutes, the password will be left blank. // Reset the password in one hour [self timeInterval]; return YES;}-(void) applicationWillResignActive :( UIApplication *) application {// Sent when the application is about to move from active to inactive state. this can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. games shocould use this method to pause the game .} -(void) applicationDidEnterBackground :( UIApplication *) application {// calculate the time difference [self backTime];}-(void) applicationWillEnterForeground :( UIApplication *) application {// reset the password [self timeInterval] in an hour;}-(void) applicationDidBecomeActive :( UIApplication *) application {// Restart any tasks that were paused (or not yet started) while the application was inactive. if the application was previusly in the background, optionally refresh the user interface .} -(void) applicationWillTerminate :( UIApplication *) application {// calculation time difference [self backTime];} # pragma-mark-password is saved for 1 hour // calculation time difference-(void) timeInterval {NSLog (@ "--- timeInterval ----"); // set the time of a string NSString * dateBackString = [[NSUserDefaults standardUserDefaults] objectForKey: @ "backGroundTime"]; NSLog (@ "--- dateBackString --- % @", dateBackString); if ([dateBackString isEqual: [NSNull null] | dateBackString = nil | dateBackString. length = 0) {} else {NSInteger time = [self getTimeInterval: dateBackString]; if (time> = 60) {// clear the password NSUserDefaults * userInfoDefault = [NSUserDefaults standardUserDefaults] in one hour; [userInfoDefault setObject: @ "forKey: @" login-password "]; [userInfoDefault synchronize] ;}}// reset the password one hour later-(void) backTime {NSLog (@ "---- backTime -----"); // calculate the report time difference NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat: @ "yyyy-MM-dd HH: mm: ss"]; // End Time NSDate * currentdate = [NSDate date]; NSString * currentDateString = [dateFormatter Expiration: currentdate]; NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults]; [userDefaults setObject: currentDateString forKey: @ "backGroundTime"]; [userDefaults synchronize];} // calculate the 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]; // obtain the 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