Forum Xamarin.iOS
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Need help in converting WKNavigationResponse decisionHandler to C#

timi2shoestimi2shoes USMember ✭✭

I need help in converting the following into C#. I have tried several ways but keep failing. For a better understanding, I would like to use the following method to get the response cookies for authentication purposes.

  • (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
    NSHTTPURLResponse *response = (NSHTTPURLResponse *)navigationResponse.response;
    NSArray *cookies =[NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:response.URL];

    for (NSHTTPCookie *cookie in cookies) {
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
    }

    decisionHandler(WKNavigationResponsePolicyAllow);
    }

Thank you

Tagged:

Best Answer

  • timi2shoestimi2shoes USMember ✭✭
    Accepted Answer

    Ok, i just ended up figuring it out myself.

    [Foundation.Export("webView:decidePolicyForNavigationResponse:decisionHandler:")]
            public virtual void DecidePolicy(WKWebView webView, WKNavigationResponse navigationResponse, Action<WKNavigationResponsePolicy> decisionHandler)
            {
                NSHttpUrlResponse response;
                NSHttpCookie[] cookies_holder;
    
                response = (NSHttpUrlResponse)navigationResponse.Response;
    
                cookies_holder = NSHttpCookie.CookiesWithResponseHeaderFields(response.AllHeaderFields, response.Url);
    
                foreach (var cookie in cookies_holder)
                {
                    NSHttpCookieStorage.SharedStorage.SetCookie(cookie);
                }
    
                decisionHandler(WKNavigationResponsePolicy.Allow);
            }
    

Answers

  • timi2shoestimi2shoes USMember ✭✭
    Accepted Answer

    Ok, i just ended up figuring it out myself.

    [Foundation.Export("webView:decidePolicyForNavigationResponse:decisionHandler:")]
            public virtual void DecidePolicy(WKWebView webView, WKNavigationResponse navigationResponse, Action<WKNavigationResponsePolicy> decisionHandler)
            {
                NSHttpUrlResponse response;
                NSHttpCookie[] cookies_holder;
    
                response = (NSHttpUrlResponse)navigationResponse.Response;
    
                cookies_holder = NSHttpCookie.CookiesWithResponseHeaderFields(response.AllHeaderFields, response.Url);
    
                foreach (var cookie in cookies_holder)
                {
                    NSHttpCookieStorage.SharedStorage.SetCookie(cookie);
                }
    
                decisionHandler(WKNavigationResponsePolicy.Allow);
            }
    
Sign In or Register to comment.