xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.62k stars 1.87k forks source link

[iOS] How to set BaseUrl from WKWebview to use local file like image, css, javascript #4396

Closed kritsadadechawantana closed 2 years ago

kritsadadechawantana commented 5 years ago

Description

i can't use local image reference in html with WKWebview

Steps to Reproduce

  1. set up webview source
var source = new HtmlWebViewSource()
            {
                Html = $@"
<html>
  <head>
    <title>Xamarin Forms</title>
  </head>
  <body>
    <h1>Xamrin.Forms</h1>
    <p>This is an iOS web page.</p>
    <img src=""ob03.png"" />
  </body>
</html>
                "
            };

source.BaseUrl = DependencyService.Get<IBaseUrl>().Get(); // this operation return contained image files "/var/mobile/Containers/Data/Application/BBD4FB76-852B-4BC8-BACF-A1DEAFE31C1F/Documents/mcontent/images/"
webview.Source = source;

Expected Behavior

i need to display local images from local path like this "/var/mobile/Containers/Data/Application/BBD4FB76-852B-4BC8-BACF-A1DEAFE31C1F/Documents/mcontent/images/"

Nico04 commented 5 years ago

I have the same need here. I found a temp fix while an official one is released :

In order for WkWebView to work with local content, we need to : 1/ Set the private api allowFileAccessFromFileURLs 2/ Save html file to app's Library or Documents folder (or subfolder). 3/ Set baseUrl to the html's containing folder 4/ Put all resources needed (pictures, thumbnails, assets) in the html's containing folder or subfolder. 5/ Use WkWebView.LoadFileUrl(uri, baseUri)

Note : BundlePath is accessible but readonly

jfversluis commented 2 years ago

Thanks for this suggestion! As Xamarin.Forms is now in maintenance mode, this will not happen anymore for Xamarin.Forms. We're only adding bugfixes and stability fixes.

If this is still important to you, make sure to check the .NET MAUI repo and see if it's already on the roadmap. If not, feel free to open a discussion to discuss a change first or open an issue with a detailed feature request. Thanks!