public init(documentTypes allowedUTIs: [String], in mode: UIDocumentPickerMode)
在按钮操作中编写此代码
@IBAction func importItemFromFiles(sender: UIBarButtonItem) {
var documentPicker: UIDocumentPickerViewController!
if #available(iOS 14, *) {
// iOS 14 & later
let supportedTypes: [UTType] = [UTType.image]
documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: supportedTypes)
} else {
// iOS 13 or older code
let supportedTypes: [String] = [kUTTypeImage as String]
documentPicker = UIDocumentPickerViewController(documentTypes: supportedTypes, in: .import)
}
documentPicker.delegate = self
documentPicker.allowsMultipleSelection = true
documentPicker.modalPresentationStyle = .formSheet
self.present(documentPicker, animated: true)
}
实作委派
// MARK: - UIDocumentPickerDelegate Methods
extension MyViewController: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
for url in urls {
// Start accessing a security-scoped resource.
guard url.startAccessingSecurityScopedResource() else {
// Handle the failure here.
return
}
do {
let data = try Data.init(contentsOf: url)
// You will have data of the selected file
}
catch {
print(error.localizedDescription)
}
// Make sure you release the security-scoped resource when you finish.
defer { url.stopAccessingSecurityScopedResource() }
}
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
controller.dismiss(animated: true, completion: nil)
}
}
2条答案
按热度按时间xeufq47z1#
就我了解到的您的需求而言,FilesProvider有一个API,您可以通过其他应用程序与它交互,
了解更多信息here
这是一个很好的教程here
这将使您很好地了解API的工作原理以及FileProvider在iOS中的工作原理
kgsdhlau2#
您需要查看此文档选择器
see this link
从以上链接复制
iOS 14已弃用此方法
在按钮操作中编写此代码
实作委派