将WKNavigationResponse decisionHandler Objective-C转换为Xamarin C#

时间:2017-02-25 02:38:40

标签: xamarin.ios wkwebview

我需要帮助将以下内容转换为C#。我尝试了几种方法但仍然失败。我想使用以下方法获取响应cookie以进行身份​​验证。

(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);
}

1 个答案:

答案 0 :(得分:0)

好的,结果只是自己搞清楚,不是太糟糕。

[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);
}