Replies: 6 comments
-
Hi @weedcry. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time. |
Beta Was this translation helpful? Give feedback.
-
Hi, @weedcry - Are you seeing this working on Android or any other platform? The behavior you're describing sounds like it may be what is expected |
Beta Was this translation helpful? Give feedback.
-
Hi @rachelkang |
Beta Was this translation helpful? Give feedback.
-
I tried to save the file using the path obtained through the following method. However, I received an error with "The operation couldn’t be completed. (NSURLErrorDomain error -3000.)". Can anyone help me? public override void DecideDestination(WKDownload download, NSUrlResponse response, string suggestedFilename, Action<NSUrl> completionHandler)
{
var fileManager = NSFileManager.DefaultManager;
var documentDirectory = fileManager.GetUrls(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User)[0];
var fileUrl = documentDirectory.Append(suggestedFilename, false);
completionHandler(fileUrl);
}
public override void DidFinish(WKDownload download)
{
}
public override void DidFail(WKDownload download, NSError error, NSData? resumeData)
{
} |
Beta Was this translation helpful? Give feedback.
-
I have solved the issue above. But I can only save the file to the data of a third-party app. var fileManager = NSFileManager.DefaultManager;
var documentDirectory = fileManager.GetUrls(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.All)[0]; I want to save the file to the Downloads folder of iOS but I can't figure out how to get its path. |
Beta Was this translation helpful? Give feedback.
-
This is not a MAUI bug. This is the default behavior of As you've seen, the default behavior is to show it inside the webview, and you have to add the handlers to the underlying WKWebView to change that. This is a pain to do in Swift/ObjC and needs to be better documented there, and it's worse for C#/Xamarin. https://github.com/drasticactions/MauiRepros/tree/main/WebviewTestCatalyst2 I've implemented a solution that should work. You need to implement public class TestDownloadWebView : WKWebView, IWKDownloadDelegate, IWKNavigationDelegate
{
public TestDownloadWebView(CGRect frame, WKWebViewConfiguration configuration) : base(frame, configuration)
{
this.NavigationDelegate = this;
}
public void DecideDestination(WKDownload download, NSUrlResponse response, string suggestedFilename,
Action<NSUrl> completionHandler)
{
var destinationURL = GetDestinationURL();
completionHandler?.Invoke(destinationURL);
}
[Export("webView:decidePolicyForNavigationResponse:decisionHandler:")]
public void DecidePolicy(WKWebView webView, WKNavigationResponse navigationResponse, Action<WKNavigationResponsePolicy> decisionHandler)
{
var url = navigationResponse.Response.Url;
var mimeType = navigationResponse.Response.MimeType;
Console.WriteLine($"Content-Type: {mimeType}");
// Perform any actions based on the content type
if (mimeType == "application/pdf")
{
// Download the PDF file separately instead of loading it in the WKWebView
DownloadPDF(url);
decisionHandler?.Invoke(WKNavigationResponsePolicy.Cancel);
}
else
{
decisionHandler?.Invoke(WKNavigationResponsePolicy.Allow);
}
}
private void DownloadPDF(NSUrl url)
{
var downloadTask = NSUrlSession.SharedSession.CreateDownloadTask(url, (location, _, error) =>
{
if (location is NSUrl sourceURL && error == null)
{
var destinationURL = GetDestinationURL();
try
{
NSFileManager.DefaultManager.Move(sourceURL, destinationURL, out error);
Console.WriteLine($"PDF file downloaded and saved at: {destinationURL.Path}");
// Perform any additional actions with the downloaded file
}
catch (Exception ex)
{
// Handle file moving error
}
}
else
{
// Handle download error
}
});
downloadTask.Resume();
}
private NSUrl GetDestinationURL()
{
// Customize the destination URL as desired
var documentsURL =
NSFileManager.DefaultManager.GetUrls(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User)
[0];
var destinationURL = documentsURL.Append("downloaded_file.pdf", false);
return destinationURL;
}
} There are other ways beyond this one that should work as well. This has more to do with the weird APIs of WKWebView than anything specific in MAUI. Again, unless there is some kind of new API introduced to MAUI for how to handle downloads of WebViews, it's going to be onto whatever the platform view itself does, which results in weird stuff like this. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Description
I am using the webview on iOS to load a URL. I want to download a file using the webview, but when I try to download it, the webview just open it without downloading
Steps to Reproduce
Link to public reproduction project repository
https://github.com/dotnet/maui/files/11550334/WebviewExample.zip
Version with bug
7.0.49
Last version that worked well
Unknown/Other
Affected platforms
iOS
Affected platform versions
iOS 16.2
Did you find any workaround?
I haven't found
Relevant log output
No response
Beta Was this translation helpful? Give feedback.
All reactions