UIWebView注入cookie无效
我使用UIWebView注入cookie但是web端就是不能获取到,贴出示例代码:
webView.frame = view.bounds
view.addSubview(webView)
let path = Bundle.main.path(forResource: "index", ofType: "html")
let url = URL(fileURLWithPath: path!)
var request = URLRequest(url: url)
request.httpShouldHandleCookies = true
let arr = HTTPCookieStorage.shared.cookies
for cookie in arr ?? [] {
HTTPCookieStorage.shared.deleteCookie(cookie)
}
var cookies: [HTTPCookie] = []
for i in 0..<1 {
var cookieProperties: [HTTPCookiePropertyKey: String] = [:]
if i == 0 {
cookieProperties[.name] = "token"
cookieProperties[.value] = "123"
}
cookieProperties[.path] = "/"
cookieProperties[.domain] = "www.baidu.com"
if let cookie = HTTPCookie.init(properties: cookieProperties) {
cookies.append(cookie)
HTTPCookieStorage.shared.setCookie(cookie)
}
}
// HTTPCookieStorage.shared.setCookies(cookies, for: url, mainDocumentURL: nil)
webView.loadRequest(request)
请问到底哪里出错了?
我这边使用抓包工具可以抓取到cookies,但是为什么客户端就是获取不到cookie呢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
注入cookie 主要是 app端做的吧,h5端可借助第三方成熟的库js-cookie来获取cookie,