检查 UIDocumentPickerViewController 中 zip 文件的条件 [英] Check condition for a zip file in UIDocumentPickerViewController

查看:64
本文介绍了检查 UIDocumentPickerViewController 中 zip 文件的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我调用 UIDocumentPickerViewController 来选择固件更新文件的代码,这些文件必须是 .zip.当我按下选择"时按钮,文档选择器视图出现:

This is my code for calling UIDocumentPickerViewController to choose the files for my firmware update which have to be .zip only. When I press on "Select" button, the Document Picker View shows up:

@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 文件.

Right now it's possible to open the files in .docx format, but I need to only let the user pick one format - a zip file.

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

I cannot present what I have done so far because I am not able to find a solution. Is there a way to make a check for a zip file or just forbid selecting other formats? Thank you!

推荐答案

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

In my view's extension I use the UIDocumentPickerDelegate and in the function I check if my file's last component is 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"
    }
}

这篇关于检查 UIDocumentPickerViewController 中 zip 文件的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆