你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> iOS上緩存文件有效期的實現

iOS上緩存文件有效期的實現

編輯:關於IOS
@implementation NSString (NSString_PathValid)
- (int)Interval
{
        NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:self error:nil];
        //NSLog(@"create date:%@",[attributes fileModificationDate]);
        NSString *dateString = [NSString stringWithFormat:@"%@",[attributes fileModificationDate]];
        
        
        NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
        [inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
        
        NSDate *formatterDate = [inputFormatter dateFromString:dateString];
        
        
        unsigned int unitFlags = NSDayCalendarUnit;
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *d = [cal components:unitFlags fromDate:formatterDate toDate:[NSDate date] options:0];
        
        [inputFormatter release];
        
        //NSLog(@"%d,%d,%d,%d",[d year],[d day],[d hour],[d minute]);
        
        int result = [d day];
        
    //        return 0;
        return result;
}

- (BOOL)isValid
{
        if ([self Interval] < VALIDDAYS) { //VALIDDAYS = 有效時間天數
                return YES;
        }
        return NO;
}
NSString *path =@"51ios/文件路徑";
if ([path isValid]){
// 有效
}
else{
// 無效
}

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved