【问题标题】:Check condition for a zip file in UIDocumentPickerViewController检查 UIDocumentPickerViewController 中 zip 文件的条件
【发布时间】:2020-12-10 17:31:48
【问题描述】:

这是我调用 UIDocumentPickerViewController 来选择我的固件更新文件的代码,这些文件必须是 .zip。当我按下“选择”按钮时,会显示文档选择器视图:

@IBAction func selectButtonAction(_ sender: UIButton) {
   if sender.title(for: .normal) == "Select"{
      if let controller = (UIApplication.shared.delegate as? AppDelegate)?.currentViewController {
         let importMenu = UIDocumentPickerViewController(documentTypes: [String(kUTTypeArchive)], in: .open )
         importMenu.delegate = self
         importMenu.modalPresentationStyle = .formSheet
         controller.present(importMenu, animated: true, completion: nil)
       }
    } else {
       changeDFUItemsDesign(isFileURLNil: true)
  }
}

现在可以以.docx 格式打开文件,但我只需要让用户选择一种格式 - zip 文件。

我无法展示我到目前为止所做的事情,因为我无法找到解决方案。有没有办法检查 zip 文件或只是禁止选择其他格式?谢谢!

【问题讨论】:

    标签: ios swift zip uidocumentpickerviewcontroller


    【解决方案1】:

    使用支持的类型列表初始化 DocumentPicker。

        let zip = ["com.pkware.zip-archive"]
        let importMenu = UIDocumentPickerViewController(documentTypes: zip, in: .import)
    

    Here 是支持的 UTI 列表

    【讨论】:

    【解决方案2】:

    在我的视图扩展中,我使用UIDocumentPickerDelegate,并在函数中检查我的文件的最后一个组件是否为 zip:

    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
       if let fileURL = urls.first, fileURL.pathExtension == "zip" {
          self._fileURL = fileURL
          self.fileNameLabel.text = _fileURL?.lastPathComponent
        } else {
          _fileURL = nil
          fileNameLabel.text = "Select file"
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-07
      • 1970-01-01
      • 2011-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      相关资源
      最近更新 更多