Exemplo n.º 1
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));
                }
            }
        }
Exemplo n.º 2
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
                };
                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));
            }
        }
Exemplo n.º 3
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);
            }
        }