UIDocumentPickerViewController में ज़िप फ़ाइल के लिए स्थिति जांचें
यह मेरे फर्मवेयर अद्यतन के लिए फ़ाइलों को चुनने के लिए UIDocumentPickerViewController को कॉल करने के लिए मेरा कोड है जो .zipकेवल होना है । जब मैं "Select" बटन दबाता हूं, तो दस्तावेज़ पिकर दृश्य दिखाता है:
@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प्रारूप में फाइलें खोलना संभव है , लेकिन मुझे केवल उपयोगकर्ता को एक प्रारूप चुनने की आवश्यकता है - एक ज़िप फ़ाइल।
मैंने अब तक जो भी किया है, उसे प्रस्तुत नहीं कर सकता क्योंकि मैं इसका हल नहीं ढूंढ पा रहा हूं। वहाँ एक ज़िप फ़ाइल के लिए एक जाँच करने के लिए या सिर्फ अन्य स्वरूपों का चयन करने से मना करने का एक तरीका है? धन्यवाद!
जवाब
समर्थित प्रकारों की सूची के साथ DocumentPicker का परिचय दें।
let zip = ["com.pkware.zip-archive"]
let importMenu = UIDocumentPickerViewController(documentTypes: zip, in: .import)
यहाँ समर्थित यूटीआई की एक सूची है
मेरे विचार के विस्तार में मैं उपयोग UIDocumentPickerDelegateकरता हूं और फ़ंक्शन में मैं जांचता हूं कि क्या मेरी फ़ाइल का अंतिम घटक ज़िप है:
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"
}
}