
使用哗隐丛自带的文件选择器 UIDocumentPickerViewController ,这个iOS8就可以使用了。展示效果如下:
具体使用如下(不用引入任何头文件):
这里有两个地方需要注意一下
最后,关于乱樱 form 请求可 查看
<UIDocumentPickerDelegate>
NSArray *types = @[@"public.content", @"派盯public.text", @"public.source-code ", @"public.image", @"public.audiovisual-content", @"com.adobe.pdf", @"com.apple.keynote.key", @"com.microsoft.word.doc", @"com.microsoft.excel.xls", @"com.microsoft.powerpoint.ppt”]
缺凯UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:types inMode:UIDocumentPickerModeOpen]
documentPicker.delegate = self
documentPicker.modalPresentationStyle = UIModalPresentationFullScreen
[ self presentViewController:documentPicker animated: YES completion: nil ]
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {
NSLog(@"文件路径=%@",url)
BOOL canAccessingResource = [url startAccessingSecurityScopedResource]
if (canAccessingResource) {
NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] init]
NSError *error
伏羡唤[fileCoordinator coordinateReadingItemAtURL:url options:0 error:&error byAccessor:^(NSURL *newURL) {
NSData *fileData = [NSData dataWithContentsOfURL:newURL]
NSArray *arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES )
NSString *documentPath = [arr lastObject]
NSString *desFileName = [documentPath stringByAppendingPathComponent:@"myFile.pdf"]
[fileData writeToFile:desFileName atomically: YES ]
[ self dismissViewControllerAnimated: YES completion: NULL ]
}]
if (error) {
// error handing
}
} else {
// startAccessingSecurityScopedResource fail
}
[url stopAccessingSecurityScopedResource]
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)