美文网首页iOS开发.
iOS文件管理功能使用

iOS文件管理功能使用

作者: 侭情显現 | 来源:发表于2020-07-04 18:05 被阅读0次

2017年6月5日.苹果WWDC大会就宣布了一项众望所归的功能.Files文件管理应用.该应用是从iOS11.0系统以后的自带应用

出现Files应用以后.对开发者来说是很友好的!可以将开发者存储到沙盒中的数据通过Files直接浏览!
1.方便开发者及时查看数据.
2.针对用户来说能达到安卓系统一样的文件管理功能.并可直接对文件进行删除/分享/浏览等操作

使用

在info配置中添加Supports opening documents in place并设置为YES
一般也会添加Application supports iTunes file sharing.并设置为YES.可以在iTunes中直接查看

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    [self showFilesWithURL:[NSURL fileURLWithPath:documentsDirectory]];

    return YES;
}

- (BOOL)showFilesWithURL:(NSURL *)URL
{
    assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
    NSError *error = nil;
    BOOL success = [URL setResourceValue:[NSNumber numberWithBool: YES] forKey: NSURLIsExcludedFromBackupKey error: &error];
    if (!success) {
        NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
    }
    return success;
}

随后将我们生成的xls/图片/pdf/csv等文件保存至沙盒中

- (void)createXLSFile {
    //获取数据
    NSString * muStr       = @"待存储数据";
    // 文件管理器
    NSFileManager *fileManager    = [[NSFileManager alloc]init];
    //使用UTF16才能显示汉字;如果显示为#######是因为格子宽度不够,拉开即可
    NSData *fileData              = [muStr dataUsingEncoding:NSUTF16StringEncoding];
    // 文件路径
    NSString * xlsPath = [self getXlsLocalPath];
    // 生成xls文件
    [fileManager createFileAtPath:xlsPath contents:fileData attributes:nil];
}

- (NSString *)getXlsLocalPath
{
    NSString* documentsDir = [self creatFile:@"LocationXls"];//在cache目录下生成一个盛放CSV的文件夹
    NSString *name = [NSString stringWithFormat:@"location%@",[ViewController getDate:NSDate.new formatStr:@"yyyyMMddHHmmss"]];
//    NSString *md5  = [self md5:name];//md5加密保证文件的唯一性
    NSString* xlsPath = [NSString stringWithFormat:@"%@/%@.xls",documentsDir,name];//文件路径
    NSLog(@"%@",xlsPath);
    return xlsPath;
}

- (NSString *)creatFile:(NSString *)name
{
    NSString *pathString = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    pathString = [NSString stringWithFormat:@"%@/%@",pathString,name];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //创建盛放CSV文件的文件夹
    if (![fileManager fileExistsAtPath:pathString])
    {
        [fileManager createDirectoryAtPath:pathString withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return pathString;
}

一起讲一下生成.xls文件以及.pdf文件

生成.xls文件.实际就是通过\t以及\n对数据进行分割.然后拼接成一串字符串:
//模拟数据
-(NSMutableString *)getFileMustr{
    // 创建存放XLS文件数据的数组
    NSMutableArray  *xlsDataMuArr = [[NSMutableArray alloc] init];
    // 第一行内容
    [xlsDataMuArr addObject:@"A"];
    [xlsDataMuArr addObject:@"D"];
    [xlsDataMuArr addObject:@"F"];
    [xlsDataMuArr addObject:@"R"];
    [xlsDataMuArr addObject:@"V"];
    [xlsDataMuArr addObject:@"U"];
    // 100行数据
    for (int i                    = 0; i < 100; i ++) {
        [xlsDataMuArr addObject:@(i).stringValue];
        [xlsDataMuArr addObject:@"2"];
        [xlsDataMuArr addObject:@"A"];
        [xlsDataMuArr addObject:@"6"];
        [xlsDataMuArr addObject:@"2"];
        [xlsDataMuArr addObject:@"m"];
    }
    // 把数组拼接成字符串,连接符是 \t(功能同键盘上的tab键)
    NSString *fileContent         = [xlsDataMuArr componentsJoinedByString:@"\t"];
    // 字符串转换为可变字符串,方便改变某些字符
    NSMutableString *muStr        = [fileContent mutableCopy];
    // 新建一个可变数组,存储每行最后一个\t的下标(以便改为\n)
    NSMutableArray *subMuArr      = [NSMutableArray array];
    for (int i                    = 0; i < muStr.length; i ++) {
        NSRange range                 = [muStr rangeOfString:@"\t" options:NSBackwardsSearch range:NSMakeRange(i, 1)];
        if (range.length == 1) {
            [subMuArr addObject:@(range.location)];
        }
    }
    // 替换末尾\t
    for (NSUInteger i             = 0; i < subMuArr.count; i ++) {
        if ( i > 0 && (i%6 == 0) ) {
            [muStr replaceCharactersInRange:NSMakeRange([[subMuArr objectAtIndex:i-1] intValue], 1) withString:@"\n"];
        }
    }
    return muStr;
}

随后将上述的
NSString * muStr = @"待存储数据";
替换成即可
NSMutableString * muStr = [self getFileMustr];

生成.pdf文件.实际就是先获取待拼接的图片元素.在context上下文中绘制
-(void)creatPdfFile{
    //沙盒存储地址
    NSString * pdfPath = [self getPdfLocalPath];
   //伪代码
    NSArray <UIImage *> *contentImgs = @[img1,img2,img3];
    UIGraphicsBeginPDFContextToFile(pdfPath, CGRectZero, [NSDictionary dictionaryWithObjectsAndKeys:@"优利德",kCGPDFContextAuthor, nil]);
    int count = 0;
    for (UIImage * itemContentImg in contentImgs) {
        CGFloat imgW = itemContentImg.size.width;
        CGFloat imgH = itemContentImg.size.height;
        CGFloat imgX = (self.view.bounds.size.width - itemContentImg.size.width) * 0.5;
        //使用该方法做分页.并且自定义每一页的尺寸
        UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, imgW, imgH), nil);
       [itemContentImg drawInRect:CGRectMake(imgX, 0, imgW, imgH)];
        count ++;
    }
    UIGraphicsEndPDFContext();
}

存储到沙盒中以后即可在系统的Files应用中的我的iPhone中查看保存的文件数据

21593856873_.pic.jpg

相关文章

  • iOS文件管理功能使用

    2017年6月5日.苹果WWDC大会就宣布了一项众望所归的功能.Files文件管理应用.该应用是从iOS11.0系...

  • iOS 文件(files)功能使用笔记

    iOS 文件(files)功能使用笔记 一.概述 1.Files 是什么 用一句话来总结,Files 可以集中管理...

  • 定位

    定位 1.实现定位功能 在iOS中使用定位功能,需要导入CoreLocation.h文件,其实现定位功能的步骤如下...

  • iOS文件管理系统NSFileManager使用详解

    iOS文件管理系统NSFileManager使用详解 1,找到自己的程序的目录: NSHomeDirectory(...

  • ios文件系统

    介绍如何使用mac与ios上的文件系统 获得指向文件管理器的引用NSFileManager :获得这个引用才能使用...

  • 新建header.h配置路径

    功能: 在iOS中, 可以新建一个header.h文件, 配置这个文件的路径后, 可以使用这个文件里的所有内容; ...

  • iOS中.Plist文件和.json文件的使用

    iOS -- .Plist 文件 什么是Plist文件 为什么使用Plist文件 iOS -- .json 文件的...

  • podspec中的引用资源文件

    podspec中的资源文件 iOS开发中使用cocoapod做依赖管理, 我们自己很多组件也使用cocoapod私...

  • iOS实现文件上传下载

    iOS开发中会经常用到文件上传下载的功能,这篇文件将介绍一下使用asp.net webservice实现文件上传下...

  • sandBox 文件对接器NSFileHandle

    文件对接器侧重于对文件内容的管理,可以使用它完成文件的继续写入,部分读取功能,使用之前必须保证文件是存在的 打印结果

网友评论

    本文标题:iOS文件管理功能使用

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