Exemple #1
0
        protected override void OnElementChanged(ElementChangedEventArgs <BlazorWebView> e)
        {
            base.OnElementChanged(e);

            if (Control == null && e.NewElement != null)
            {
                userController = new WKUserContentController();
                //var script = new WKUserScript(new NSString(ContextBridgeHelper.GetInjectableJavascript(false)), WKUserScriptInjectionTime.AtDocumentEnd, false);
                //userController.AddUserScript(script);
                userController.AddScriptMessageHandler(this, "invokeAction");

                var config = new WKWebViewConfiguration {
                    UserContentController = userController, Preferences = new WKPreferences()
                    {
                        JavaScriptCanOpenWindowsAutomatically = false,
                        JavaScriptEnabled = true
                    }
                };
                var webView = new WKWebView(Frame, config);
                webView.NavigationDelegate = new WebNavigationDelegate(this);
                SetNativeControl(webView);
            }

            if (e.OldElement != null)
            {
                userController.RemoveAllUserScripts();
                userController.RemoveScriptMessageHandler("invokeAction");
            }
            if (e.NewElement != null)
            {
                Control.LoadRequest(new NSUrlRequest(new NSUrl(WebApplicationFactory.GetBaseURL())));
            }
        }
        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);
            }
        }
Exemple #3
0
        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 <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)));
            }
        }
Exemple #5
0
        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));
            }
        }
        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 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);

				}
			}
		}
        /// <summary>
        /// ⽬目前不不⽤用此⽅方法实现js被调⽤用
        /// </summary>
        /// <param name="e"></param>
        protected override void OnElementChanged(ElementChangedEventArgs <HyBridWebView> e)
        {
            //给JS⽅方法重命名(多参数需要放在⼀一个字典⾥里里⾯面)
            const string rename1 = "function UserId(data){window.webkit.messageHandlers.UserId.postMessage(data);}";

            if (e.OldElement != null)
            {
                userController.RemoveAllUserScripts();
                userController.RemoveScriptMessageHandler("UserId");
            }
            if (e.NewElement != null)
            {
                if (Control == null)
                {
                    userController = new WKUserContentController();
                    userController.AddScriptMessageHandler(this, "UserId");
                    var config = new WKWebViewConfiguration {
                        UserContentController = userController
                    };
                    var webView = new WKWebView(Frame, config);
                    _webView = webView;
                    SetNativeControl(webView);
                }
                hyBridWebView = e.NewElement as HyBridWebView;
                UrlWebViewSource    source  = e.NewElement.Source as UrlWebViewSource;
                NSMutableUrlRequest request = new NSMutableUrlRequest(new NSUrl(source.Url));
                Control.LoadRequest(new NSUrlRequest(new NSUrl(source.Url)));
            }
        }
Exemple #9
0
        protected override void OnElementChanged(ElementChangedEventArgs <HybridWebView> e)
        {
            base.OnElementChanged(e);

            // if disposing, NewElement is null. In that case just return
            if (e.NewElement == null)
            {
                return;
            }

            if (Control == null)
            {
                EmbedJS();
                var config = new WKWebViewConfiguration {
                    UserContentController = _UserController
                };
                var webView = new WKWebView(Frame, config);
                webView.WeakNavigationDelegate = this;
                webView.TranslatesAutoresizingMaskIntoConstraints = false;
                SetNativeControl(webView);
            }

            if (e.OldElement != null)
            {
                _UserController.RemoveAllUserScripts();
                _UserController.RemoveScriptMessageHandler("invokeAction");
                Element.InvokeScript -= Element_InvokeScriptEvent;
            }

            if (e.NewElement != null)
            {
                Element.InvokeScript += Element_InvokeScriptEvent;
            }
        }
        public override void RemoveFromSuperview()
        {
            base.RemoveFromSuperview();

            _baseWebView = null;
            _img         = null;
            _userController.RemoveAllUserScripts();
        }
        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")));
            }
        }
Exemple #13
0
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null)
            {
                userController.RemoveAllUserScripts();
                userController.RemoveScriptMessageHandler("invokeAction");
                HybridWebView hybridWebView = e.OldElement as HybridWebView;
                hybridWebView.Cleanup();
            }
        }
 protected override void Dispose(bool disposing)
 {
     base.Dispose(disposing);
     if (disposing)
     {
         userController.RemoveAllUserScripts();
         userController.RemoveScriptMessageHandler("ButtonPressedScriptHandler");
         buttonPressedScriptHandler.Dispose();
         userController.Dispose();
         wkWebView.Dispose();
     }
 }
        protected override void OnElementChanged(ElementChangedEventArgs <BlazorWebView> e)
        {
            base.OnElementChanged(e);

            if (Control == null)
            {
                userController = new WKUserContentController();

                var config = new WKWebViewConfiguration
                {
                    UserContentController = userController,
                    Preferences           = new WKPreferences()
                    {
                        JavaScriptCanOpenWindowsAutomatically = false,
                        JavaScriptEnabled = true
                    },
                    WebsiteDataStore = WKWebsiteDataStore.NonPersistentDataStore
                };

                webView = new WKWebView(Frame, config);
                webView.NavigationDelegate = new WebNavigationDelegate(this);

                SetNativeControl(webView);
            }

            if (e.OldElement != null)
            {
                userController.RemoveAllUserScripts();

                var oldElementController = e.OldElement as IWebViewController;
                oldElementController.EvalRequested -= OnEvalRequested;
                oldElementController.EvaluateJavaScriptRequested -= OnEvaluateJavaScriptRequested;
                oldElementController.GoBackRequested             -= OnGoBackRequested;
                oldElementController.GoForwardRequested          -= OnGoForwardRequested;
                oldElementController.ReloadRequested             -= OnReloadRequested;
                ((WebNavigationDelegate)webView.NavigationDelegate).SetBlazorWebViewForms(null);
            }
            if (e.NewElement != null)
            {
                var newElementController = e.NewElement as IWebViewController;
                newElementController.EvalRequested += OnEvalRequested;
                newElementController.EvaluateJavaScriptRequested += OnEvaluateJavaScriptRequested;
                newElementController.GoBackRequested             += OnGoBackRequested;
                newElementController.GoForwardRequested          += OnGoForwardRequested;
                newElementController.ReloadRequested             += OnReloadRequested;
                currentViewForms = e.NewElement;
                ((WebNavigationDelegate)webView.NavigationDelegate).SetBlazorWebViewForms(e.NewElement);
            }

            Load();
        }
Exemple #16
0
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null)
            {
                userController.RemoveAllUserScripts();
            }

            if (e.NewElement != null)
            {
                var filename = Path.Combine(NSBundle.MainBundle.BundlePath, $"Content/{((HybridWebView)Element).Uri}");
                LoadRequest(new NSUrlRequest(new NSUrl(filename, false)));
            }
        }
		public async void DidReceiveScriptMessage(WKUserContentController userContentController, WKScriptMessage message)
		{

			var jsMessage = message.Body.ToString();
			if (jsMessage != "")
			{
			}
			else {
				
					userController.RemoveAllUserScripts();
					userController.RemoveScriptMessageHandler("invokeAction");
					await App.Nav.PopAsync();

			}
		}
Exemple #18
0
        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");
                }
            }
        }
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null)
            {
                userController.RemoveAllUserScripts();
                userController.RemoveScriptMessageHandler("invokeAction");
                HybridWebView hybridWebView = e.OldElement as HybridWebView;
                hybridWebView.Cleanup();
            }

            if (e.NewElement != null)
            {
                string filename = Path.Combine(NSBundle.MainBundle.BundlePath, $"Content/{((HybridWebView)Element).Uri}");
                LoadRequest(new NSUrlRequest(new NSUrl(filename, false)));
            }
        }
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null)
            {
                _userController.RemoveAllUserScripts();
                _userController.RemoveScriptMessageHandler("invokeAction");
                HybridWebView hybridWebViewMain = e.OldElement as HybridWebView;
                hybridWebViewMain?.Cleanup();
            }

            if (e.NewElement != null)
            {
                //// No need this since we're loading dynamically generated HTML content
                //string filename = Path.Combine(NSBundle.MainBundle.BundlePath, $"Content/{((HybridWebView)Element).Uri}");
                //LoadRequest(new NSUrlRequest(new NSUrl(filename, false)));
            }
        }
Exemple #21
0
        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);
                };
            }
        }
        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());
                    });
                });
            }
        }
Exemple #24
0
        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
            }
        }
        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);
            }
        }
Exemple #26
0
        /// <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);
            }
        }
Exemple #27
0
        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));
                }
            }
        }