Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit d5f2d35

Browse files
authored
Reinstate loading of local HTML files in WKWebViewRenderer (#12029)
* Allow loading of local HTML files in WKWebViewRenderer; makes test 32033 work again * Revert whitespace changes * Add missing using directive
1 parent 1c88203 commit d5f2d35

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

Xamarin.Forms.Platform.iOS/Renderers/WkWebViewRenderer.cs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.ComponentModel;
44
using System.Diagnostics;
55
using System.Drawing;
6+
using System.IO;
67
using System.Linq;
78
using System.Net;
89
using System.Text;
@@ -104,17 +105,55 @@ public async void LoadUrl(string url)
104105
try
105106
{
106107
var uri = new Uri(url);
108+
107109
var safeHostUri = new Uri($"{uri.Scheme}://{uri.Authority}", UriKind.Absolute);
108110
var safeRelativeUri = new Uri($"{uri.PathAndQuery}{uri.Fragment}", UriKind.Relative);
109111
NSUrlRequest request = new NSUrlRequest(new Uri(safeHostUri, safeRelativeUri));
110112

111113
await SyncNativeCookies(url);
112114
LoadRequest(request);
113115
}
116+
catch (UriFormatException formatException)
117+
{
118+
// If we got a format exception trying to parse the URI, it might be because
119+
// someone is passing in a local bundled file page. If we can find a better way
120+
// to detect that scenario, we should use it; until then, we'll fall back to
121+
// local file loading here and see if that works:
122+
if (!LoadFile(url))
123+
{
124+
Log.Warning(nameof(WkWebViewRenderer), $"Unable to Load Url {url}: {formatException}");
125+
}
126+
}
114127
catch (Exception exc)
115128
{
116-
Log.Warning(nameof(WkWebViewRenderer), $"Unable to Load Url {exc}");
129+
Log.Warning(nameof(WkWebViewRenderer), $"Unable to Load Url {url}: {exc}");
130+
}
131+
}
132+
133+
bool LoadFile(string url)
134+
{
135+
try
136+
{
137+
var file = Path.GetFileNameWithoutExtension(url);
138+
var ext = Path.GetExtension(url);
139+
140+
var nsUrl = NSBundle.MainBundle.GetUrlForResource(file, ext);
141+
142+
if (nsUrl == null)
143+
{
144+
return false;
145+
}
146+
147+
LoadFileUrl(nsUrl, nsUrl);
148+
149+
return true;
150+
}
151+
catch (Exception ex)
152+
{
153+
Log.Warning(nameof(WkWebViewRenderer), $"Could not load {url} as local file: {ex}");
117154
}
155+
156+
return false;
118157
}
119158

120159
public override void LayoutSubviews()

0 commit comments

Comments
 (0)