通过 UIImagePickerController 我们可以访问和上传图片。如何从 iphone 访问和上传文件?

By UIImagePickerController we can access and upload images. How to access and upload documents from iphone?

我正在开发一个与贷款相关的应用程序,用户可以在其中上传他的文件,如工资单、it-returns 等。为此,我应该向用户显示所有文件 he/she his/her iPhone。如何显示文档选择器?

当你想在你的应用程序中上传文档时,只需调用 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)
        }
    }
}

如果您想访问其他文档而不是仅使用此代码,则只有在此代码之上您才能访问 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 就是您要找的。

您使用您希望能够选择的文档类型列表和一种模式(通常是 .open 来直接访问云提供商中的文件来初始化一个。您还可以使用 .import 将文件复制到您的容器,而不是让您直接访问云提供商容器中的文件,如果目标只是上传它(您可以在上传后删除副本)。

创建选择器后,将其呈现,并实施委托方法 didPickDocumentsAt 以检索用户选择的文件列表。

查看 Particles sample code and this years WWDC session « Managing Documents in your iOS Apps»

iOS 11 或更高版本

let importMenu = UIDocumentPickerViewController.init(documentTypes: ["public.item"], in: UIDocumentPickerMode.import)
  self.present(importMenu, animated: true) {

 } 

更多说明请参考以下link https://www.techotopia.com/index.php/An_iOS_Document_Browser_Tutorial

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)
    }