[Code Note] iOS-the password will be reset one hour after entering the background, and ios-Background

Source: Internet
Author: User

[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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.