-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEnhancedWebViewRenderer.cs
More file actions
96 lines (85 loc) · 3.6 KB
/
EnhancedWebViewRenderer.cs
File metadata and controls
96 lines (85 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Drawing;
using CoreGraphics;
using Foundation;
using Plugin.XF.Controls.iOS.Renderer;
using Plugin.XF.Controls.Controls;
using UIKit;
using WebKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(EnhancedWebView), typeof(EnhancedWebViewRenderer))]
namespace Plugin.XF.Controls.iOS.Renderer
{
public partial class WebViewDelegate : WKNavigationDelegate, INSUrlConnectionDataDelegate
{
public string Username { get; set; }
public string Password { get; set; }
public event Action<string> LoadCompleted;
public event Action<string> LoadError;
public override void DidReceiveAuthenticationChallenge(WKWebView webView, NSUrlAuthenticationChallenge challenge, Action<NSUrlSessionAuthChallengeDisposition, NSUrlCredential> completionHandler)
{
//base.DidReceiveAuthenticationChallenge(webView, challenge, completionHandler);
completionHandler(NSUrlSessionAuthChallengeDisposition.UseCredential, new NSUrlCredential(Username, Password, NSUrlCredentialPersistence.ForSession));
return;
}
public override void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
{
LoadCompleted?.Invoke(webView.Url.ToString());
}
public override void DidFailNavigation(WKWebView webView, WKNavigation navigation, NSError error)
{
LoadError?.Invoke(error.DebugDescription);
}
}
public class EnhancedWebViewRenderer : ViewRenderer<EnhancedWebView, WKWebView>
{
public static void Init() { }
protected override void OnElementChanged(ElementChangedEventArgs<EnhancedWebView> e)
{
base.OnElementChanged(e);
var webView = Control as WKWebView;
if (webView == null)
{
var config = new WKWebViewConfiguration();
webView = new WKWebView(Frame, config);
SetNativeControl(webView);
}
if (e.OldElement != null)
{
}
if (e.NewElement != null)
{
UrlWebViewSource source = (Xamarin.Forms.UrlWebViewSource)Element.Source;
var webRequest = new NSMutableUrlRequest(new NSUrl(source.Url));
if (Element.CustomHeaders != null) {
if (Element.CustomHeaders.Count > 0)
{
foreach (string key in Element.CustomHeaders.Keys)
webRequest[key] = Element.CustomHeaders[key];
}
}
if (!string.IsNullOrEmpty(Element.Username) && !string.IsNullOrEmpty(Element.Password))
{
WebViewDelegate webViewDelegate = new WebViewDelegate();
webViewDelegate.Username = Element.Username;
webViewDelegate.Password = Element.Password;
webViewDelegate.LoadCompleted += (url) =>
{
Element.TriggerEnhancedWebViewLoadCompleted(url);
};
webViewDelegate.LoadError += (errorMsg) =>
{
Element.TriggerEnhancedWebViewLoadError(errorMsg);
};
webView.NavigationDelegate = webViewDelegate;
Element.RefreshPageAction = new Action(() =>
{
webView.Reload();
});
}
Control.LoadRequest(webRequest);
}
}
}
}