/// <summary> /// Request to load an uri /// </summary> /// <param name="completion"></param> private void Request(Action <bool> completion) { var element = Element as WeavyWebView; if (element == null) { return; } if (!string.IsNullOrEmpty(element.AuthenticationToken)) { var uri = new Uri(element.BaseUrl); string domain = uri.Host; // Set cookies here var cookieJar = NSHttpCookieStorage.SharedStorage; cookieJar.AcceptPolicy = NSHttpCookieAcceptPolicy.Always; //clean up old cookies foreach (var aCookie in cookieJar.Cookies) { cookieJar.DeleteCookie(aCookie); } //set up the new cookies var jCookies = element.Cookies.GetCookies(uri); IList <NSHttpCookie> eCookies = (from object jCookie in jCookies where jCookie != null select(Cookie) jCookie into netCookie select new NSHttpCookie(netCookie)).ToList(); cookieJar.SetCookies(eCookies.ToArray(), uri, uri); // create wk cookie WKHttpCookieStore cookieStore = Control.Configuration.WebsiteDataStore.HttpCookieStore; if (cookieJar.Cookies.Any()) { foreach (var c in cookieJar.Cookies) { cookieStore.SetCookie(c, () => { completion(true); return; }); } } else { completion(false); } } else { completion(false); } }
protected override async void OnElementChanged(VisualElementChangedEventArgs e) { base.OnElementChanged(e); if (e.NewElement != null) { // https://stackoverflow.com/a/26577303 => MinimumOSVersion=11.0 cookieStore = Configuration.WebsiteDataStore.HttpCookieStore; // TODO might be good to clear cache // if element is new, load cookies into platform if (e.NewElement is CookieWebView newElement && newElement.CookieContainer != null) { await RemoveAllCookies(); await LoadAllCookiesAsync(newElement); } } }
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)); } }