简体   繁体   中英

How to open a downloaded file

I am writing an application for Xamarin.iOS, but an answer in native iOS would suffice. After I download a file (image or pdf) I want to open it:

public void DidFinishDownloading(NSUrlSession session, NSUrlSessionDownloadTask downloadTask, NSUrl location)
{
    // how to open location?
}

I tried UIApplication.SharedApplication.OpenUrl(location); but nothing happens.

I'm not sure whether you mind using WKWebView to load the local file, and if you store the file in Library/Caches/ of Application directories , you could use follow ways to load the downloaded file.

Get the cache path:

var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
cache = Path.Combine(documents, "..", "Library", "Caches");
Console.WriteLine("path is : "+cache);

Load a tree.png in WKWebView :

 webView = new WKWebView(View.Frame, new WKWebViewConfiguration());
 View.AddSubview(webView);

 NSUrl nSUrl = new NSUrl("file://" + cache + "/tree.png");
 webView.LoadFileUrl(nSUrl, nSUrl);

Here is the output:

2020-10-28 10:27:47.303745+0800 IOSSplashScreen[4370:73093] path is : /Users/xxx/Library/Developer/CoreSimulator/Devices/C30029F0-379A-4255-B38B-27E2BAAC8CC1/data/Containers/Data/Application/5C7D7CA5-7394-414A-897F-8F1A278838A8/Documents/../Library/Caches

And effect:

在此处输入图片说明

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