I won’t say much nonsense, I will just post the code to you. The specific code is as follows:
#import "" #import "" @interface AppDelegate () @end @implementation AppDelegate - (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 will be set to empty. //Reset password after 1 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 should use this method to pause the game. } - (void)applicationDidEnterBackground:(UIApplication *)application { // Calculate the time difference [self backTime]; } - (void)applicationWillEnterForeground:(UIApplication *)application { //Reset password after 1 hour [self timeInterval]; } - (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - (void)applicationWillTerminate:(UIApplication *)application { // Calculate the time difference [self backTime]; } #pragma -mark - Password saved for 1 hour// 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:[NSNull null]] || dateBackString==nil || ==0) { } else { NSInteger time = [self getTimeInterval:dateBackString]; if (time >= 60) { //Clear password in 1 hour NSUserDefaults *userInfoDefault=[NSUserDefaults standardUserDefaults]; [userInfoDefault setObject:@"" forKey:@"login-password"]; [userInfoDefault synchronize]; } } } //Reset password after 1 hour- (void)backTime { NSLog(@"----backTime-----"); //Calculate the reporting 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 the time difference- (NSInteger)getTimeInterval:(NSString *)sendDateString { NSInteger minute; if (sendDateString ==nil||==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 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
Summarize
The above is the iOS password introduced by the editor to you. After entering the background, it will be reset after 1 hour. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!