Search code examples
iosxcodeiframewkwebview

localStorage not saving inside iFrame when using WKWebView


scratching my head over this iFrame issue when used within WKWebView, as none of the localStorage changes persisted. While the values seem to be saved while the app is still running, as soon as the app is terminated, all localStorage values within the iFrame pages get wiped out.

So this is my standard web view code

override func viewDidLoad() {
        super.viewDidLoad()
        guard let url = URL(string: "https://example.com") else {
            return
        }
        webView.configuration.preferences.javaScriptEnabled = true
        
        view.addSubview(webView)
        webView.load(URLRequest(url: url))
}

https://example.com returns a page that contains an iFrame which references a sub page within https://example.com.

I already tried adding sandbox="allow-same-origin" in the iFrame tag but to no avail.

Could use some ideas as to how I could get this working. Thanks in advance.


Solution

  • Issue sorted by making the parent frame and child frame coming from the same domain.

    Ex: if the page is loaded at https://mysite.example.com, the iFrame can't be to https://exmaple.com/mypage.html, which was exactly the set up I had when I saw the issue.