iOS密码在进入后台1小时后重新设置
废话不多说了,直接给大家贴代码了,具体代码如下所示:
AppDelegate.m
#import"AppDelegate.h"
#import"ViewController.h"
@interfaceAppDelegate()
@end
@implementationAppDelegate
-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{
//Overridepointforcustomizationafterapplicationlaunch.
//当程序在后台停留超过60分的时候,密码会置为空。
//1小时后将密码重新设置
[selftimeInterval];
returnYES;
}
-(void)applicationWillResignActive:(UIApplication*)application{
//Sentwhentheapplicationisabouttomovefromactivetoinactivestate.Thiscanoccurforcertaintypesoftemporaryinterruptions(suchasanincomingphonecallorSMSmessage)orwhentheuserquitstheapplicationanditbeginsthetransitiontothebackgroundstate.
//Usethismethodtopauseongoingtasks,disabletimers,andthrottledownOpenGLESframerates.Gamesshouldusethismethodtopausethegame.
}
-(void)applicationDidEnterBackground:(UIApplication*)application{
//计算时间差
[selfbackTime];
}
-(void)applicationWillEnterForeground:(UIApplication*)application{
//1小时后将密码重新设置
[selftimeInterval];
}
-(void)applicationDidBecomeActive:(UIApplication*)application{
//Restartanytasksthatwerepaused(ornotyetstarted)whiletheapplicationwasinactive.Iftheapplicationwaspreviouslyinthebackground,optionallyrefreshtheuserinterface.
}
-(void)applicationWillTerminate:(UIApplication*)application{
//计算时间差
[selfbackTime];
}
#pragma-mark-密码保存1个小时
//计算时间差
-(void)timeInterval
{
NSLog(@"---timeInterval----");
//设置一个字符串的时间
NSString*dateBackString=[[NSUserDefaultsstandardUserDefaults]objectForKey:@"backGroundTime"];
NSLog(@"---dateBackString---%@",dateBackString);
if([dateBackStringisEqual:[NSNullnull]]||dateBackString==nil||dateBackString.length==0){
}
else
{
NSIntegertime=[selfgetTimeInterval:dateBackString];
if(time>=60){
//1小时后将密码清空
NSUserDefaults*userInfoDefault=[NSUserDefaultsstandardUserDefaults];
[userInfoDefaultsetObject:@""forKey:@"login-password"];
[userInfoDefaultsynchronize];
}
}
}
//1小时后将密码重新设置
-(void)backTime
{
NSLog(@"----backTime-----");
//计算上报时间差
NSDateFormatter*dateFormatter=[[NSDateFormatteralloc]init];
[dateFormattersetDateFormat:@"yyyy-MM-ddHH:mm:ss"];
//结束时间
NSDate*currentdate=[NSDatedate];
NSString*currentDateString=[dateFormatterstringFromDate:currentdate];
NSUserDefaults*userDefaults=[NSUserDefaultsstandardUserDefaults];
[userDefaultssetObject:currentDateStringforKey:@"backGroundTime"];
[userDefaultssynchronize];
}
//计算时间差
-(NSInteger)getTimeInterval:(NSString*)sendDateString
{
NSIntegerminute;
if(sendDateString==nil||sendDateString.length==0){
}
else
{
NSDateFormatter*dateFormatter=[[NSDateFormatteralloc]init];
[dateFormattersetDateFormat:@"yyyy-MM-ddHH:mm:ss"];
//结束时间
NSDate*currentdate=[NSDatedate];
NSDate*currentDate=[dateFormatterdateFromString:[dateFormatterstringFromDate:currentdate]];
NSDate*endDate=[dateFormatterdateFromString:sendDateString];
//得到时间差
NSTimeIntervaltime=[currentDatetimeIntervalSinceDate:endDate];
//intdays=((int)time)/(3600*24);
//inthours=((int)time)%(3600*24)/3600;
//minute=((NSInteger)time)%(3600*24)/3600/60;
minute=(NSInteger)time;
}
returnminute;
}
@end
总结
以上所述是小编给大家介绍的iOS密码在进入后台1小时后重新设置,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!