代码来源:(取自微博项目)
#import "IWAccountTool.h"
#import "IWAccount.h"
#define IWAccountFile [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingString:@"account.data"]
@implementation IWAccountTool
+(void)saveAccount:(IWAccount *)account
{
//这是当前的时间值
NSDate *now = [NSDate date];
//这是当前时间加上一个timeout的值
account.expiresTime = [now dateByAddingTimeInterval:account.expires_in];
[NSKeyedArchiver archiveRootObject:account toFile:IWAccountFile];
}
+(IWAccount *)account
{
IWAccount *account = [NSKeyedUnarchiver unarchiveObjectWithFile:IWAccountFile];
//判断是否过期
NSDate *now = [NSDate date];
//返回结果是升序还是降序
if ([now compare:account.expiresTime] == NSOrderedAscending) {
// NSLog(@"now is %@ -%@",now,account.expiresTime);
//后者大,代表没有过期
return account;
}else{
//前者大,代表已经过期
return nil;
}
}
@end