WebKit WebView - Customizing Xamarin Forms WebView in Xamarin.Mac - Assigning a download delegate

noobsoftware 251 Reputation points

I'm trying to assign a download delegate to download files in WebKit.WebView in Xamarin Mac and the file calls DidFinish method of my download delegate but the file does not appear in download folder and i would like to be able to call this method first:

public override void DecideDestination(WKDownload download, NSUrlResponse response, string suggestedFilename, Action<NSUrl> completionHandler)
string url = "/Users/siggi/Downloads/"+suggestedFilename;
completionHandler(new NSUrl(url));

And instead download the file to a specific location.

Any help is appreciated.

Also I'm a bit confused about the difference betwen WKWebView and WebKit.WebView since the object that appears in the DidFinish call is marked as WKDownload in my WKDownloadDelegate but appears in the debug as WebKit.WebDownload and i'm unable to cast between these to object types.

A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,159 questions
0 comments No comments
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 22,496 Reputation points Microsoft Vendor

    Hello @noobsoftware ,

    WebKit.WebView is no longer supported, you could try WKWebView instead. For more details, you can refer to this doc - Replacing UIWebView in your app | Apple Developer Documentation.
    As described in the doc, WKWebView uses various delegates to implement functionality that’s similar to UIWebViewDelegate. You could check the WKWebView equivalent methods in the WKNavigationDelegate.

    After that, you can use WKDownloadDelegate , it's for macOS 11.3 and later. For more details, you can see: WKDownloadDelegate | Apple Developer Documentation

    Best Regards,
    Wenyan Zhang

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

0 additional answers

Sort by: Most helpful