简体   繁体   中英

Present native ios Files app a specific folder from my app

I have an app say called MyApp it is primarily wkwebview . It is already integrated to Files app so I can import/export files from my wkwebview to be able to download files to local machine or upload files to the server.

Finally I am trying to build a UIbutton in my app that will allow my users to Jump into Files app in the folder where I am storing all their content. Instead of building a full FileProviderUI into my app, I just want the button to take the user into the Files App navigating to the folder.

When I give the path for UIDocumentInteractionController to be a directory and do a shared open to present it, nothing happens. No error, nothing at at all. I just want the user to be able to Jump into the folder called MyApp inside the Files app.

I thought it will be very simple. Adding a FileProvider extension or FilePRoviderUI extension seems superflous to just jump the user into this folder and let him interact with Files app to do whatever he likes - open/delete/modify document.

I have to assume that the users are not savvy enough to know even if I tell them that files are saved in Files App for them to be able to interact with directly when they are offline!

        let documentsUrl =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!    
        // Just jump to the folder forcing Files app to open this                    
        let viewer = UIDocumentInteractionController(documentsUrl)

        viewer.delegate = self
        viewer.presentPreview(animated: true)

Nothing gets presented to the user and nothing happens. The button tap just quietly fails.

I think I figured this out. There is a specific URL format that will automatically open the Files app it is shareddocuments:// - Here is a simple function that seems to achieve this quiet simply.

    func openSharedFilesApp() {
    let documentsUrl =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
    let sharedurl = documentsUrl.absoluteString.replacingOccurrences(of: "file://", with: "shareddocuments://")
    let furl:URL = URL(string: sharedurl)!
    UIApplication.shared.open(furl, options: convertToUIApplicationOpenExternalURLOptionsKeyDictionary([:]), completionHandler: nil)
}

Thanks, Vijay. Here it is in Objective-C.

- (void) openSharedFilesApp
{
    NSArray<NSString *>* directories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);     // yes = expand tilde
    NSURL* url = [NSURL fileURLWithPath:directories.firstObject];
    NSString* sharedDocumentPath = [url.absoluteString stringByReplacingOccurrencesOfString:@"file://" withString:@"shareddocuments://"];
    NSURL* sharedDocumentURL = [NSURL URLWithString:sharedDocumentPath];
    [UIApplication.sharedApplication openURL:sharedDocumentURL options:@{UIApplicationOpenURLOptionUniversalLinksOnly: @(NO)} completionHandler:^(BOOL success) {
    }];
}

Adding to @Vijay's answer in iOS 15, Swift 5 :

let documentsUrl =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let sharedurl = documentsUrl.absoluteString.replacingOccurrences(of: "file://", with: "shareddocuments://")
let furl:URL = URL(string: sharedurl)!
if UIApplication.shared.canOpenURL(furl) {
    UIApplication.shared.open(furl, options: [:])
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM