美文网首页
沙盒保存文件

沙盒保存文件

作者: kenewang | 来源:发表于2016-11-16 18:07 被阅读9次

    pragma mark - 沙盒保存文件

    + (void)createDocumentFileName:(NSString *)fileName content:(NSDictionary *)contentDic
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *directory = [[NSString alloc]initWithFormat:@"%@/%@",documentsDirectory,fileName];
        
        NSFileManager *fileManager = [NSFileManager defaultManager];
        // 创建目录
        if (![fileManager fileExistsAtPath:directory])
        {
            [fileManager createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:nil];
        }
        
        NSString *filePath = [directory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",fileName]];
        
        NSLog(@"filePath %@",filePath);
        //写文件
        [contentDic writeToFile:filePath atomically:YES];
    }
    
    + (NSDictionary *)getDocumentFileName:(NSString *)fileName
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *directory = [[NSString alloc]initWithFormat:@"%@/%@",documentsDirectory,fileName];
        
        NSString *filePath = [directory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",fileName]];
        
        NSDictionary *resultDic = [NSDictionary dictionaryWithContentsOfFile:filePath];
        
        NSLog(@"resultdic.....%@",resultDic);
        return resultDic;
    }
    
    + (void)deleteDocumentFileName:(NSString *)fileName
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *directory = [[NSString alloc]initWithFormat:@"%@/%@",documentsDirectory,fileName];
        NSString *filePath = [directory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",fileName]];
        
        NSFileManager *fileManager = [NSFileManager defaultManager];
        [fileManager removeItemAtPath:filePath error:nil];
    }
    

    相关文章

      网友评论

          本文标题:沙盒保存文件

          本文链接:https://www.haomeiwen.com/subject/slmrpttx.html