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

noobsoftware 251 Reputation points
2022-09-09T17:31:08.917+00:00

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)
{
System.Diagnostics.Debug.WriteLine("download");
System.Diagnostics.Debug.WriteLine(response);
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.

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

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 30,746 Reputation points Microsoft Vendor
    2022-09-12T09:13:16.787+00:00

    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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.