0

I am working on UIDocumentPickerViewController() in Swift. After opening the document picker the files and images look blurred and I can't select any of them. I have attached the screenshot and the code below. Any idea why these files and images are not selectable?

Files and images look are not selectable

extension BecomeMuftiTwoVC: UIDocumentPickerDelegate {
    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        guard let selectedFilesURL = urls.first else {
            return
        }
        let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        let sandboxFileURL = dir.appendingPathComponent(selectedFilesURL.lastPathComponent)
        
        if FileManager.default.fileExists(atPath: sandboxFileURL.path) {
            print("Already File Exist")
        }
        else {
            
            do{
                try FileManager.default.copyItem(at: selectedFilesURL, to: sandboxFileURL)
                print("Copied Files")
            }
            catch{
                print("Files Are not exist ")
                
            }
        }
    }
}
//MARK: - Import File Btn Action
@IBAction func importFile(_ sender: Any) {
    let documentPicker = UIDocumentPickerViewController(documentTypes: [KUTTypedPlainText], in: .import)
    documentPicker.delegate = self
    documentPicker.allowsMultipleSelection = false
    present(documentPicker, animated: true, completion: nil)

1 Answer 1

1

Your line

let documentPicker = UIDocumentPickerViewController(documentTypes: [KUTTypedPlainText], in: .import)

seems to initialize a document picker for plain-text files only.

Try this instead:

let documentPicker = UIDocumentPickerViewController(documentTypes: [kUTTypeImage as String], in: .import)

Also, be careful as the initializer init(documentTypes:in:) is deprecated since iOS14.0. So for iOS14 and above, you should use:

let documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: [.heic, .heif, .livePhoto, .image])
Sign up to request clarification or add additional context in comments.

3 Comments

What if user want to pick files other than the type mensioned in e.g. mkv, rar, 7z forOpeningContentTypes: [.heic, .heif, .livePhoto, .image]
This is an enum. I'd suggest you take a look at available options for UTType here: developer.apple.com/documentation/uniformtypeidentifiers/…
Does that mean We will not be able to upload files other than the available options?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.