Problems rendering HTML for iOS physical device when updating to WKWebView

Anil Kakar 6 Reputation points
2021-03-19T19:16:33.043+00:00

When I'm attempting to render HTML in a WKWebView using a custom renderer in iOS, the CSS and images aren't rendered when I use;

public void LoadUrl(string url)
{
var uri = new Uri(url);
var safeHostUri = new Uri($"{uri.Scheme}://{uri.Authority}", UriKind.Absolute);
var safeRelativeUri = new Uri($"{uri.PathAndQuery}{uri.Fragment}", UriKind.Relative);
LoadRequest(new NSUrlRequest(new Uri(safeHostUri, safeRelativeUri)));
}

or;

public void LoadUrl(string url)
 {
 var stringHtml = File.ReadAllText(url, Encoding.UTF8);
 var baseUrl = Path.GetDirectoryName(url);
LoadHtmlString(stringHtml, baseUrl == null ? new NSUrl(NSBundle.MainBundle.BundlePath, true) : new NSUrl(baseUrl, true));
 }

There is no problem in Android or in iOS emulators. Any help is greatly appreciated!

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

1 answer

Sort by: Most helpful
  1. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,751 Reputation points
    2021-03-22T08:18:53.673+00:00

    Welcome to Microsoft Q&A!

    Try to use WKWebView.LoadFileUrl instead of LoadHtmlString() to access local html file.

    Refer to https://stackoverflow.com/a/28676439/8187800.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our [documentation][2] to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments