How to save and load cookies in an webview iOS app?

I want to save cookies in my webview ios app and then use the cookies everytime the user opens the app. For example autologging in after user enters their login info once in the webview app. Here is how some of the code looks like:


override func viewDidLoad() {

        super.viewDidLoad()
        view.addSubview(webView)

        guard let url = URL(string: "domain.com") else{
        return
        }
        
        
        webView.load(URLRequest(url: url))
        webView.customUserAgent = "iphone/Safari/SomethingRandom"
        
        DispatchQueue.main.asyncAfter(deadline: .now()+5){

            self.webView.evaluateJavaScript("document.body.innerHTML"){ result, error in

                guard let html = result as? String, error == nil else{

                    return
                   }

                print (html)

                }            self.webView.configuration.websiteDataStore.httpCookieStore.setCookie(self.cookie)

            for cookie in self.cookies {
self.webView.configuration.websiteDataStore.httpCookieStore.setCookie(cookie)

            }

            }

    }
let cookie = HTTPCookie(properties: [

        .domain: "example.com",

        .path: "/",

        .name: "MyCookieName",

        .value: "MyCookieValue",

        .secure: "TRUE",

        .expires: NSDate(timeIntervalSinceNow: 31556926)

    ])!


    let cookies = HTTPCookieStorage.shared.cookies ?? []
How to save and load cookies in an webview iOS app?
 
 
Q