UIDocumentPickerViewController - 'init(documentTypes:in:)' was deprecated in iOS 14.0
Asked Answered
O

2

12

Method is deprecated from iOS 14, so need support for both iOS 14 and later. And iOS 13 and earlier.

Opalescent answered 4/9, 2021 at 11:13 Comment(0)
O
19

Here is the complete code written in Swift 5 to support earlier version of iOS 14 and later

This method is deprecated from iOS 14

public init(documentTypes allowedUTIs: [String], in mode: UIDocumentPickerMode)
  1. Write this code in your button action

    import UniformTypeIdentifiers
    
    @IBAction func importItemFromFiles(sender: UIBarButtonItem) {
    
             var documentPicker: UIDocumentPickerViewController!
             if #available(iOS 14, *) {
                 // iOS 14 & later
                 let supportedTypes: [UTType] = [UTType.image]
                 documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: supportedTypes)
             } else {
                 // iOS 13 or older code
                 let supportedTypes: [String] = [kUTTypeImage as String]
                 documentPicker = UIDocumentPickerViewController(documentTypes: supportedTypes, in: .import)
             }
             documentPicker.delegate = self
             documentPicker.allowsMultipleSelection = true
             documentPicker.modalPresentationStyle = .formSheet
             self.present(documentPicker, animated: true)
         }
    
  2. Implement Delegates

// MARK: - UIDocumentPickerDelegate Methods

extension MyViewController: UIDocumentPickerDelegate {
        func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
            
            for url in urls {
                
                // Start accessing a security-scoped resource.
                guard url.startAccessingSecurityScopedResource() else {
                    // Handle the failure here.
                    return
                }
                
                do {
                    let data = try Data.init(contentsOf: url)
                    // You will have data of the selected file
                }
                catch {
                    print(error.localizedDescription)
                }
                
                // Make sure you release the security-scoped resource when you finish.
                defer { url.stopAccessingSecurityScopedResource() }
            }
        }
        
        func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
            controller.dismiss(animated: true, completion: nil)
        }
    }
Opalescent answered 4/9, 2021 at 11:13 Comment(2)
import UniformTypeIdentifiers in case using UTType.image to made it in scopeHooknose
It's much easier to create UIDocumentPickerViewController(forOpeningContentTypes: supportedTypes, asCopy: true) which is a fallback to .import previously. After that you don't need to worry about calling startAccessingSecurityScopedResource, stopAccessingSecurityScopedResourceCalcification
A
-1
   import UniformTypeIdentifiers

   let contentTypes: [UTType] = [
      .init(filenameExtension: "doc")!,
      .init(filenameExtension: "docx")!,
      .pdf,
      .presentation,
      .spreadsheet,
      .plainText,
      .text
   ]

    let documentPicker: UIDocumentPickerViewController

    if #available(iOS 14.0, *) {
        documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: contentTypes, asCopy: true)
    } else {
        documentPicker = UIDocumentPickerViewController(documentTypes: contentTypes.map({$0.identifier}), in: .import)
    }
Ashram answered 27/9, 2023 at 7:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.