From f847dfe04b06dee6886e259c14fddb48a38980d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Sua=CC=81rez?= Date: Thu, 20 Oct 2022 13:32:22 +0200 Subject: [PATCH] Fix issue loading local files in iOS WebView --- src/Core/src/Platform/iOS/MauiWKWebView.cs | 64 ++++++++++++++++++++-- 1 file changed, 58 insertions(+), 6 deletions(-) diff --git a/src/Core/src/Platform/iOS/MauiWKWebView.cs b/src/Core/src/Platform/iOS/MauiWKWebView.cs index eda616c05ed6..928f938be49a 100644 --- a/src/Core/src/Platform/iOS/MauiWKWebView.cs +++ b/src/Core/src/Platform/iOS/MauiWKWebView.cs @@ -1,10 +1,12 @@ using System; using System.Drawing; +using System.IO; using System.Threading.Tasks; using CoreGraphics; using Foundation; using UIKit; using WebKit; +using Microsoft.Extensions.Logging; namespace Microsoft.Maui.Platform { @@ -89,13 +91,36 @@ public void LoadHtml(string? html, string? baseUrl) } public void LoadUrl(string? url) - { - var uri = new Uri(url ?? string.Empty); - var safeHostUri = new Uri($"{uri.Scheme}://{uri.Authority}", UriKind.Absolute); - var safeRelativeUri = new Uri($"{uri.PathAndQuery}{uri.Fragment}", UriKind.Relative); - NSUrlRequest request = new NSUrlRequest(new NSUrl(new Uri(safeHostUri, safeRelativeUri).AbsoluteUri)); + { + try + { + var uri = new Uri(url ?? string.Empty); + var safeHostUri = new Uri($"{uri.Scheme}://{uri.Authority}", UriKind.Absolute); + var safeRelativeUri = new Uri($"{uri.PathAndQuery}{uri.Fragment}", UriKind.Relative); + NSUrlRequest request = new NSUrlRequest(new NSUrl(new Uri(safeHostUri, safeRelativeUri).AbsoluteUri)); - LoadRequest(request); + LoadRequest(request); + } + catch (UriFormatException formatException) + { + // If we got a format exception trying to parse the URI, it might be because + // someone is passing in a local bundled file page. If we can find a better way + // to detect that scenario, we should use it; until then, we'll fall back to + // local file loading here and see if that works: + if(!string.IsNullOrEmpty(url)) + { + if (!LoadFile(url)) + { + if (_handler.TryGetTarget(out var handler)) + handler.MauiContext?.CreateLogger()?.LogWarning(nameof(MauiWKWebView), $"Unable to Load Url {url}: {formatException}"); + } + } + } + catch (Exception exc) + { + if (_handler.TryGetTarget(out var handler)) + handler.MauiContext?.CreateLogger()?.LogWarning(nameof(MauiWKWebView), $"Unable to Load Url {url}: {exc}"); + } } // https://developer.apple.com/forums/thread/99674 @@ -114,5 +139,32 @@ public static WKWebViewConfiguration CreateConfiguration() return config; } + + bool LoadFile(string url) + { + try + { + var file = Path.GetFileNameWithoutExtension(url); + var ext = Path.GetExtension(url); + + var nsUrl = NSBundle.MainBundle.GetUrlForResource(file, ext); + + if (nsUrl == null) + { + return false; + } + + LoadFileUrl(nsUrl, nsUrl); + + return true; + } + catch (Exception ex) + { + if (_handler.TryGetTarget(out var handler)) + handler.MauiContext?.CreateLogger()?.LogWarning(nameof(MauiWKWebView), $"Could not load {url} as local file: {ex}"); + } + + return false; + } } } \ No newline at end of file