Ios 通过UIImagePickerController,我们可以访问和上传图像。如何从iphone访问和上载文档?
我正在开发一个与贷款相关的应用程序,用户可以上传他的文档,如工资单、退货等。为此,我应该向用户展示他/她在iPhone中拥有的所有文档。如何显示文档选择器?当您想在应用程序中上载文档时,只需调用openDocumentPicker方法Ios 通过UIImagePickerController,我们可以访问和上传图像。如何从iphone访问和上载文档?,ios,uidocumentpickerviewcontroller,Ios,Uidocumentpickerviewcontroller,我正在开发一个与贷款相关的应用程序,用户可以上传他的文档,如工资单、退货等。为此,我应该向用户展示他/她在iPhone中拥有的所有文档。如何显示文档选择器?当您想在应用程序中上载文档时,只需调用openDocumentPicker方法 import MobileCoreServices func openDocumentPicker() { let importMenu = UIDocumentMenuViewController(documentTypes: [kUTT
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)
}
}
}
如果您想访问其他文档,而不仅仅是使用此代码,则只能访问此代码上方的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]
*/
UIDocumentPickerViewController
是您需要的
您可以使用您希望能够选择的文档类型列表和模式初始化one,该模式通常为。打开,可以直接访问云提供商中的文件。您还可以使用.import
,它将文件复制到您的容器中,而不是让您直接访问云提供商容器中的文件,前提是您的目标只是上传文件(上传后您可以删除副本)
创建选择器后,将其呈现,并实现委托方法didPickDocumentsAt
,以检索用户选择的文件列表
查看今年的WWDC会议«»iOS 11或更高版本
let importMenu = UIDocumentPickerViewController.init(documentTypes: ["public.item"], in: UIDocumentPickerMode.import)
self.present(importMenu, animated: true) {
}
有关更多说明,请参阅以下链接
iOS 10或更早版本
当文档选择器打开时,可以编写以下函数。它适用于您要上载的所有文件类型
func openDocumentPicker() {
let importMenu = UIDocumentMenuViewController(documentTypes: ["public.item"], in: .import)
importMenu.delegate = self
importMenu.modalPresentationStyle = .formSheet
self.present(importMenu, animated: true, completion: nil)
}
它是否允许所有类型的文档,如doc、pdf、txt我正在处理您的建议,我将ping结果。在这段代码中,如果您需要其他文档,您只能上载pdf,而不是告诉meGot我会检查out@PraveenGowlikar有解决方案吗?请不要使用UIDocumentMenuViewController。它已被弃用。您只需自己直接实例化UIDocumentPickerViewController即可。@ThomasDeniau我已经回答了swift 3的版本。当时没有人反对它。所以我写了代码。顺便说一句,感谢您提供的信息(当然;)但现在它已经被弃用了,所以我觉得这需要一个新的答案,让人们搜索和发现这个问题。