我正在开发一个贷款相关的应用程序,用户上传他的文件,如支付单,退货等。为此,我应该向用户显示他/她在他/她的iPhone中的所有文件。如何显示文件选择器?
答案 0 :(得分:1)
UIDocumentPickerViewController
正是您要找的。 p>
您可以使用您希望能够选择的文档类型列表进行初始化,并使用一种模式(通常为.open
)直接访问云提供程序中的文件。您还可以使用.import
将文件复制到您的容器,而不是让您直接访问云提供商容器中的文件,如果目标只是上传它(您可以在上传后删除该副本)。
创建选择器后,将其呈现,并实现委托方法didPickDocumentsAt
以检索用户选择的文件列表。
查看Particles sample code和今年的WWDC会议«Managing Documents in your iOS Apps»
答案 1 :(得分:0)
如果要在应用程序中上传文档,只需调用openDocumentPicker方法即可。
import MobileCoreServices
func openDocumentPicker() {
let importMenu = UIDocumentMenuViewController(documentTypes: [kUTTypePDF as String], in: .import)
importMenu.delegate = self
self.present(importMenu, animated: true, completion: nil)
}
创建你的viewcontroller扩展
extension ViewController: UIDocumentMenuDelegate {
func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
documentPicker.delegate = self
present(documentPicker, animated: true, completion: nil)
}
func documentMenuWasCancelled(_ documentMenu: UIDocumentMenuViewController) {
print("we cancelled")
dismiss(animated: true, completion: nil)
}
}
extension ViewController: UIDocumentPickerDelegate {
internal func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
do {
let fileAttributes = try FileManager.default.attributesOfItem(atPath: url.path)
let fileSizeNumber = fileAttributes[FileAttributeKey.size] as! NSNumber
let fileSizea = fileSizeNumber.doubleValue
let fileSize = fileSizea/1000000.0
if fileSize > 5.0 {
appDelegate.displayAlert(Title: "", Message: "Selected File are too big,Please select file less than 5.0 mb")
} else {
let documentData = try Data(contentsOf: url, options: .dataReadingMapped)
}
} catch let error {
print(error.localizedDescription)
}
}
}
在此代码上方,只有在您想要访问anpther文档时才能访问pdf,而不仅仅是使用此代码
/*
let pdf = String(kUTTypePDF)
let spreadsheet = String(kUTTypeSpreadsheet)
let movie = String(kUTTypeMovie)
let aviMovie = String(kUTTypeAVIMovie)
let docs = String(kUTTypeCompositeContent)
let img = String(kUTTypeImage)
let png = String(kUTTypePNG)
let jpeg = String(kUTTypeJPEG)
let txt = String(kUTTypeText)
let zip = String(kUTTypeZipArchive)
let msg1 = String(kUTTypeEmailMessage)
let msg2 = String(kUTTypeMessage)
let types = [pdf, spreadsheet, movie, aviMovie, img, png, jpeg, txt, docs, zip, msg1, msg2]
*/
答案 2 :(得分:0)
iOS 11或更高版本
("INFO LED NOTIFICATION STARTED")
有关更多说明,请参阅以下链接 https://www.techotopia.com/index.php/An_iOS_Document_Browser_Tutorial
iOS 10或更早版本 您可以在文档选择器打开时编写以下功能。它适用于您要上传的所有文件类型。
let importMenu = UIDocumentPickerViewController.init(documentTypes: ["public.item"], in: UIDocumentPickerMode.import)
self.present(importMenu, animated: true) {
}