【问题标题】:WKWebView WKWebsiteDataStore get cookies name and value as stringWKWebView WKWebsiteDataStore 获取 cookie 名称和值作为字符串
【发布时间】:2021-07-03 04:49:58
【问题描述】:

Swift 5 iOS 13.0 Xcode 12.5.1

我通过下面的代码调用了Wkwebview的Cookies。

let dataStore = WKWebsiteDataStore.default()
    dataStore.httpCookieStore.getAllCookies({ (cookies) in
        print(cookies)
    })

现在我得到这样的“cookies”列表:

[<NSHTTPCookie
version:1
name:_ga
value:GA1.2.1804988442.1625286371
expiresDate:'2021-07-10 04:26:11 +0000'
created:'2021-07-03 04:26:11 +0000'
sessionOnly:FALSE
domain:.mydomain.com
partition:none
sameSite:none
path:/
isSecure:FALSE
path:"/" isSecure:FALSE>,
<NSHTTPCookie
version:1
name:_gid
value:GA1.2.1499099201.1625286371
expiresDate:'2021-07-04 04:26:11 +0000'
created:'2021-07-03 04:26:11 +0000'
sessionOnly:FALSE
domain:.mydomain.com
partition:none
sameSite:none
path:/
isSecure:FALSE
path:"/" isSecure:FALSE>]

我想在列表的第一个和第二个 cookie 中获取每个名称和值作为字符串。

示例) 代码

var dict: [String : String]
*Datas into the dictionary*
print(dict)

结果

["_ga":"GA1.2.1804988442.1625286371", "_gid":"GA1.2.1499099201.1625286371"]

我该怎么办?

【问题讨论】:

    标签: swift xcode cookies wkwebview


    【解决方案1】:

    使用 cookies 数组上的 map 函数来转换数组中的项目,如下所示:

    var dict: [String : String] = cookies.map { [$0.name: $0.value] }
    

    【讨论】:

    • @wookboy00 - 如果这回答了您的问题,请通过点击复选标记将其标记为已接受的答案。
    猜你喜欢
    • 2020-06-13
    • 2011-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-06
    • 2013-09-30
    • 2021-09-17
    • 2022-11-11
    相关资源
    最近更新 更多