将 WKNavigationResponse decisionHandler Objective-C 转换为 Xamarin C# [英] Converting WKNavigationResponse decisionHandler Objective-C to Xamarin C#
本文介绍了将 WKNavigationResponse decisionHandler Objective-C 转换为 Xamarin C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要帮助将以下内容转换为 C#.我尝试了几种方法,但一直失败.我想使用以下方法来获取响应 cookie 以进行身份验证.
I need help in converting the following into C#. I have tried several ways but keep failing. 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);
}
推荐答案
好吧,最后我自己弄明白了,还不错.
Ok, ended up just figuring it out myself, wasnt too bad.
[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);
}
这篇关于将 WKNavigationResponse decisionHandler Objective-C 转换为 Xamarin C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文