1

I don't know what this is called. but I need to make something similar like this one

so if I want to pick an Image from the photo gallery, after pressing the button, the view controller will be displayed like this if using UIImagePickerController enter image description here

But I need to make something like this before uploading pdf, or doc file to the server, so the user can pick the document in their iPhone or in remote storage

something like this in Mac,

press the button enter image description here

and then choose the file enter image description here

how to do that for iPhone in Swift?

sarah
  • 3,819
  • 4
  • 38
  • 80
  • you want to upload xDocs from the iPhone ? or preview them only – Mohmmad S Sep 18 '18 at 09:30
  • @Tobi I need to upload pdf or xdocs to the server – sarah Sep 18 '18 at 09:31
  • alright, Considering that the files are inside the "Files" app that comes with the OS – Mohmmad S Sep 18 '18 at 09:34
  • right ? , and as the files are already on the device not on anywhere else, because "Files" the application reads the files from alot of places, like icloud , google drive ,, etc – Mohmmad S Sep 18 '18 at 09:35
  • @Tobi yes, thats what I want, but I don't know how to display those files to the user so the user can choose the pdf file – sarah Sep 18 '18 at 10:39
  • good, @sarah since you don't need a code but rather a way ill provide you an answer of how to do it – Mohmmad S Sep 18 '18 at 10:52
  • @sarah you can alse see some document picker code in here https://stackoverflow.com/questions/37296929/implement-document-picker-in-swift-ios/42370660 – maximiliano Sep 18 '18 at 22:44

2 Answers2

0

as far as i figured out of what you need, FilesProvider has an API that you can interact with thru other applications,

read more about it here

and Here is a good Tutorial about it here

that should gives you a good understanding of how the API work, and how FileProvider work in iOS

Mohmmad S
  • 5,001
  • 4
  • 18
  • 50
  • please note, this is not code problem rather than of what to do problem, check out the links i am sure they will help you ^^ happy coding – Mohmmad S Sep 18 '18 at 10:54
0

You need to see this for document picker

see this link

Copied from above link

This method is deprecated from iOS 14

 public init(documentTypes allowedUTIs: [String], in mode: UIDocumentPickerMode)

Write this code in your button action

  @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)
 }

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)
    }
}
Sazid Iqabal
  • 409
  • 2
  • 21