UIDocumentPickerViewController
init(documentTypes allowedUTIs: [String], in mode: UIDocumentPickerMode)
UIDocumentPickerMode
import //复制导入沙盒中
open //共享目录打开
exportToService
moveToService
官方给的文档说直接首前两个。
documentTypes
所以支持的类型,这里是
官方地址
let vc = UIDocumentPickerViewController(documentTypes: ["public.content","public.text"], in: .open)
vc.delegate = self
vc.modalPresentationStyle = .fullScreen
vc.allowsMultipleSelection = true//ios11之后 加入多选功能
self.present(vc, animated: true, completion: nil)
* 此处必须是present方法 如果navigationController?.pushViewController(vc, animated: true) 选中文件不会出发代理事件,不知原因,这是我遇到的一个坑。
UIDocumentPickerDelegate
documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL)
ios8.0 ios11废弃
documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL])
iOS11.0+ 选中文件后,文件的地址。
*需要注意的是,URL的权限不能直接访问共享文件的内容,需要申请权限,操作完成释放权限
url.tartAccessingSecurityScopedResource()
申请权限,BOOL
url.stopAccessingSecurityScopedResource()
释放
UIDocumentBrowserViewController
实例化的方法基本一样
let vc = UIDocumentBrowserViewController(forOpeningFilesWithContentTypes: ["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"])
vc.modalPresentationStyle = .fullScreen