gpt4 book ai didi

swift3 - WKWebView 如何从 HTTPCookieStorage 添加 auth cookie

转载 作者:行者123 更新时间:2023-12-04 12:38:25 24 4
gpt4 key购买 nike

我正在开发一个使用混合 native View Controller 和 UIWebView 的应用程序.应用程序登录由 API 处理,我有一个实用程序函数可以将身份验证 token 保存到 HTTPCookieStorage 中的 cookie 中。以及持久的用户模型。

现在我想更新到 WKWebView但它不会自动从 HTTPCookieStorage 加载 cookie喜欢 UIWebView .

我找到了一种从答案中保存响应 cookie 的方法 here我可以修改答案以将身份验证 token 也存储到我的用户模型中,但我不确定如何将 cookie 用于新请求。我只需要将 cookie 添加到初始加载中,因为我将禁用 Web View 中的导航。

有没有办法可以将 cookie 值添加到标题或其他解决方案中?

这是我将用来从响应中获取 cookie 的代码。

func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
if let urlResponse = navigationResponse.response as? HTTPURLResponse,
let url = urlResponse.url,
let allHeaderFields = urlResponse.allHeaderFields as? [String : String] {
let cookies = HTTPCookie.cookies(withResponseHeaderFields: allHeaderFields, for: url)
HTTPCookieStorage.shared.setCookies(cookies , for: urlResponse.url!, mainDocumentURL: nil)
decisionHandler(.allow)
}
}

最佳答案

func webViewDidStartLoad(webView: UIWebView)
{


let currentURL: String = webView.stringByEvaluatingJavaScriptFromString("window.location.href")!

print("currentURL =\(currentURL)")

self.invalidUserNameMsg.hidden = true;
self.startActivityIndicatory()

if(!self.isLoginButtonClick)
{
let cookieStorage = NSHTTPCookieStorage.sharedHTTPCookieStorage()
var cookieStr:String = ""
let cookies = cookieStorage.cookies!
let strTemp = self.linstance_url.stringByReplacingOccurrencesOfString("https://", withString: "")
for cookie in cookies {
cookieStr = cookieStr + "=" + cookie.name + "=" + cookie.value


if(cookie.name == "sid" && (cookie.value.characters.count > 100) && cookie.domain == strTemp)
{

ApplicationContext.sharedInstance.accessToken = cookie.value
ApplicationContext.sharedInstance.instanceUrl = self.linstance_url
ApplicationContext.sharedInstance.orgNamespace = orgNamespaceStr


NSUserDefaults.standardUserDefaults().setObject(cookie.value, forKey: "accessToken")
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "IsSSOLogin")
NSUserDefaults.standardUserDefaults().setObject(self.linstance_url, forKey: "linstance_url")

NSUserDefaults.standardUserDefaults().synchronize()

let appDelegate : AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate;

appDelegate.isSidValue = true
NSOperationQueue.mainQueue().addOperationWithBlock {

self.ssoWebView.hidden = true
self.ssoWebViewCloseButton.hidden = true

}


if(!self.isLoginButtonClick)
{
userNameTextField.text = ""
passwordTextField.resignFirstResponder();
userNameTextField.resignFirstResponder();
loginScrollView.contentSize = CGSizeMake(loginScrollView.frame.width, loginScrollView.frame.height - 100)

self.loginButtonAction(UIButton)
self.isLoginButtonClick = true
}

}

}
}
}

func webViewDidFinishLoad(webView: UIWebView)
{


self.stopActivityIndicator()




}
func webView(webView: UIWebView, didFailLoadWithError error: NSError?)
{
self.stopActivityIndicator()
}

关于swift3 - WKWebView 如何从 HTTPCookieStorage 添加 auth cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42651283/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com