iOS 文件选择

iOS 文件选择,第1张

文件来源是从手机 资源库 获取( 不包括沙盒文件 ),即 iCould云盘 。在iOS11之后系统自带的app 文携孙件 中的内容就是访问iCould云盘获取到的,即使删除此APP也不会影响我们功能的使用

使用哗隐丛自带的文件选择器 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]

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/tougao/8200223.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-14
下一篇2023-04-14

发表评论

登录后才能评论

评论列表(0条)

    保存