刪除文件及緩存

4、刪除目錄下所有文件

//獲取文件路徑
- (NSString *)attchmentFolder{

NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSString *path = [document stringByAppendingPathComponent:@"Attchments"];


NSFileManager *manager = [NSFileManager defaultManager];


if(![manager contentsOfDirectoryAtPath:path error:nil]){

[manager createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:nil];

}

return path;

}

 

--清除附件
BOOL result = [[NSFileManager defaultManager] removeItemAtPath:[[MOPAppDelegate instance] attchmentFolder] error:nil];


 // 清理緩存

        dispatch_async(

                       dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)

                       , ^{

                           NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];

                           

                           NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];

                           NSLog(@"files :%d",[files count]);

                           for (NSString *p in files) {

                               NSError *error;

                               NSString *path = [cachPath stringByAppendingPathComponent:p];

                               if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {

                                   [[NSFileManager defaultManager] removeItemAtPath:path error:&error];

                               }

                           }

                           [self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];});

    }



// 清理緩存成功

-(void)clearCacheSuccess

{


    NSLog(@"清理成功");

}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章