iOS-How to set themultiple cookies in WKWebview

639 Views Asked by At
How to set the multiple cookies in WKWebView.I have tried the following approach.

In the below code i am able to save only one cookie i.e "Abp.TenantId" other one is not saved.Can anyone help me out for this.I have tried also with the NSMutableURLRequest but it does not work for me also.

      if let url = URL(string: "URL") {
        var request = URLRequest(url:url)
        request.httpShouldHandleCookies = true
        let days: TimeInterval = 30 * 24 * 60 * 60
        WKWebsiteDataStore.default().httpCookieStore.add(self)
         guard let cookies = HTTPCookie(properties: [
                .domain: "domainname/",
                .path: "/",
                .name: "Abp.AuthToken",
                .value: "Abp.AuthToken=token",
                .secure: "TRUE",
                .expires: NSDate(timeIntervalSinceNow: days),
            ])else{
                return
            }
            HTTPCookieStorage.shared.setCookie(cookies)
            
          guard  let cook = HTTPCookie(properties: [
                .domain: "domainname",
                .path: "/",
                .name: "Abp.TenantId",
                .value: "Id",
                .secure: "TRUE",
                .expires: NSDate(timeIntervalSinceNow: days),

          ])else{
            return
          }
                HTTPCookieStorage.shared.setCookie(cook)

            
            let arrCookies = HTTPCookieStorage.shared.cookies ?? []
            for strcookie in arrCookies {
                webView.configuration.websiteDataStore.httpCookieStore.setCookie(strcookie, completionHandler: {
                    print("cookie setup done")
                })
            }
            
            DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) 
            {
                self.webView.load(request)
                }}
0

There are 0 best solutions below