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中查看保存的文件数据