代码:

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小时后重新设置的更多相关文章

  1. IOS开发笔记 IOS如何访问通讯录

    IOS开发笔记  IOS如何访问通讯录 其实我是反对这类的需求,你说你读我的隐私,我肯定不愿意的. 幸好ios6.0 以后给了个权限控制.当打开app的时候你可以选择拒绝. 实现方法: [plain] ...

  2. 支付宝APP支付IOS手机端java后台版

    版权声明:http://blog.csdn.net/u012131769/article/details/76639527#t8 转载:http://blog.csdn.net/u012131769/ ...

  3. iOS密码框实现(二)取消确定按钮

    由于将确定按钮去掉了,所以需要重新修改下代码,当输入第四个数字时,自动进入房间.   iOS 密码框效果图:     实现方式:   首先声明一个block初始化方法,因为这只是个框框,并不需要处理网 ...

  4. DW网页代码笔记

    DW网页代码笔记 1.样式.       class  插入类样式  标签技术(html)解决页面的内容样式技术(css)解决页面的外观脚本技术       解决页面动态交互问题<form> ...

  5. 【hadoop代码笔记】Mapreduce shuffle过程之Map输出过程

    一.概要描述 shuffle是MapReduce的一个核心过程,因此没有在前面的MapReduce作业提交的过程中描述,而是单独拿出来比较详细的描述. 根据官方的流程图示如下: 本篇文章中只是想尝试从 ...

  6. 【hadoop代码笔记】hadoop作业提交之汇总

    一.概述 在本篇博文中,试图通过代码了解hadoop job执行的整个流程.即用户提交的mapreduce的jar文件.输入提交到hadoop的集群,并在集群中运行.重点在代码的角度描述整个流程,有些 ...

  7. 【Hadoop代码笔记】目录

    整理09年时候做的Hadoop的代码笔记. 开始. [Hadoop代码笔记]Hadoop作业提交之客户端作业提交 [Hadoop代码笔记]通过JobClient对Jobtracker的调用看详细了解H ...

  8. 笔记-iOS 视图控制器转场详解(上)

    这是一篇长文,详细讲解了视图控制器转场的方方面面,配有详细的示意图和代码,为了使得文章在微信公众号中易于阅读,seedante 辛苦将大量长篇代码用截图的方式呈现,另外作者也在 Github 上附上了 ...

  9. <Python Text Processing with NLTK 2.0 Cookbook>代码笔记

    如下是<Python Text Processing with NLTK 2.0 Cookbook>一书部分章节的代码笔记. Tokenizing text into sentences ...

随机推荐

  1. 对于opencv全面貌的认识和理解

    1.opencv其实最开始只有源码,也就是sources中的代码,sources中有个modules,进入里面是各个我们平常使用的模块,如下图. 进入任意一个模块,比如calib3d,其中有inclu ...

  2. C#实现程序的版本升级更新

    我们做了程序,不免会有版本升级,这就需要程序有自动版本升级的功能.那么看看我是如何实现程序自动更新的. 直接上代码: using System; using System.Collections.Ge ...

  3. Java DB访问(一) JDBC

    项目说明 项目采用 maven 组织 ,jdbc 唯一的依赖就是 mysql-connector-java pom 依赖如下: <dependency> <groupId>my ...

  4. 设置ListView显示到最后一行

    上次聊天的那个界面上用的一个TextView,然后每次消息都用text.append("消息内容"+"\n")函数来在text字符串后边接一段,然后重新显示这个 ...

  5. Vue图片懒加载插件 - vue lazyload的简单使用

    Vue module for lazyloading images in your applications. Some of goals of this project worth noting i ...

  6. Android 开发工具类 36_ getSimSerial

    1 /** * 获取手机的 sim 卡串号 * 需要在清单文件中配置权限: * <uses-permission android:name="android.permission.RE ...

  7. redis集群环境的搭建和错误分析

    redis集群环境的搭建和错误分析 redis集群时,出现的几个异常问题 09 redis集群的搭建 以及遇到的问题

  8. 目标检测技术演进:R-CNN、Fast R-CNN、Faster R-CNN

    看到一篇循序渐进讲R-CNN.Fast R-CNN.Faster R-CNN演进的博文,写得非常好,摘入于此,方便查找和阅读. object detection,就是在给定的图片中精确找到物体所在位置 ...

  9. 常用的oh-my-zsh插件

    每次换电脑,需要重新配置开发环境是件很麻烦的事情,作为一个有洁癖的人又不想用Time Machine.记忆力大不如以前,很多插件又忘了装.正好下个月又需要给团队小伙伴讲讲提升效率这件事要讲到oh-my ...

  10. 【LeetCode题解】347_前K个高频元素(Top-K-Frequent-Elements)

    目录 描述 解法一:排序算法(不满足时间复杂度要求) Java 实现 Python 实现 复杂度分析 解法二:最小堆 思路 Java 实现 Python 实现 复杂度分析 解法三:桶排序(bucket ...