wookboy00
wookboy00

Reputation: 41

WKWebView WKWebsiteDataStore get cookies name and value as string

Swift 5 iOS 13.0 Xcode 12.5.1

I called Wkwebview's Cookies through the code below.

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

Now I get 'cookies' list like this:

[<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>]

And I want to get each name and value as a string in the first and second cookie of the list.

Example) Code

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

Result

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

How can I do?

Upvotes: 0

Views: 718

Answers (1)

de.
de.

Reputation: 8457

Use the map function on your cookies array to transform the items in the array like this:

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

Upvotes: 1

Related Questions