如何打开下载的文件

How to open a downloaded file

我正在为 Xamarin.iOS 编写一个应用程序,但用原生 iOS 回答就足够了。下载文件(图片或 pdf)后,我想打开它:

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

我尝试了 UIApplication.SharedApplication.OpenUrl(location); 但没有任何反应。

不知道你是否介意使用WKWebView to load the local file, and if you store the file in Library/Caches/ of Application directories,你可以使用以下方式加载下载的文件。

获取cache路径:

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

WKWebView中加载tree.png:

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

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

这是输出:

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

及效果: