【问题标题】:By UIImagePickerController we can access and upload images. How to access and upload documents from iphone?通过 UIImagePickerController 我们可以访问和上传图片。如何从 iPhone 访问和上传文件?
【发布时间】:2018-06-18 05:34:56
【问题描述】:

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

【问题讨论】:

    标签: ios uidocumentpickerviewcontroller


    【解决方案1】:

    UIDocumentPickerViewController 就是你要找的东西。

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

    一旦您创建了您的选择器,您就可以展示它,并实现委托方法didPickDocumentsAt 来检索用户选择的文件列表。

    查看Particles sample code 和今年的 WWDC 会议«Managing Documents in your iOS Apps»

    【讨论】:

      【解决方案2】:

      当您想在应用程序中上传文档时,只需调用 openDocumentPicker 方法即可。

      import MobileCoreServices  
      
       func openDocumentPicker() {
              let importMenu = UIDocumentMenuViewController(documentTypes: [kUTTypePDF as String], in: .import)
              importMenu.delegate = self
              self.present(importMenu, animated: true, completion: nil)
          }
      

      创建您的视图控制器扩展

      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]
       */
      

      【讨论】:

      • 它是否允许各种文档,如 doc、pdf、txt
      • 我正在处理你的建议,我会 ping 结果。
      • 在此代码中只有 pdf,如果您想要其他内容,可以上传,而不是告诉我
      • 知道了,我去看看
      • @PraveenGowlikar 你有解决方案吗?
      【解决方案3】:

      iOS 11 或更高版本

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

      更多描述请参考以下链接 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)
          }
      

      【讨论】:

      • 请不要使用 UIDocumentMenuViewController。它已被弃用。只需自己直接实例化一个 UIDocumentPickerViewController 即可。
      • @ThomasDeniau 我已经回答了 swift 3 的版本。当时它没有被弃用。所以我写了代码。顺便说一句,感谢您提供信息
      • 当然 ;) 但它现在已被弃用,所以我觉得对于搜索和查找此问题的人来说,这需要一个更新的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-10
      • 2011-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-23
      相关资源
      最近更新 更多