Open iOS 11 Files app via URL Scheme or some other way
Asked Answered
F

4

13

My app is now sharing Documents directory on iOS 11 Files app "On My iPhone" section.

(Thanks to this article: Working with the Files App in iOS 11)

Question is how to open this directory or at least File app root page via URL Scheme. UIDocumentPickerViewController or UIDocumentBrowserViewController doesn't help in this situation.

Farm answered 30/9, 2017 at 5:26 Comment(2)
I am looking for a way to do this too. Could not find something yet.Archenemy
There is a thread about this in the Apple Developer Forums but without any answer yet: forums.developer.apple.com/message/257860#257860Archenemy
A
6
    guard let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first,
          let components = NSURLComponents(url: documentDirectory, resolvingAgainstBaseURL: true) else {
        return
    }
    components.scheme = "shareddocuments"
    
    if let sharedDocuments = components.url {
        UIApplication.shared.open(
            sharedDocuments,
            options: [:],
            completionHandler: nil
        )
    }
Agranulocytosis answered 15/3, 2021 at 14:29 Comment(3)
Welcome to stackoverflow, describe how this part of code is working, will make your answer more usableKerch
Using this snippet you can find the documents directory and replace the URL scheme from file:// to shareddocuments://, that is needed to open the Files app.Agranulocytosis
This works as of iOS 15Kerin
L
2

The Files app can be opened with the URL scheme shareddocuments://.

I don’t know if there’s a way to open a specific directory however.

Lytton answered 6/3, 2019 at 13:8 Comment(0)
A
1
    func openSharedFilesApp() {
        let documentsUrl =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        let sharedurl = documentsUrl.absoluteString.replacingOccurrences(of: "file://", with: "shareddocuments://")
        print(sharedurl)
        let furl:URL = URL(string: sharedurl)!
        UIApplication.shared.open(furl, options: [:], completionHandler: nil)
    }

This code would work.

Aria answered 8/4, 2020 at 7:45 Comment(0)
R
0

If you just want to open the Files app then use following code:

guard let urlForFilesApp = URL(string: "shareddocuments://"), UIApplication.shared.canOpenURL(urlForFilesApp) else{return}
UIApplication.shared.open(urlForFilesApp, options: [:])

And if you want to open your app's directory/folder named same as your App's name in the Files app then following code will work:

guard let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first, let appFolderURL = URL(string: "shareddocuments://\(documentsURL.path)")else{return}
guard UIApplication.shared.canOpenURL(appFolderURL) else{return}
UIApplication.shared.open(appFolderURL, options: [:])

Reference here->

Refrigeration answered 21/5, 2024 at 12:43 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.