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"))); } }
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; } }
/// <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) { _userController = new WKUserContentController(); var config = new WKWebViewConfiguration() { UserContentController = _userController }; // SDM config.Preferences.SetValueForKey(new NSNumber(1), new NSString("allowFileAccessFromFileURLs")); 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 }; webView.AddGestureRecognizer(_leftSwipeGestureRecognizer); webView.AddGestureRecognizer(_rightSwipeGestureRecognizer); } if (e.NewElement == null && Control != null) { Control.RemoveGestureRecognizer(_leftSwipeGestureRecognizer); Control.RemoveGestureRecognizer(_rightSwipeGestureRecognizer); } Unbind(e.OldElement); Bind(); }
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. }
protected override void OnElementChanged(ElementChangedEventArgs <PaystackWebView> e) { base.OnElementChanged(e); if (Control == null) { var webviewElement = (PaystackWebView)Element; userController = new WKUserContentController(); var paymentscript = new WKUserScript(new NSString(PaymentJavaScriptFunction), WKUserScriptInjectionTime.AtDocumentEnd, false); var closeScript = new WKUserScript(new NSString(ClosePaymentJavaScriptFunction), WKUserScriptInjectionTime.AtDocumentEnd, false); var paystackScript = new WKUserScript(new NSString($"payWithPaystack({webviewElement.Data})"), WKUserScriptInjectionTime.AtDocumentEnd, false); userController.AddUserScript(paymentscript); userController.AddUserScript(closeScript); userController.AddUserScript(paystackScript); userController.AddScriptMessageHandler(this, "invokePayAction"); userController.AddScriptMessageHandler(this, "invokeCloseAction"); var config = new WKWebViewConfiguration { UserContentController = userController }; var webView = new WKWebView(Frame, config); SetNativeControl(webView); } if (e.OldElement != null) { userController.RemoveAllUserScripts(); userController.RemoveScriptMessageHandler("invokePayAction"); userController.RemoveScriptMessageHandler("invokeCloseAction"); var paystackWebView = e.OldElement as PaystackWebView; paystackWebView.CleanUp(); } if (e.NewElement != null) { string content = LoadHtmlString(); Control.LoadHtmlString(content, baseUrl: null); } }
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 void AppDomainInitializer() { var userController = new WKUserContentController(); userController.AddScriptMessageHandler(this, "CSharp"); var config = new WKWebViewConfiguration { UserContentController = userController }; webview_real = new WKWebView(View.Frame, config); View.AddSubview(webview_real); string localHtmlUrl = Path.Combine(NSBundle.MainBundle.BundlePath, "Content/index.html"); webview_real.LoadRequest(new NSUrlRequest(new NSUrl(localHtmlUrl, false))); }
public override void CustomizeWebView() { WKWebViewConfiguration config = new WKWebViewConfiguration (); // Adding Custom Message Handler WKUserContentController userContentController = new WKUserContentController (); WKScriptMessageHandler scriptMessageHandler = new IPhoneWKScriptMessageHandler (); userContentController.AddScriptMessageHandler (scriptMessageHandler, "service"); config.UserContentController = userContentController; // UIScreen screen = UIScreen.MainScreen; // RectangleF viewFrame = new RectangleF (new PointF (0, 0), new SizeF (320, 480)); this.webView = new WKWebView (this.View.Frame, config); //log ("************************* UnityUI_iOSViewController_WKWebView this.View.Frame.Height: " + this.View.Frame.Height); log ("UnityUI_iOSViewController_WKWebView removing bounces on WKWebView.ScrollView."); this.webView.ScrollView.Bounces = false; // TESTING REMOVING BOUNCYNG this.View.AddSubview (this.webView); }
private void CreateWebView(UIView newsuper) { _userController = new WKUserContentController(); _userController.AddScriptMessageHandler(this, "jsBridge"); var preferences = new WKPreferences { JavaScriptEnabled = true }; var configuration = new WKWebViewConfiguration { UserContentController = _userController, Preferences = preferences, }; _baseWebView = new WKWebView(newsuper.Frame, configuration); _baseWebView.Hidden = true; _baseWebView.NavigationDelegate = new CustomNavigationDelegate(this); }
protected override void OnElementChanged(ElementChangedEventArgs <ShellWebView> e) { base.OnElementChanged(e); userController = new WKUserContentController(); userController.AddScriptMessageHandler(this, ScriptMessageHandlerName); WKWebView webView = new WKWebView(Frame, new WKWebViewConfiguration { UserContentController = userController }); webView.WeakNavigationDelegate = this; SetNativeControl(webView); Element.InjectJavaScriptRequested += InjectJavaScriptRequested; UpdateSource(); }
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); }; } }
void SetupControl() { _navigationDelegate = new FormsNavigationDelegate(this); _contentController = new WKUserContentController(); _contentController.AddScriptMessageHandler(this, "invokeAction"); _configuration = new WKWebViewConfiguration { UserContentController = _contentController, AllowsInlineMediaPlayback = true }; _configuration.ProcessPool = _processPool; var wkWebView = new WKWebView(Frame, _configuration) { Opaque = false, UIDelegate = this, BackgroundColor = UIColor.Clear, NavigationDelegate = _navigationDelegate, }; FormsWebView.CallbackAdded += OnCallbackAdded; try { //Autoresize after rotation. Wrapped in try catch with no handling since untested wkWebView.ContentMode = UIViewContentMode.ScaleAspectFit; wkWebView.AutoresizingMask = UIViewAutoresizing.FlexibleDimensions; wkWebView.SizeToFit(); if (wkWebView.ScrollView != null) { wkWebView.ScrollView.Bounces = false; } } catch { /*Do nothing right now*/ } SetNativeControl(wkWebView); SetUserAgent(); OnControlChanged?.Invoke(this, wkWebView); }
void SetupControl() { _navigationDelegate = new FormsNavigationDelegate(this); _contentController = new WKUserContentController(); _contentController.AddScriptMessageHandler(this, "invokeAction"); _configuration = new WKWebViewConfiguration { UserContentController = _contentController }; var wkWebView = new WKWebView(Frame, _configuration) { UIDelegate = this, NavigationDelegate = _navigationDelegate }; FormsWebView.CallbackAdded += OnCallbackAdded; SetNativeControl(wkWebView); OnControlChanged?.Invoke(this, wkWebView); }
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()); }); }); } }
public override void CustomizeWebView() { WKWebViewConfiguration config = new WKWebViewConfiguration(); // Adding Custom Message Handler WKUserContentController userContentController = new WKUserContentController(); WKScriptMessageHandler scriptMessageHandler = new IPhoneWKScriptMessageHandler(); userContentController.AddScriptMessageHandler(scriptMessageHandler, "service"); config.UserContentController = userContentController; // UIScreen screen = UIScreen.MainScreen; // RectangleF viewFrame = new RectangleF (new PointF (0, 0), new SizeF (320, 480)); this.webView = new WKWebView(this.View.Frame, config); //log ("************************* UnityUI_iOSViewController_WKWebView this.View.Frame.Height: " + this.View.Frame.Height); log("UnityUI_iOSViewController_WKWebView removing bounces on WKWebView.ScrollView."); this.webView.ScrollView.Bounces = false; // TESTING REMOVING BOUNCYNG this.View.AddSubview(this.webView); }
protected override void OnElementChanged(ElementChangedEventArgs <WebViewer> e) { base.OnElementChanged(e); if (Control == null) { userController = new WKUserContentController(); var script = new WKUserScript(new NSString(JSDefines.JavaScriptFunction_iOS), WKUserScriptInjectionTime.AtDocumentEnd, false); userController.AddUserScript(script); userController.AddScriptMessageHandler(this, "invokeAction"); var config = new WKWebViewConfiguration { UserContentController = userController }; webView = new WKWebView(Frame, config) { WeakNavigationDelegate = new WebViewNavigationDelegate(this, Element.Uri) }; SetNativeControl(webView); Element.RegisterJSCallback(this.JSCallBack); } if (e.OldElement != null) { userController.RemoveAllUserScripts(); userController.RemoveScriptMessageHandler("invokeAction"); var webViewer = e.OldElement as WebViewer; webViewer.Cleanup(); } if (e.NewElement != null) { #if LOCALHTMLTEST string localhtmlurl = Path.Combine(NSBundle.MainBundle.BundlePath, "Local.html"); Control.LoadRequest(new NSUrlRequest(new NSUrl(localhtmlurl, false))); #else Control.LoadRequest(new NSUrlRequest(new NSUrl(Element.Uri))); #endif } }
private void SetupControl() { _navigationDelegate = new HybridWebViewNavigationDelegate(this); _contentController = new WKUserContentController(); _contentController.AddScriptMessageHandler(this, "invokeAction"); _contentController.AddUserScript(new WKUserScript( (NSString)HybridWebViewControl.InjectedFunction, WKUserScriptInjectionTime.AtDocumentStart, true)); if (!string.IsNullOrEmpty(Element.InjectScript)) { _contentController.AddUserScript(new WKUserScript( (NSString)Element.InjectScript, WKUserScriptInjectionTime.AtDocumentStart, true)); } _configuration = new WKWebViewConfiguration { UserContentController = _contentController, AllowsInlineMediaPlayback = true }; var wkWebView = new WKWebView(Frame, _configuration) { Opaque = false, UIDelegate = this, NavigationDelegate = _navigationDelegate, }; HybridWebViewControl.CallbackAdded += OnCallbackAdded; SetNativeControl(wkWebView); SetUserAgent(); OnControlChanged?.Invoke(this, wkWebView); }
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions) { // Initialize the content controller. var bounds = UIScreen.MainScreen.Bounds; var contentController = new WKUserContentController(); var contentMessageHandler = new WebClient(); contentController.AddScriptMessageHandler(contentMessageHandler, "native"); // Initialize the content. var webView = new WKWebView(bounds, new WKWebViewConfiguration { UserContentController = contentController }); var view = new ViewClient(webView); _bridge = new Bridge(new BridgeClient(webView), new CorePlugin(view)); // Initialize the content configuration. contentMessageHandler.UseBridge(_bridge); webView.ScrollView.ContentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentBehavior.Never; webView.ScrollView.ShowsHorizontalScrollIndicator = false; webView.ScrollView.ShowsVerticalScrollIndicator = false; // Initialize the view splash screen. var launchScreen = NSBundle.MainBundle.LoadNib("LaunchScreen", null, null); var launchView = launchScreen.GetItem <UIView>(0); launchView.Frame = bounds; // Initialize the window. Window = new UIWindow(bounds) { RootViewController = view }; Window.RootViewController.View = launchView; Window.MakeKeyAndVisible(); return(true); }
private WKUserContentController GetUserContentController() { string jsToInject = this.authenticator.InjectedJavascriptForPage(null); if (jsToInject == null) { return(null); } var userContentController = new WKUserContentController(); if (_wKScriptMessageHandler == null) { _wKScriptMessageHandler = new SamlWKWebViewJavascriptMessageHandler(this); } userContentController.AddScriptMessageHandler(_wKScriptMessageHandler, "jsInterceptor"); WKUserScript userScript = new WKUserScript(new NSString(jsToInject), WKUserScriptInjectionTime.AtDocumentEnd, isForMainFrameOnly: true); userContentController.AddUserScript(userScript); return(userContentController); }
protected override void OnElementChanged(ElementChangedEventArgs <ExtendedWebView> e) { base.OnElementChanged(e); if (Control == null) { user = new WKUserContentController(); var script = new WKUserScript(new NSString(JavascriptFunction), WKUserScriptInjectionTime.AtDocumentStart, false); user.AddUserScript(script); user.AddScriptMessageHandler(this, "invokeAction"); var config = new WKWebViewConfiguration { UserContentController = user }; var view = new WKWebView(Frame, config); SetNativeControl(view); } if (e.NewElement != null) { var webView = (ExtendedWebView)Element; Control.LoadRequest(new NSUrlRequest(new NSUrl("https://chihuahuacapital.mpiochih.gob.mx"))); Control.WeakNavigationDelegate = new MyWebViewDelegate(webView); } }
private void SetupControl(FormsWebView element) { WebViewControlDelegate.OnNavigationRequestedFromUser += OnUserNavigationRequested; WebViewControlDelegate.OnInjectJavascriptRequest += OnInjectJavascriptRequested; WebViewControlDelegate.OnActionAdded += OnActionAdded; UserController = new WKUserContentController(); UserController.AddScriptMessageHandler(this, "invokeAction"); NavigationDelegate = new FormsWKNavigationDelegate(this, element); WebViewConfiguration = new WKWebViewConfiguration { UserContentController = UserController }; var webView = new WKWebView(Frame, WebViewConfiguration); webView.UIDelegate = this; webView.NavigationDelegate = NavigationDelegate; OnControlChanging?.Invoke(this, Element, Control); SetNativeControl(webView); OnControlChanged?.Invoke(this, Element, Control); }
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) { if (!string.IsNullOrEmpty(Element.HttpScource)) { Control.LoadRequest(new NSUrlRequest(new NSUrl(Element.HttpScource, false))); } else { 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 <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"); //Console.WriteLine("active render"); var config = new WKWebViewConfiguration { UserContentController = userController }; 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)); if (Element.Uri != null) { Control.LoadRequest(new NSUrlRequest(new NSUrl(Element.Uri))); } //webView.EvaluateJavaScriptAsync("readcontent();"); //Console.WriteLine("active render"); } }
protected override void OnElementChanged(ElementChangedEventArgs <HybridWebView> e) { base.OnElementChanged(e); if (Control == null) { userController = new WKUserContentController(); userController.AddScriptMessageHandler(this, "BloggerPro"); var config = new WKWebViewConfiguration { UserContentController = userController, Preferences = new WKPreferences() { JavaScriptEnabled = true } }; var webView = new CustomWebView(Frame, config) { NavigationDelegate = new CustomWebViewDelegate(this), AutosizesSubviews = true }; //if (Element.EditorToolbar != null) //{ // var rend = Platform.CreateRenderer(e.NewElement.EditorToolbar); // CustomInputAccessoryView = rend.NativeView; //} // HideFormAccessoryBar.SetHideFormAccessoryBar(true); SetNativeControl(webView); } if (e.NewElement != null) { var newElement = e.NewElement; newElement.PropertyChanged += HandlePropertyChanged; newElement.EvalRequested += OnEvalRequested; newElement.EvaluateJavaScriptRequested += OnEvaluateJavaScriptRequested; newElement.EvaluateJavascript = async(js) => { var result = await Control.EvaluateJavaScriptAsync(js); return(result?.ToString()); }; Load(); } if (e.OldElement != null) { userController.RemoveAllUserScripts(); userController.RemoveScriptMessageHandler("BloggerPro"); var hybridWebView = e.OldElement as HybridWebView; hybridWebView.PropertyChanged -= HandlePropertyChanged; hybridWebView.EvalRequested -= OnEvalRequested; hybridWebView.Cleanup(); } }
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 }; cookieStore = config.WebsiteDataStore.HttpCookieStore; 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) { if (Element.Html == "" || Element.Html is null) { if (Element.Uri.Contains("://")) { string fileName = Element.Uri; Control.WeakNavigationDelegate = new NavigationDelegate(e.NewElement); var urlReq = new NSMutableUrlRequest(new NSUrl(fileName)); if (Element.OpenWithPost != null) { urlReq.HttpMethod = "POST"; urlReq.Body = NSData.FromArray(Element.OpenWithPost); } else { urlReq.HttpMethod = "GET"; } if (Element.Cookie != null) { var domain = Regex.Match(Element.Uri, DomainPattern).Groups["domain"].Value; foreach (var cookieString in Element.Cookie) { var cookieSplit = cookieString.Split(new[] { '=' }, 2); var cookieObj = new NSHttpCookie(cookieSplit[0], cookieSplit[1], "/", domain); cookieStore.SetCookie(cookieObj, null); } cookieStore.GetAllCookies((obj) => { foreach (var ob in obj) { Core.Log(ob.ToString()); } }); urlReq.ShouldHandleCookies = true; } Control.LoadRequest(urlReq); } else { string fileName = Path.Combine(NSBundle.MainBundle.BundlePath, "WebWrapper", Element.Uri); Control.LoadRequest(new NSUrlRequest(new NSUrl(fileName, false))); } } else { string fileName = Path.Combine(NSBundle.MainBundle.BundlePath, "WebWrapper"); Control.LoadHtmlString(Element.Html, new NSUrl(fileName, true)); } Element.JavaScriptRequested += (eval) => Control.EvaluateJavaScript(eval, null); // Element.JavaScriptRequested += (eval) => userController.AddUserScript(new WKUserScript(new NSString(eval), WKUserScriptInjectionTime.AtDocumentEnd, false)); } }
protected override void OnElementChanged(ElementChangedEventArgs <CoreWebView> e) { base.OnElementChanged(e); if (e.OldElement != null) { userController.RemoveAllUserScripts(); userController.RemoveScriptMessageHandler("invokeAction"); var hybridWebView = e.OldElement as CoreWebView; hybridWebView.Cleanup(); } if (e.NewElement != null) { WebView.RegisterJavscriptAction((script) => { Device.BeginInvokeOnMainThread(async() => { await webView.EvaluateJavaScriptAsync(script); }); }); 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 }; webView = new WKWebView(Frame, config); webView.UIDelegate = new CoreWebViewUIDelegate(); SetNativeControl(webView); } if (Element.Source == null) { NSUrl uri = null; if (Element.IsLocal) { string fileName = Path.Combine(NSBundle.MainBundle.BundlePath, Element.Uri); uri = new NSUrl(fileName, false); } else { uri = new NSUrl(Element.Uri); } if (uri != null) { Control.LoadRequest(new NSUrlRequest(uri)); } } else { var source = (HtmlWebViewSource)Element.Source; Control.LoadHtmlString(source.Html, new NSUrl(source.BaseUrl, true)); } } }
/// <summary> /// When element changes /// </summary> /// <param name="e"></param> protected override void OnElementChanged(ElementChangedEventArgs <WeavyWebView> 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 WeavyWebView; } if (e.NewElement != null) { // set js handler e.NewElement.JavaScriptLoadRequested += (sender, js) => { Inject(js); }; // handle load requests e.NewElement.LoadRequested += (sender, args) => { Request(LoadRequestComplete); }; // handle go back requests e.NewElement.GoBackRequested += (sender, args) => { if (!Control.CanGoBack) { return; } Control.GoBack(); }; // handle go formward requests e.NewElement.GoForwardRequested += (sender, args) => { if (!Control.CanGoForward) { return; } Control.GoForward(); }; // handle go formward requests e.NewElement.ReloadRequested += (sender, args) => { Control.Reload(); }; e.NewElement.OnInitFinished(this, EventArgs.Empty); } }