protected override void OnElementChanged(ElementChangedEventArgs<HybridWebView> e) { base.OnElementChanged(e); if (Control == null) { userController = new WKUserContentController(); var config = new WKWebViewConfiguration { UserContentController = userController }; var webView = new WKWebView(Frame, config); var script = new WKUserScript(new NSString(JavaScriptFunction), WKUserScriptInjectionTime.AtDocumentEnd, false); userController.AddUserScript(script); userController.AddScriptMessageHandler(this, "invokeAction"); SetNativeControl(webView); if (e.OldElement != null) { userController.RemoveAllUserScripts(); userController.RemoveScriptMessageHandler("invokeAction"); var hybridWebView = e.OldElement as HybridWebView; hybridWebView.Cleanup(); } if (e.NewElement != null) { var a = this.Control.Url; Control.AccessibilityViewIsModal = true; NSMutableUrlRequest sendData = new NSMutableUrlRequest(new NSUrl(Element.Uri)); Control.LoadRequest(sendData); } } }
protected override void OnElementChanged(ElementChangedEventArgs <HybridWebView> e) { if (Control == null) { userController = new WKUserContentController(); var script = new WKUserScript(new NSString(JavaScriptFunction), WKUserScriptInjectionTime.AtDocumentStart, false); userController.AddUserScript(script); userController.AddScriptMessageHandler(this, "invokeNativeCode"); var config = new WKWebViewConfiguration { UserContentController = userController }; var webView = new WKWebView(Frame, config); SetNativeControl(webView); } if (e.OldElement != null) { userController.RemoveAllUserScripts(); userController.RemoveScriptMessageHandler("invokeNativeCode"); var hybridWebView = e.OldElement as HybridWebView; hybridWebView.Cleanup(); } if (e.NewElement != null) { string fileName = Path.Combine(NSBundle.MainBundle.BundlePath, string.Format("Content/{0}", Element.Uri)); Control.LoadRequest(new NSUrlRequest(new NSUrl(fileName, false))); (e.NewElement as HybridWebView).NativeEvent += HybridWebViewRenderer_NativeEvent; //var fileContents = File.ReadAllText(fileName); //Control.LoadHtmlString(fileContents, new NSUrl(NSBundle.MainBundle.BundlePath + "/Content", true)); } }
private void PhoneNumberLogin() { string javaScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);"; WKUserScript wkUScript = new WKUserScript((NSString)javaScript, WKUserScriptInjectionTime.AtDocumentEnd, true); WKUserContentController wkUController = new WKUserContentController(); wkUController.AddUserScript(wkUScript); WKWebViewConfiguration wkWebConfig = new WKWebViewConfiguration(); wkWebConfig.UserContentController = wkUController; WKWebView webView = new WKWebView(View.Frame, wkWebConfig); View.AddSubview(webView); webView.AllowsBackForwardNavigationGestures = true; var url = new NSUrl("http://34.216.71.66/Plaid/Plaid"); var request = new NSUrlRequest(url); webView.LoadRequest(request); }
protected override void OnElementChanged(ElementChangedEventArgs <ExtendedWebView> e) { try { view = (ExtendedWebView)Element; string url = view.Source.GetValue(UrlWebViewSource.UrlProperty).ToString(); //The width=device-width part sets the width of the page to follow the screen-width of the device //(which will vary depending on the device) string javaScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);"; WKUserScript wkUScript = new WKUserScript((NSString)javaScript, WKUserScriptInjectionTime.AtDocumentEnd, true); WKUserContentController wkUController = new WKUserContentController(); wkUController.AddUserScript(wkUScript); WKWebViewConfiguration wkWebConfig = new WKWebViewConfiguration(); wkWebConfig.UserContentController = wkUController; _wkWebView = new WKWebView(Frame, wkWebConfig); _wkWebView.ScrollView.ScrollEnabled = true; _wkWebView.NavigationDelegate = new HybridWebviewDelegate(); _wkWebView.LoadRequest(new NSUrlRequest(new NSUrl(url))); SetNativeControl(_wkWebView); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
protected override void OnElementChanged(ElementChangedEventArgs <HybridWebView> e) { base.OnElementChanged(e); if (Control == null) { userController = new WKUserContentController(); var script = new WKUserScript(new NSString(JavaScriptFunction), WKUserScriptInjectionTime.AtDocumentEnd, true); var api = new WKUserScript(new NSString(Element.APIJS), WKUserScriptInjectionTime.AtDocumentStart, true); // var apiSetup = new WKUserScript(new NSString(APISetup), WKUserScriptInjectionTime.AtDocumentStart, true); userController.AddUserScript(script); userController.AddUserScript(api); // userController.AddUserScript(apiSetup); userController.AddScriptMessageHandler(this, "invokeAction"); var config = new WKWebViewConfiguration { UserContentController = userController }; WKJavascriptEvaluationResult handler = (NSObject result, NSError err) => { if (err != null) { System.Console.WriteLine(err); } if (result != null) { System.Console.WriteLine(result); } }; var webView = new WKWebView(Frame, config); // var js = (NSString)"SetupAPI()"; //webView.EvaluateJavaScript(js, null); webView.Configuration.Preferences.SetValueForKey(NSObject.FromObject(true), new NSString("allowFileAccessFromFileURLs")); webView.CustomUserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Mobile/15E148 Safari/604.1"; webView.SizeToFit(); SetNativeControl(webView); } if (e.OldElement != null) { userController.RemoveAllUserScripts(); userController.RemoveScriptMessageHandler("invokeAction"); userController.RemoveScriptMessageHandler("Terminate"); userController.RemoveScriptMessageHandler("Commit"); var hybridWebView = e.OldElement as HybridWebView; hybridWebView.Cleanup(); } if (e.NewElement != null) { var docsDir = NSFileManager.DefaultManager.GetUrl(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User, null, true, out var error); var data = NSUrl.FromFilename(Path.Combine(docsDir.Path, Element.iOSPath)); var frame = NSUrl.FromFilename(Path.Combine(docsDir.Path, "Courses/index.htm")); var htmlString = new NSString(Element.Source); var content = NSUrl.FromFilename(Path.Combine(docsDir.Path, "Courses")); var contentFrame = NSUrl.FromFilename(Path.Combine(docsDir.Path)); Control.LoadFileUrl(data, content); } }
/// <summary> /// Produces PNG from HTML /// </summary> /// <param name="taskCompletionSource"></param> /// <param name="html"></param> /// <param name="fileName"></param> public void ToPng(TaskCompletionSource <ToFileResult> taskCompletionSource, string html, string fileName, int width) { if (NSProcessInfo.ProcessInfo.IsOperatingSystemAtLeastVersion(new NSOperatingSystemVersion(11, 0, 0))) { string jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);"; WKUserScript wkUScript = new WKUserScript((NSString)jScript, WKUserScriptInjectionTime.AtDocumentEnd, true); WKUserContentController wkUController = new WKUserContentController(); wkUController.AddUserScript(wkUScript); var configuration = new WKWebViewConfiguration { UserContentController = wkUController }; var webView = new WKWebView(new CGRect(0, 0, width, width), configuration) { UserInteractionEnabled = false, BackgroundColor = UIColor.White }; webView.NavigationDelegate = new WKNavigationCompleteCallback(fileName, new WebViewToPngSize(width), null, taskCompletionSource, NavigationComplete); webView.LoadHtmlString(html, null); } else { taskCompletionSource.SetResult(new ToFileResult(true, "PNG output not available prior to iOS 11")); } }
public async void LoadUrl(string url) { var uri = new Uri(url); 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 Uri(safeHostUri, safeRelativeUri)); if (WebView.Cookies != null) { if (Forms.IsiOS11OrNewer) { var existingCookies = await Configuration.WebsiteDataStore.HttpCookieStore.GetAllCookiesAsync(); foreach (var cookie in existingCookies) { await Configuration.WebsiteDataStore.HttpCookieStore.DeleteCookieAsync(cookie); } var jCookies = WebView.Cookies.GetCookies(uri); foreach (System.Net.Cookie jCookie in jCookies) { await Configuration.WebsiteDataStore.HttpCookieStore.SetCookieAsync(new NSHttpCookie(jCookie)); } } else if (WebView.Cookies.Count > 0) { WKUserScript wKUserScript = new WKUserScript(new NSString(GetCookieString(uri)), WKUserScriptInjectionTime.AtDocumentStart, false); Configuration.UserContentController.AddUserScript(wKUserScript); } } LoadRequest(request); }
protected override void OnElementChanged(ElementChangedEventArgs <HybridWebView> e) { base.OnElementChanged(e); if (Control == null) { _userController = new WKUserContentController(); var script = new WKUserScript(new NSString(JSFunction), WKUserScriptInjectionTime.AtDocumentEnd, false); _userController.AddUserScript(script); _userController.AddScriptMessageHandler(this, "invokeAction"); var config = new WKWebViewConfiguration { UserContentController = _userController }; var webView = new WKWebView(Frame, config); SetNativeControl(webView); } if (e.OldElement != null) { _userController.RemoveAllUserScripts(); _userController.RemoveScriptMessageHandler("invokeAction"); var hybridWebView = e.OldElement as HybridWebView; hybridWebView.Cleanup(); } if (e.NewElement != null) { Control.LoadRequest(new NSUrlRequest(new NSUrl(Element.Uri))); } }
protected override void OnElementChanged(ElementChangedEventArgs <HybridWebView> e) { base.OnElementChanged(e); if (Control == null) { userController = new WKUserContentController(); var script = new WKUserScript(new NSString(JavaScriptFunction), WKUserScriptInjectionTime.AtDocumentEnd, false); userController.AddUserScript(script); userController.AddScriptMessageHandler(this, "invokeAction"); var config = new WKWebViewConfiguration { UserContentController = userController }; var webView = new WKWebView(Frame, config); SetNativeControl(webView); } if (e.OldElement != null) { userController.RemoveAllUserScripts(); userController.RemoveScriptMessageHandler("invokeAction"); var hybridWebView = e.OldElement as HybridWebView; hybridWebView.Cleanup(); } if (e.NewElement != null) { string fileName = Path.Combine(NSBundle.MainBundle.BundlePath, string.Format("Content/{0}", Element.Uri)); Control.LoadRequest(new NSUrlRequest(new NSUrl(fileName, false))); } }
protected override void OnElementChanged(ElementChangedEventArgs <CustomWebView> e) { base.OnElementChanged(e); if (Control == null) { userController = new WKUserContentController(); var script = new WKUserScript(new NSString(JavaScriptFunction), WKUserScriptInjectionTime.AtDocumentEnd, false); userController.AddUserScript(script); userController.AddScriptMessageHandler(this, "invokeAction"); var config = new WKWebViewConfiguration { UserContentController = userController }; var webView = new WKWebView(Frame, config); SetNativeControl(webView); } if (e.OldElement != null) { userController.RemoveAllUserScripts(); userController.RemoveScriptMessageHandler("invokeAction"); var customWebView = e.OldElement as CustomWebView; customWebView.Cleanup(); } if (e.NewElement != null) { System.Diagnostics.Debug.WriteLine(Element.Uri); Control.LoadRequest(new NSUrlRequest(new NSUrl(Element.Uri))); } }
protected override void OnElementChanged(ElementChangedEventArgs <GalleyBrowserControl> e) { base.OnElementChanged(e); if (Control == null) { _userController = new WKUserContentController(); var script = new WKUserScript(new NSString(JavaScriptFunction), WKUserScriptInjectionTime.AtDocumentEnd, false); _userController.AddUserScript(script); _userController.AddScriptMessageHandler(this, GalleyHtmlConstants.InvokeSharpCodeActionName); var config = new WKWebViewConfiguration { UserContentController = _userController }; var webView = new WKWebView(Frame, config) { NavigationDelegate = new CustomWKNavigationDelegate() }; SetNativeControl(webView); OnElementPropertyChanged(nameof(Element.Uri)); OnElementPropertyChanged(nameof(Element.Html)); OnJavaScriptInvoked(Element.JavaScriptFunction); } if (e.OldElement != null) { _userController.RemoveAllUserScripts(); _userController.RemoveScriptMessageHandler(GalleyHtmlConstants.InvokeSharpCodeActionName); e.OldElement.JavaScriptInvoked -= OnJavaScriptInvoked; } if (e.NewElement != null) { e.NewElement.JavaScriptInvoked += OnJavaScriptInvoked; } }
protected override void OnElementChanged(ElementChangedEventArgs <HybridWebPlatformView> e) { base.OnElementChanged(e); if (Control == null && e.NewElement != null) { userController = new WKUserContentController(); var config = new WKWebViewConfiguration() { UserContentController = userController }; var script = new WKUserScript(new NSString(NativeFunction + HybridWebPlatformView.GetInitialJsScript(NativeFuncCall)), WKUserScriptInjectionTime.AtDocumentEnd, false); userController.AddUserScript(script); userController.AddScriptMessageHandler(this, "native"); var webView = new WKWebView(Frame, config) { NavigationDelegate = CreateNavidationalDelagate(), UIDelegate = new HybridWebPlatformUIDelegate() }; webView.Opaque = false; webView.BackgroundColor = UIColor.Clear; SetNativeControl(webView); } e.NewElement.SetViewRenderer(this); }
public HybridWebViewRenderer(WKWebViewConfiguration config) : base(config) { userController = config.UserContentController; var script = new WKUserScript(new NSString(JavaScriptFunction), WKUserScriptInjectionTime.AtDocumentEnd, false); userController.AddUserScript(script); }
public override void ViewDidLoad() { base.ViewDidLoad(); var userController = new WKUserContentController(); var scriptImage = new WKUserScript(new NSString(JavaScriptFunctionForImage), WKUserScriptInjectionTime.AtDocumentEnd, false); var scriptVideo = new WKUserScript(new NSString(JavaScriptFunctionForVideo), WKUserScriptInjectionTime.AtDocumentEnd, false); var scriptPDF = new WKUserScript(new NSString(JavaScriptFunctionForPDF), WKUserScriptInjectionTime.AtDocumentEnd, false); var scriptAllFiles = new WKUserScript(new NSString(JavaScriptFunctionForDownloadAll), WKUserScriptInjectionTime.AtDocumentEnd, false); var scriptGetURL = new WKUserScript(new NSString(JavaScriptFunction), WKUserScriptInjectionTime.AtDocumentEnd, false); userController.AddUserScript(scriptGetURL); userController.AddScriptMessageHandler(this, "callBack"); webView = new WKWebView(new CoreGraphics.CGRect(0, 0, 375, 667), new WKWebViewConfiguration() { UserContentController = userController }); webView.UIDelegate = this; webView.NavigationDelegate = this; View.AddSubview(webView); webView.LoadRequest(new NSUrlRequest(url)); StartActivityIndicatorWithText("Loading...", new CoreGraphics.CGRect(0, 0, 375, 667)); }
public _c_hwebview_renderer(WKWebViewConfiguration config) : base(config) { userController = config.UserContentController; var script = new WKUserScript(new NSString(JavaScriptFunction), WKUserScriptInjectionTime.AtDocumentEnd, false); userController.AddUserScript(script); userController.AddScriptMessageHandler(this, "invokeAction"); }
/// <summary> /// The on element changed callback. /// </summary> /// <param name="e"> /// The event arguments. /// </param> protected override void OnElementChanged(ElementChangedEventArgs <CustomWebView> e) { base.OnElementChanged(e); if (Control == null && e.NewElement != null) { _userController = new WKUserContentController(); var config = new WKWebViewConfiguration() { UserContentController = _userController }; var script = new WKUserScript(new NSString(NativeFunction + GetFuncScript()), WKUserScriptInjectionTime.AtDocumentEnd, false); _userController.AddUserScript(script); _userController.AddScriptMessageHandler(this, "native"); var webView = new WKWebView(Frame, config) { WeakNavigationDelegate = this }; SetNativeControl(webView); //webView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight; //webView.ScalesPageToFit = true; _leftSwipeGestureRecognizer = new UISwipeGestureRecognizer(() => Element.OnLeftSwipe(this, EventArgs.Empty)) { Direction = UISwipeGestureRecognizerDirection.Left }; _rightSwipeGestureRecognizer = new UISwipeGestureRecognizer(() => Element.OnRightSwipe(this, EventArgs.Empty)) { Direction = UISwipeGestureRecognizerDirection.Right }; _downSwipeGestureRecognizer = new UISwipeGestureRecognizer(() => Element.OnDownSwipe(this, EventArgs.Empty)) { Direction = UISwipeGestureRecognizerDirection.Down }; webView.AddGestureRecognizer(_leftSwipeGestureRecognizer); webView.AddGestureRecognizer(_rightSwipeGestureRecognizer); webView.AddGestureRecognizer(_downSwipeGestureRecognizer); } if (e.NewElement == null && Control != null) { Control.RemoveGestureRecognizer(_leftSwipeGestureRecognizer); Control.RemoveGestureRecognizer(_rightSwipeGestureRecognizer); Control.RemoveGestureRecognizer(_downSwipeGestureRecognizer); } Unbind(e.OldElement); Bind(); }
protected override void OnElementChanged(ElementChangedEventArgs <HybridWebView> e) { base.OnElementChanged(e); if (Control == null) { userController = new WKUserContentController(); var script = new WKUserScript(new NSString(JavaScriptFunction), WKUserScriptInjectionTime.AtDocumentEnd, false); userController.AddUserScript(script); userController.AddScriptMessageHandler(this, "invokeAction"); var config = new WKWebViewConfiguration { UserContentController = userController }; //config.WebsiteDataStore = null; var webView = new WKWebView(Frame, config); SetNativeControl(webView); } if (e.OldElement != null) { userController.RemoveAllUserScripts(); userController.RemoveScriptMessageHandler("invokeAction"); var hybridWebView = e.OldElement as HybridWebView; hybridWebView.Cleanup(); } if (e.NewElement != null) { string fileName = Path.Combine(NSBundle.MainBundle.BundlePath, string.Format("Content/{0}", Element.Uri)); Control.LoadRequest(new NSUrlRequest(new NSUrl(fileName, false))); } (e.NewElement as HybridWebView).DoJavascriptCalls += (sender, data) => { if (Control != null) { Device.BeginInvokeOnMainThread(() => { var mString = string.Format("addNewIcon('{0}', '{1}', '{2}')", data[0].ToString(), data[1].ToString(), data[2].ToString()); Control.EvaluateJavaScript(new NSString(mString), null); }); } }; (e.NewElement as HybridWebView).DoJavascriptSaves += (sender, data) => { if (Control != null) { Device.BeginInvokeOnMainThread(() => { Control.EvaluateJavaScript(new NSString("saveStatus()"), null); }); } }; }
protected override void OnElementChanged(ElementChangedEventArgs <HybridWebView> e) { base.OnElementChanged(e); NSObject agent = NSUserDefaults.StandardUserDefaults["UserAgernt"]; string newUserAgent = ""; if (agent != null) { newUserAgent = agent.ToString(); } if (!newUserAgent.Contains("coltrack_ios_mobile")) { newUserAgent += " coltrack_ios_mobile"; var dictionary = new NSDictionary("UserAgent", newUserAgent); NSUserDefaults.StandardUserDefaults.RegisterDefaults(dictionary); } if (Control == null) { userController = new WKUserContentController(); var script = new WKUserScript(new NSString(JavaScriptFunction), WKUserScriptInjectionTime.AtDocumentEnd, false); userController.AddUserScript(script); userController.AddScriptMessageHandler(this, "invokeAction"); var config = new WKWebViewConfiguration { UserContentController = userController }; var webView = new WKWebView(Frame, config); SetNativeControl(webView); System.Console.WriteLine("==================================== JavaScriptFunction"); } if (e.OldElement != null) { userController.RemoveAllUserScripts(); userController.RemoveScriptMessageHandler("invokeAction"); var hybridWebView = e.OldElement as HybridWebView; hybridWebView.Cleanup(); } if (e.NewElement != null) { //string fileName = Path.Combine (NSBundle.MainBundle.BundlePath, string.Format ("Content/{0}", Element.Uri)); //Control.LoadRequest (new NSUrlRequest (new NSUrl (fileName, false))); Control.LoadRequest(new NSUrlRequest(new NSUrl("https://coltrack.com/index.jsp"))); } }
private void EmbedJS() { const string JS_NOTIFY_SCRIPT = "function NotifyApp(data){window.webkit.messageHandlers.invokeAction.postMessage(data);}" + "window.external = { notify : function(data){NotifyApp(data);}};"; _UserController = new WKUserContentController(); var script = new WKUserScript(new NSString(JS_NOTIFY_SCRIPT), WKUserScriptInjectionTime.AtDocumentEnd, false); _UserController.AddUserScript(script); _UserController.AddScriptMessageHandler(this, "invokeAction"); }
protected override void OnElementChanged(ElementChangedEventArgs <HybridWebView> e) { base.OnElementChanged(e); if (Control == null) { userController = new WKUserContentController(); var script = new WKUserScript(new NSString(JavaScriptFunction), WKUserScriptInjectionTime.AtDocumentEnd, false); userController.AddUserScript(script); userController.AddScriptMessageHandler(this, "invokeAction"); var config = new WKWebViewConfiguration { UserContentController = userController }; var webView = new WKWebView(Frame, config); webView.Configuration.Preferences.JavaScriptEnabled = true; webView.Configuration.Preferences.JavaScriptCanOpenWindowsAutomatically = true; //allow application.navigatye in wisej //https://forums.xamarin.com/discussion/9791/set-a-top-margin-for-a-webview-in-ios //webView.ScrollView.ContentInset = new UIKit.UIEdgeInsets(40, 0,0 , 0); //webView.ScrollView.BackgroundColor = UIKit.UIColor.Clear; //webView.BackgroundColor = UIKit.UIColor.Clear; SetNativeControl(webView); webView.NavigationDelegate = new CustomWebViewDelegate(); //webView. } if (e.OldElement != null) { userController.RemoveAllUserScripts(); userController.RemoveScriptMessageHandler("invokeAction"); var hybridWebView = e.OldElement as HybridWebView; hybridWebView.Cleanup(); } if (e.NewElement != null) { //string fileName = Path.Combine (NSBundle.MainBundle.BundlePath, string.Format ("Content/{0}", Element.Uri)); //Control.LoadRequest (new NSUrlRequest (new NSUrl (fileName, false))); //Control.LoadRequest(new NSUrlRequest(new NSUrl("https://fixity.io/?hybrid=1"))); //force load of fixity.io Control.LoadRequest(new NSUrlRequest(new NSUrl(e.NewElement.Uri))); //force load of fixity.io } Control.Configuration.Preferences.JavaScriptEnabled = true; Control.Configuration.Preferences.JavaScriptCanOpenWindowsAutomatically = true; //allow application.navigatye in wisej //Control.Configuration.Preferences. }
/// <summary> /// The on element changed callback. /// </summary> /// <param name="e"> /// The event arguments. /// </param> protected override void OnElementChanged(ElementChangedEventArgs <HybridWebView> e) { base.OnElementChanged(e); if (Control == null && e.NewElement != null) { this.userController = new WKUserContentController(); var config = new WKWebViewConfiguration { UserContentController = this.userController }; var script = new WKUserScript(new NSString(NativeFunction + GetFuncScript()), WKUserScriptInjectionTime.AtDocumentEnd, false); this.userController.AddUserScript(script); this.userController.AddScriptMessageHandler(this, ScriptMessageHandlerName); var webView = new WKWebView(this.Frame, config) { WeakNavigationDelegate = this }; SetNativeControl(webView); //webView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight; //webView.ScalesPageToFit = true; this.leftSwipeGestureRecognizer = new UISwipeGestureRecognizer(() => Element.OnLeftSwipe(this, EventArgs.Empty)) { Direction = UISwipeGestureRecognizerDirection.Left }; this.rightSwipeGestureRecognizer = new UISwipeGestureRecognizer(() => Element.OnRightSwipe(this, EventArgs.Empty)) { Direction = UISwipeGestureRecognizerDirection.Right }; webView.AddGestureRecognizer(this.leftSwipeGestureRecognizer); webView.AddGestureRecognizer(this.rightSwipeGestureRecognizer); } if (e.NewElement == null) { HandleCleanup(); } this.Unbind(e.OldElement); this.Bind(); }
protected override void OnElementChanged(ElementChangedEventArgs <HybridWebView> e) { base.OnElementChanged(e); if (Control == null) { userController = new WKUserContentController(); var script = new WKUserScript(new NSString(JavaScriptFunction), WKUserScriptInjectionTime.AtDocumentEnd, false); userController.AddUserScript(script); userController.AddScriptMessageHandler(this, "invokeAction"); var config = new WKWebViewConfiguration { UserContentController = userController }; var webView = new WKWebView(Frame, config); SetNativeControl(webView); } if (e.OldElement != null) { userController.RemoveAllUserScripts(); userController.RemoveScriptMessageHandler("invokeAction"); var hybridWebView = e.OldElement as HybridWebView; hybridWebView.Cleanup(); } if (e.NewElement != null) { e.NewElement.GoBackOnNativeEventListener += (sender, args) => { Control.GoBack(); }; Control.LoadRequest(new NSUrlRequest(new NSUrl(Element.Uri))); } if (NativeView != null && e.NewElement != null) { var webView = ((Xamarin.Forms.Platform.iOS.ViewRenderer <XXXXX.HybridWebView, WebKit.WKWebView>)NativeView); var vista = webView.Subviews[0] as WKWebView; if (!zoom.zoom_var) { vista.ScrollView.MinimumZoomScale = nfloat.Parse("1"); vista.ScrollView.MaximumZoomScale = nfloat.Parse("1"); } else { vista.ScrollView.MinimumZoomScale = nfloat.Parse("0.5"); vista.ScrollView.MaximumZoomScale = nfloat.Parse("2"); } } }
public WebView(WKWebViewConfiguration configuration) : base(CGRect.Empty, configuration) { var url = NSBundle.MainBundle.GetUrlForResource("WebView", "js"); var data = NSData.FromUrl(url); var source = new NSString(data, NSStringEncoding.UTF8); var userScript = new WKUserScript(source, WKUserScriptInjectionTime.AtDocumentEnd, true); configuration.UserContentController.AddUserScript(userScript); configuration.UserContentController.AddScriptMessageHandler(this, "turbolinks"); TranslatesAutoresizingMaskIntoConstraints = false; ScrollView.DecelerationRate = UIKit.UIScrollView.DecelerationRateNormal; }
private void SetupWebViewContent() { WebView = new WKWebView(View.Frame, new WKWebViewConfiguration { }); View.AddSubview(WebView); //WebView.UIDelegate = new WebDelegate(); WebView.NavigationDelegate = new GenericNavDelegate(this); WebView.ScrollView.ContentInset = new UIEdgeInsets(63f, 0, 0, 0);// / 414f * _screenWidth var width = UIScreen.MainScreen.Bounds.Width; //device-width NSString viewportScriptString = (NSString) $@"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width={width} * 1'); meta.setAttribute('initial-scale', '1.0'); meta.setAttribute('maximum-scale', '1.0'); meta.setAttribute('minimum-scale', '1.0'); meta.setAttribute('user-scalable', 'no'); document.getElementsByTagName('head')[0].appendChild(meta);"; //NSString disableSelectionScriptString = (NSString)"document.documentElement.style.webkitUserSelect='none';"; //NSString disableCalloutScriptString = (NSString)"document.documentElement.style.webkitTouchCallout='none';"; // 1 - Make user scripts for injection var viewportScript = new WKUserScript(source: viewportScriptString, injectionTime: WKUserScriptInjectionTime.AtDocumentEnd, isForMainFrameOnly: true); //var disableSelectionScript = new WKUserScript(source: disableSelectionScriptString, injectionTime: WKUserScriptInjectionTime.AtDocumentEnd, isForMainFrameOnly: true); //var disableCalloutScript = new WKUserScript(source: disableCalloutScriptString, injectionTime: WKUserScriptInjectionTime.AtDocumentEnd, isForMainFrameOnly: true); // 2 - Initialize a user content controller, add scripts // From docs: "provides a way for JavaScript to post messages and inject user scripts to a web view." var controller = WebView.Configuration.UserContentController; //var controller = new WKUserContentController(); controller.AddUserScript(viewportScript); //controller.AddUserScript(disableSelectionScript); //controller.AddUserScript(disableCalloutScript); // 6 - Webview options WebView.ScrollView.ScrollEnabled = true; // Make sure our view is interactable WebView.ScrollView.Bounces = false; // Things like this should be handled in web code WebView.AllowsBackForwardNavigationGestures = false; // Disable swiping to navigate WebView.ContentMode = UIViewContentMode.ScaleToFill; // Scale the page to fill the web view }
protected override void OnElementChanged(ElementChangedEventArgs <BingMapView> e) { base.OnElementChanged(e); if (Control == null) { userController = new WKUserContentController(); var script = new WKUserScript(new NSString(OnLoadComplete), WKUserScriptInjectionTime.AtDocumentEnd, false); userController.AddUserScript(script); userController.AddScriptMessageHandler(this, OnLoadCompleteMethod); script = new WKUserScript(new NSString(PinClick), WKUserScriptInjectionTime.AtDocumentEnd, false); userController.AddUserScript(script); userController.AddScriptMessageHandler(this, PinClickMethod); var config = new WKWebViewConfiguration { UserContentController = userController }; var webView = new WKWebView(Frame, config); SetNativeControl(webView); } if (e.OldElement != null) { userController.RemoveAllUserScripts(); userController.RemoveScriptMessageHandler(OnLoadCompleteMethod); userController.RemoveScriptMessageHandler(PinClickMethod); var hybridWebView = e.OldElement as BingMapView; } if (e.NewElement != null) { string fileName = Path.Combine(NSBundle.MainBundle.BundlePath, string.Format("BingMap/index.html")); Control.LoadRequest(new NSUrlRequest(new NSUrl(fileName, false))); } if (Element != null) { Element.ReceiveAction += Element_Send; } }
protected override void OnElementChanged(ElementChangedEventArgs <HybridWebView> e) { base.OnElementChanged(e); if (Control == null) { userController = new WKUserContentController(); var script = new WKUserScript(new NSString(NativeFunction + GetFuncScript()), WKUserScriptInjectionTime.AtDocumentEnd, false); userController.AddUserScript(script); userController.AddScriptMessageHandler(this, "native"); var config = new WKWebViewConfiguration { UserContentController = userController }; var webView = new WKWebView(Frame, config) { AllowsBackForwardNavigationGestures = true, WeakNavigationDelegate = this, AllowsLinkPreview = false, }; webView.ScrollView.Bounces = false; SetNativeControl(webView); } if (e.OldElement != null) { userController.RemoveAllUserScripts(); userController.RemoveScriptMessageHandler("invokeAction"); var hybridWebView = e.OldElement as HybridWebView; hybridWebView.Cleanup(); } if (e.NewElement != null) { Control.LoadRequest(new NSUrlRequest(new NSUrl(new Uri(e.NewElement.Uri).AbsoluteUri))); // set js handler e.NewElement.JavaScriptLoadRequested += (sender, js) => { Inject(js); }; } }
async Task SetCookie(List <Cookie> cookies) { if (PlatformVersion.IsAtLeast(11)) { foreach (var cookie in cookies) { await PlatformView.Configuration.WebsiteDataStore.HttpCookieStore.SetCookieAsync(new NSHttpCookie(cookie)); } } else { PlatformView.Configuration.UserContentController.RemoveAllUserScripts(); if (cookies.Count > 0) { WKUserScript wKUserScript = new WKUserScript(new NSString(GetCookieString(cookies)), WKUserScriptInjectionTime.AtDocumentStart, false); PlatformView.Configuration.UserContentController.AddUserScript(wKUserScript); } } }
async Task SetCookie(List <Cookie> cookies) { if (Forms.IsiOS11OrNewer) { foreach (var cookie in cookies) { await Configuration.WebsiteDataStore.HttpCookieStore.SetCookieAsync(new NSHttpCookie(cookie)); } } else { Configuration.UserContentController.RemoveAllUserScripts(); if (cookies.Count > 0) { WKUserScript wKUserScript = new WKUserScript(new NSString(GetCookieString(cookies)), WKUserScriptInjectionTime.AtDocumentStart, false); Configuration.UserContentController.AddUserScript(wKUserScript); } } }
public HybridWebViewRenderer(WKWebViewConfiguration config) : base(config) { userController = config.UserContentController; var invokationScript = new WKUserScript(new NSString(LoadScript(Constants.Scripts.Invoker)), WKUserScriptInjectionTime.AtDocumentEnd, false); var nativeInvoker = new WKUserScript(new NSString(LoadScript(_nativeInvoker)), WKUserScriptInjectionTime.AtDocumentEnd, false); var jqueryScript = new WKUserScript(new NSString(LoadScript(Constants.Scripts.JQuery)), WKUserScriptInjectionTime.AtDocumentEnd, false); var inputSpyScript = new WKUserScript(new NSString(LoadScript(Constants.Scripts.Spy)), WKUserScriptInjectionTime.AtDocumentEnd, false); userController.AddUserScript(invokationScript); userController.AddUserScript(nativeInvoker); userController.AddUserScript(jqueryScript); userController.AddUserScript(inputSpyScript); userController.AddScriptMessageHandler(this, "invokeAction"); //userController.AddScriptMessageHandler(this, "logging"); //string js = @"var console = { log: function(msg){window.webkit.messageHandlers.logging.postMessage(msg) } };"; //this.EvaluateJavaScript(new NSString(js), (result, error) => //{ // if (error != null) // Console.WriteLine($"installation of console.log() failed: {0}", error); //}); }
protected override void OnElementChanged(ElementChangedEventArgs <HybridWebView> e) { base.OnElementChanged(e); if (Control == null) { _userController = new WKUserContentController(); var script = new WKUserScript(new NSString(JavaScriptFunction), WKUserScriptInjectionTime.AtDocumentEnd, false); _userController.AddUserScript(script); _userController.AddScriptMessageHandler(this, "invokeAction"); var config = new WKWebViewConfiguration { UserContentController = _userController }; var webView = new WKWebView(Frame, config); SetNativeControl(webView); } if (e.OldElement != null) { _userController.RemoveAllUserScripts(); _userController.RemoveScriptMessageHandler("invokeAction"); var hybridWebView = e.OldElement as HybridWebView; } if (e.NewElement != null) { string fileName = Path.Combine(NSBundle.MainBundle.BundlePath, $"HybridWeb/{Element.Uri}"); Control.LoadRequest(new NSUrlRequest(new NSUrl(fileName, false))); var hybirdWebView = e.NewElement; hybirdWebView.RegisterInvokeJsFunctionAgent((s, action) => { string jsInvokeStr = $"javascript: {s}"; Control.EvaluateJavaScript(jsInvokeStr, (rs, error) => { action?.Invoke(rs.ToString()); }); }); } }