1

I have a Cocoa application with several webviews but with the same all webviews have the same url. I want every webviews to have their own cookies.

The current problem is that I save cookies and I set them in a webviews, all other webviews receive cookies I think is because they all have the same domain.

Is there a way to separate cookies and that each webview receives its custom cookies?

Even a solution in javascript I am a taker ..

Here is my current code:

    func setData(_ value: Any, key: String) {
        let ud = UserDefaults.standard
        let archivedPool = NSKeyedArchiver.archivedData(withRootObject: value)
        ud.set(archivedPool, forKey: key)
    }

    func getData<T>(key: String) -> T? {
        let ud = UserDefaults.standard
        if let val = ud.value(forKey: key) as? Data,
           let obj = NSKeyedUnarchiver.unarchiveObject(with: val) as? T {
            return obj
        }

        return nil
    }

And:

let processPool: WKProcessPool

        if let pool: WKProcessPool = self.getData(key: "pool\(service.id)") {
            processPool = pool
        } else {
            processPool = WKProcessPool()
            self.setData(processPool, key: "pool\(service.id)")
        }

        self.configuration.processPool = processPool

        if let cookies: [HTTPCookie] = self.getData(key: service.id) {
            for cookie in cookies {
                self.configuration.websiteDataStore.httpCookieStore.setCookie(cookie) {
                    print("Set cookie = \(cookie) with name = \(cookie.name)")
                }
            }
        }


        Timer.scheduledTimer(withTimeInterval: 20, repeats: true) { _ in
            print("Saving...")
            self.configuration.websiteDataStore.httpCookieStore.getAllCookies { cookies in
                self.setData(cookies, key: self.service.id)
            }
        }

        let request = URLRequest(url: service.url)
        Timer.scheduledTimer(withTimeInterval: 1.5, repeats: false) { _ in
            self.load(request)
        }
Mickael Belhassen
  • 2,970
  • 1
  • 25
  • 46

0 Answers0