欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS密码在进入后台1小时后重新设置

程序员文章站 2023-12-21 11:39:52
废话不多说了,直接给大家贴代码了,具体代码如下所示: appdelegate.m #import "appdelegate.h" #import "viewc...

废话不多说了,直接给大家贴代码了,具体代码如下所示:

appdelegate.m

#import "appdelegate.h"
#import "viewcontroller.h"
@interface appdelegate ()
@end
@implementation appdelegate
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions {
  // override point for customization after application launch.
  //当程序在后台停留超过60分的时候,密码会置为空。
  //1小时后将密码重新设置
  [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 {
  //计算时间差
  [self backtime];
}
- (void)applicationwillenterforeground:(uiapplication *)application {
  //1小时后将密码重新设置
  [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 {
  //计算时间差
  [self backtime];
}
#pragma -mark -密码保存1个小时
//计算时间差
- (void)timeinterval
{
  nslog(@"---timeinterval----");
  //设置一个字符串的时间
  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) {
      //1小时后将密码清空
      nsuserdefaults *userinfodefault=[nsuserdefaults standarduserdefaults];
      [userinfodefault setobject:@"" forkey:@"login-password"];
      [userinfodefault synchronize];
    }
  }
}
//1小时后将密码重新设置
- (void)backtime
{
  nslog(@"----backtime-----");
  //计算上报时间差
  nsdateformatter *dateformatter = [[nsdateformatter alloc] init];
  [dateformatter setdateformat:@"yyyy-mm-dd hh:mm:ss"];
  //结束时间
  nsdate * currentdate = [nsdate date];
  nsstring * currentdatestring = [dateformatter stringfromdate: currentdate];
  nsuserdefaults *userdefaults = [nsuserdefaults standarduserdefaults];
  [userdefaults setobject:currentdatestring forkey:@"backgroundtime"];
  [userdefaults synchronize];
}
//计算时间差
- (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"];
    //结束时间
    nsdate * currentdate = [nsdate date];
    nsdate * currentdate = [dateformatter datefromstring:[dateformatter stringfromdate: currentdate]];
    nsdate * enddate = [dateformatter datefromstring:senddatestring];
    //得到时间差
    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

总结

以上所述是小编给大家介绍的ios密码在进入后台1小时后重新设置,希望对大家有所帮助

上一篇:

下一篇: