|
3 | 3 | using System.ComponentModel;
|
4 | 4 | using System.Diagnostics;
|
5 | 5 | using System.Drawing;
|
| 6 | +using System.IO; |
6 | 7 | using System.Linq;
|
7 | 8 | using System.Net;
|
8 | 9 | using System.Text;
|
@@ -104,17 +105,55 @@ public async void LoadUrl(string url)
|
104 | 105 | try
|
105 | 106 | {
|
106 | 107 | var uri = new Uri(url);
|
| 108 | + |
107 | 109 | var safeHostUri = new Uri($"{uri.Scheme}://{uri.Authority}", UriKind.Absolute);
|
108 | 110 | var safeRelativeUri = new Uri($"{uri.PathAndQuery}{uri.Fragment}", UriKind.Relative);
|
109 | 111 | NSUrlRequest request = new NSUrlRequest(new Uri(safeHostUri, safeRelativeUri));
|
110 | 112 |
|
111 | 113 | await SyncNativeCookies(url);
|
112 | 114 | LoadRequest(request);
|
113 | 115 | }
|
| 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 | + } |
114 | 127 | catch (Exception exc)
|
115 | 128 | {
|
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}"); |
117 | 154 | }
|
| 155 | + |
| 156 | + return false; |
118 | 157 | }
|
119 | 158 |
|
120 | 159 | public override void LayoutSubviews()
|
|
0 commit comments