Skip to content

Instantly share code, notes, and snippets.

@tiagocrizanto
Created February 3, 2019 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiagocrizanto/5500b6f7b98276e3f727960d523ddb13 to your computer and use it in GitHub Desktop.
Save tiagocrizanto/5500b6f7b98276e3f727960d523ddb13 to your computer and use it in GitHub Desktop.
[assembly: ExportRenderer(typeof(HybridWebView), typeof(HybridWebViewRenderer))]
namespace ProxyWebView.iOS
{
public class HybridWebViewRenderer : ViewRenderer<HybridWebView, WKWebView>
{
WKUserContentController userController;
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
var formsWebView = sender as WebView;
if (formsWebView != null)
{
userController = new WKUserContentController();
var config = new WKWebViewConfiguration { UserContentController = userController };
var webView = new WKWebView(Frame, config);
SetNativeControl(webView);
Control.AllowsBackForwardNavigationGestures = true;
Control.AllowsLinkPreview = true;
Control.NavigationDelegate = new CustomWebViewClient(Element);
if ((formsWebView.Source as UrlWebViewSource) != null)
{
string url = System.Web.HttpUtility.UrlPathEncode((formsWebView.Source as UrlWebViewSource).Url);
Control.LoadRequest(new NSUrlRequest(new NSUrl(url)));
}
else if ((formsWebView.Source as HtmlWebViewSource) != null)
{
if (!string.IsNullOrEmpty((formsWebView.Source as HtmlWebViewSource).BaseUrl))
Control.LoadHtmlString((formsWebView.Source as HtmlWebViewSource).Html, new NSUrl(System.Web.HttpUtility.UrlPathEncode((formsWebView.Source as HtmlWebViewSource).BaseUrl)));
else
//Option to load local HTML
Control.LoadHtmlString((formsWebView.Source as HtmlWebViewSource).Html, NSUrl.CreateFileUrl(new[] { NSBundle.MainBundle.BundlePath }));
}
}
}
public class CustomWebViewClient : WKNavigationDelegate, INSUrlConnectionDataDelegate
{
private HybridWebView _webclient;
private WKWebView _webView;
public CustomWebViewClient(HybridWebView webclient)
{
_webclient = webclient;
}
public override void DidReceiveAuthenticationChallenge(WKWebView webView, NSUrlAuthenticationChallenge challenge, Action<NSUrlSessionAuthChallengeDisposition, NSUrlCredential> completionHandler)
{
var crendential = new NSUrlCredential(_webclient.Username, _webclient.Password, NSUrlCredentialPersistence.ForSession);
completionHandler(NSUrlSessionAuthChallengeDisposition.UseCredential, crendential);
}
public override void DidStartProvisionalNavigation(WKWebView webView, WKNavigation navigation)
{
//if you need store the navigation history you can do it in this method
// e.g: _webclient.History.Add(webView.Url.AbsoluteString);
//_webclient is an instance of HybridWebView and History is a list of string in HybridWebView class
}
public override void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
{
//If the page don't allow zoom, insert metatag to allow it
string allowZoom = @"javascript:
var newMeta = document.createElement('meta');
newMeta.setAttribute('name', 'viewport');
newMeta.setAttribute('content', 'user-scalable=yes, width=device-width');
document.getElementsByTagName('head')[0].appendChild(newMeta);";
webView.EvaluateJavaScript(allowZoom, null);
_webView = webView;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment