iOS因为沙盒机制的缘故,无法直接应用之间的文件。解决此问题的途径两种,一个是将文件通过iCloud的方式存储,然后应用的文件可以共享,具体内容不赘述。
第二个方式是通过文件关联的方式,也就是通过UIDocument中的方法打开在应用中打开并存储,表现形式如下:
要做到上述形式,必须先在info.plist中做好文件配置
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>
<string>com.myapp.common-data</string>
<key>LSItemContentTypes</key>
<array>
<string>com.microsoft.powerpoint.ppt</string>
<string>public.item</string>
<string>com.microsoft.word.doc</string>
<string>com.adobe.pdf</string>
<string>com.microsoft.excel.xls</string>
<string>public.image</string>
<string>public.content</string>
<string>public.composite-content</string>
<string>public.archive</string>
<string>public.audio</string>
<string>public.movie</string>
<string>public.text</string>
<string>public.data</string>
</array>
</dict>
</array>
在应用中选取别的应用打开后,会在如下方法获取文件地址,这个地址其实就是在实际打开应用中存储的地址
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
NSString *path = [url absoluteString];
NSMutableString *string = [[NSMutableString alloc] initWithString:path];
if ([path hasPrefix:@"file://"]) {
[string replaceOccurrencesOfString:@"file://" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, path.length)];
NSDictionary * dict = [NSDictionary dictionaryWithObject:path forKey:@"filepath"];
[[NSNotificationCenter defaultCenter]postNotificationName:KOpenFile object:nil userInfo:dict];
return YES;
}
}
在需要获取文件的controller中遵守代理UIDocumentInteractionControllerDelegate并打开文件,在此代理方法并未实现,只是简单的文件预览
if (!_path) {
return;
}
NSURL *fileURL = [NSURL URLWithString:_path];
//创建实例
self.documentController =
[UIDocumentInteractionController
interactionControllerWithURL:fileURL];
//设置代理
self.documentController.delegate = self;
//打开文件
[self.documentController presentPreviewAnimated:YES];
附获取本地文件名称路径及大小
NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDirectoryEnumerator *dirEnum = [fileManager enumeratorAtPath:docsDir];
NSString *fileName;
while (fileName = [dirEnum nextObject]) {
NSString * filepath = [docsDir stringByAppendingPathComponent:fileName];
//获取文件属性
unsigned long long fileSize = [[fileManager attributesOfItemAtPath:filepath error:nil]fileSize];
NSString * size ;
if (fileSize >= pow(10, 9)) { // size >= 1GB
size = [NSString stringWithFormat:@"%.2fGB", fileSize / pow(10, 9)];
} else if (fileSize >= pow(10, 6)) { // 1GB > size >= 1MB
size = [NSString stringWithFormat:@"%.2fMB", fileSize / pow(10, 6)];
} else if (fileSize >= pow(10, 3)) { // 1MB > size >= 1KB
size = [NSString stringWithFormat:@"%.2fKB", fileSize / pow(10, 3)];
} else { // 1KB > size
size = [NSString stringWithFormat:@"%zdB", fileSize];
}
//除掉inbox
if (fileName.length>6) {
NSString * newfileName = [fileName substringFromIndex:6];
NSMutableDictionary * dict = [[NSMutableDictionary alloc]init];
[dict setValue:newfileName forKey:@"fileName"];
[dict setValue:filepath forKey:@"path"];
[dict setValue:size forKey:@"fileSize"];
[self.fileArray addObject:dict ];
}
}
网友评论