36 ios swift uidocumentpickervc
我想在我的iOS应用程序中选择任何类型的文件(.pdf,.docs,.xlsx,.jpeg,.txt,.rtf等).点击上传按钮,我希望我的应用程序打开一个目录并选择文件(DocumentsPicker)
@IBAction pickDocument(sender: UIButton) {
//Open Document Picker
}
Run Code Online (Sandbox Code Playgroud)
这样做的方法是什么Swift?
Ale*_*ros 64
从项目的功能,启用iCloud和Key-Sharing.
MobileCoreServices在你的类中导入,最后在你的UIViewController中扩展以下三个类:
UIDocumentMenuDelegate,UIDocumentPickerDelegate,UINavigationControllerDelegate
Run Code Online (Sandbox Code Playgroud)
实现以下功能:
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
guard let myURL = urls.first else {
return
}
print("import result : \(myURL)")
}
public func documentMenu(_ documentMenu:UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
documentPicker.delegate = self
present(documentPicker, animated: true, completion: nil)
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
print("view was cancelled")
dismiss(animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
怎么称呼这一切?将以下位代码添加到您的单击功能中..
func clickFunction(){
let importMenu = UIDocumentMenuViewController(documentTypes: [String(kUTTypePDF)], in: .import)
importMenu.delegate = self
importMenu.modalPresentationStyle = .formSheet
self.present(importMenu, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
单击您的按钮.将弹出以下菜单..
在DropBox的情况下.点击任何项目.您将被重定向回您的应用程序,URL将记录在您的终端中.
根据您的需要操作documentTypes.在我的应用程序中,用户只允许使用Pdf.所以,适合自己.
kUTTypePDF
kUTTypePNG
kUTTypeJPEG
...
此外,如果您想要自定义自己的菜单栏.添加以下代码并在处理程序中自定义您自己的函数
importMenu.addOption(withTitle: "Create New Document", image: nil, order: .first, handler: { print("New Doc Requested") })
Run Code Online (Sandbox Code Playgroud)
好好享受.
pab*_*ros 25
您可以使用UIDocumentPickerViewController从文件应用程序或iCloud Drive获取文件.
Key-value storage和iCloud Documents从iCloud功能:在要打开文档选择器的视图控制器上导入以下框架:
import MobileCoreServices
Run Code Online (Sandbox Code Playgroud)从以下方面实施以下方法UIDocumentPickerDelegate:
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
// you get from the urls parameter the urls from the files selected
}
Run Code Online (Sandbox Code Playgroud)创建一个UIDocumentPickerViewController以显示文件选择器或iCloud驱动器:
let types: [String] = [kUTTypePDF as String]
let documentPicker = UIDocumentPickerViewController(documentTypes: types, in: .import)
documentPicker.delegate = self
documentPicker.modalPresentationStyle = .formSheet
self.present(documentPicker, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
如果您希望用户可以将更多类型的文件导入您的应用程序,则必须添加更多UTTypes types NSArray.要查看所有可用类型,您可以查看UTType文档
当文档选择器在iOS 11上打开并且您尝试在Google云端硬盘中选择一个文件时,由于存在错误,该文件可能会被禁用:http://www.openradar.me/24187873
Sof*_*ner 13
该UIDocumentMenuViewController自iOS11已被弃用。从模态视图控制器显示时,我也发现它有问题。这是使用选择器的直接方法:
import MobileCoreServices
private func attachDocument() {
let types = [kUTTypePDF, kUTTypeText, kUTTypeRTF, kUTTypeSpreadsheet]
let importMenu = UIDocumentPickerViewController(documentTypes: types as [String], in: .import)
if #available(iOS 11.0, *) {
importMenu.allowsMultipleSelection = true
}
importMenu.delegate = self
importMenu.modalPresentationStyle = .formSheet
present(importMenu, animated: true)
}
extension AViewController: UIDocumentPickerDelegate, UINavigationControllerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
viewModel.attachDocuments(at: urls)
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
controller.dismiss(animated: true, completion: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
UIDocumentPickerViewController(documentTypes: [String], in: UIDocumentPickerMode)在 iOS 14.0 中已弃用
现在是 UIDocumentPickerViewController(forOpeningContentTypes: [UTType])
ContentTypes 为以下任意项或其组合:
UTType.image, UTType.text, UTType.plainText, UTType.utf8PlainText, UTType.utf16ExternalPlainText, UTType.utf16PlainText, UTType.delimitedText, UTType.commaSeparatedText, UTType.tabSeparatedText, UTType.utf8TabSeparatedText, UTType.rtf, UTType.pdf, UTType.webArchive, UTType.image, UTType.jpeg, UTType.tiff, UTType.gif, UTType.png, UTType.bmp, UTType.ico, UTType.rawImage, UTType.svg, UTType.livePhoto, UTType.movie, UTType.video, UTType.audio, UTType.quickTimeMovie, UTType.mpeg, UTType.mpeg2Video, UTType.mpeg2TransportStream, UTType.mp3, UTType.mpeg4Movie, UTType.mpeg4Audio, UTType.avi, UTType.aiff, UTType.wav, UTType.midi, UTType.archive, UTType.gzip, UTType.bz2, UTType.zip, UTType.appleArchive, UTType.spreadsheet, UTType.epub
Run Code Online (Sandbox Code Playgroud)
这对我来说是这样的:
let supportedTypes = [myArrayFromAnyOfTheAbove]
func openDocument() {
let documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: supportedTypes, asCopy: true)
documentPicker.delegate = self
documentPicker.allowsMultipleSelection = false
documentPicker.shouldShowFileExtensions = true
present(documentPicker, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
35505 次 |
| 最近记录: |