8

I have folder with files in my app. How can i open default Files app with myapp folder programmatically?

This is similar folder of GarageBand App in Files.

enter image description here

alexbayker
  • 882
  • 9
  • 19

1 Answers1

14

You can open Files app in specific folder using shareddocuments: URL scheme and providing file path.


Example:

func getDocumentsDirectory() -> URL { // returns your application folder
    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    let documentsDirectory = paths[0]
    return documentsDirectory
}

let path = getDocumentsDirectory().absoluteString.replacingOccurrences(of: "file://", with: "shareddocuments://")
let url = URL(string: path)!

UIApplication.shared.open(url)


P.S:

For more information, read this article.

えるまる
  • 2,409
  • 3
  • 24
  • 44
  • This is not what is being asked here. OP is asking how to make his app to show as the initial directory. – Leo Dabus Oct 29 '20 at 15:14
  • @LeoDabus *How can i open default Files app with myapp folder programmatically?* May be I've got it wrong, but I was sure that there is a question about how to open Files app in some given directory. – えるまる Oct 29 '20 at 15:17
  • there is a property `directoryURL` that can be set to define the starting directory but I have never succeeded using it. https://developer.apple.com/documentation/uikit/uidocumentpickerviewcontroller/3183918-directoryurl – Leo Dabus Oct 29 '20 at 15:18
  • @LeoDabus Hm, I gonna wait for OP to respond. I will remove the answer if it doesn't relate. Just wanna help :) – えるまる Oct 29 '20 at 15:21
  • I want to open Files app (as other application), without selecting dialog – alexbayker Oct 30 '20 at 06:56
  • I know about replace "file" with "shareddocuments"))) – alexbayker Oct 30 '20 at 07:31
  • @alexbayker I hope it helped you :) – えるまる Oct 30 '20 at 07:32
  • 1
    This was exactly what I was looking for. Just adding this here if anyone is using this approach. You will need to enable showing your app folder. The link will show you how to **enable showing your app folder with downloaded files**. https://stackoverflow.com/questions/63481939/how-to-download-a-pdf-file-in-swift-from-a-url-and-where-to-find-the-file-in-fi – Francois Apr 07 '21 at 07:55
  • Is it possible to open this within the App? – unobatbayar Jan 17 '23 at 09:46
  • @unobatbayar you can use ur own views and access files using FileManager. https://stackoverflow.com/questions/27721418/getting-list-of-files-in-documents-folder – えるまる Jan 24 '23 at 10:33