WKWebView Cookie Insertion on Request Load

Joshua_DJoshua_D USMember ✭✭
edited March 2017 in Xamarin.iOS

I’m attempting to load a WKWebView and assign a set of cookies to the initial request, so that the app can perform an automatic login process via custom webpage with JavaScript. I have found the following Stack Overflow response, but have been unable to get the initial load request to translate well in C#. See the link and my test code below:

http://stackoverflow.com/questions/26573137/can-i-set-the-cookies-to-be-used-by-a-wkwebview

NSMutableUrlRequest request = new NSMutableUrlRequest(new NSUrl(GConstants.MobileWebUrl)); request.ShouldHandleCookies = true; request.Headers = new NSDictionary(); //request.Headers.SetValueForKey(new NSString(GSession.Username), new NSString("username")); //request.Headers.SetValueForKey(new NSString(GSession.UserToken), new NSString("uToken")); request["username"] = GSession.Username; request["uToken"] = GSession.UserToken; appWebView.LoadRequest(request);

I have tried numerous steps on getting this step to work properly, including the code which is commented out in my example above. Doing some further digging, I can see that the WKWebView does have issues with getting cookies assigned to it (at least some older SO posts indicated...). Does anyone have any suggestions?

Posts

  • Joshua_DJoshua_D USMember ✭✭
    edited March 2017

    Here's a very hackish version that does not work:
    For what its worth, I've ran both in front of and after the LoadRequest() method.

    appWebView.LoadRequest(request); appWebView.EvaluateJavaScript("document.cookie='username=" + GSession.Username + "' path=/ domain=" + GConstants.MobileWebUrl + ";", null); appWebView.EvaluateJavaScript("document.cookie='uToken=" + GSession.UserToken + "' path=/ domain=" + GConstants.MobileWebUrl + ";", null);

  • Joshua_DJoshua_D USMember ✭✭
    edited April 2017

    I wanted to add this to the forums to show how I was able to solve this issue:

        var config = new WKWebViewConfiguration ();
            var userContentController = new WKUserContentController();
            WKUserScript script = new WKUserScript(new NSString(
                "document.cookie = 'username=" + GSession.Username + "; ';" +  
                "document.cookie = 'uToken=" + GSession.UserToken + "; '"
                    ), WKUserScriptInjectionTime.AtDocumentStart, false);
            userContentController.AddUserScript(script);
            config.UserContentController = userContentController;
    
            appWebView = new WKWebView (new CGRect (0, pixelOffset, _controller.View.Frame.Width, _controller.View.Frame.Height - pixelOffset), config);
    

    It's feels pretty hackish, but it gets the job done by creating the cookies prior to the page load, which is what I need to pass my user credentials along.

  • Joshua_DJoshua_D USMember ✭✭

    I wanted to publish on here how I was able to solve my problem:

        var config = new WKWebViewConfiguration ();
            var userContentController = new WKUserContentController();
            WKUserScript script = new WKUserScript(new NSString(
                "document.cookie = 'username=" + GSession.Username + "; ';" +  
                "document.cookie = 'uToken=" + GSession.UserToken + "; '"
                    ), WKUserScriptInjectionTime.AtDocumentStart, false);
            userContentController.AddUserScript(script);
            config.UserContentController = userContentController;
    
            appWebView = new WKWebView (new CGRect (0, pixelOffset, _controller.View.Frame.Width, _controller.View.Frame.Height - pixelOffset), config);
    
Sign In or Register to comment.