I have a WebView in a MAUI app that generally works, but whenever I click a link on iOS that is supposed to download a file (link returns a Content-Disposition header) it is opened in the WebView. I would like it to be downloaded (and opened in the default iOS app) instead.
How is this supposed to be implemented? There is apparently a IWKDownloadDelegate
interface with a DecideDestination()
method but I can't find examples of how to wire it all up in MAUI. I got it working on Android by writing some platform-specific code, and I imagine something similar can be done for iOS.
<WebView
x:Name="WebView"
Source=".." />
public partial class WebClientPage : ContentPage
{
public WebClientPage()
{
InitializeComponent();
}
protected override void OnHandlerChanged()
{
base.OnHandlerChanged();
#if IOS
var iosWebView = WebView.Handler.PlatformView as WebKit.WKWebView;
// Otherwise swiping doesn't work
iosWebView.AllowsBackForwardNavigationGestures = true;
#endif
}
}
Related question for Android: Download files in MAUI Android WebView
You can download files on iOS by handling the
DecideDestination
event of theWKDownloadDelegate
. Also, you need to implementIWKDownloadDelegate
andIWKNavigationDelegate
and override the default response to show the downloaded file. Here's a solution provided by Tim for your reference: