简体   繁体   中英

How to persist Cookies in WKWebview iOS Swift - iOS 13 SwiftUI

I am looking for solution for iOS 13 SDK WKWebview based app built with SwiftUI in order to make cookies saved between different sessions of app usage.

This problem has been numerously discussed acrossed stackoverflow provided with different soltuions for ObjectiveC and Swift with Storyboards .

I am asking the community if there is ready to use code examlpe of how to provide WKWebView Cookie Persistency on iOS 13 WKWebView app with SwiftUI used

To make it clear: my app accesses remote web server with web site designed to be like mobile app. It's not local web app with need to manipulate cookies locally. Simply to make cookies manipulation in web site from remote server in Jquery JS code of the page in order to work and be saved across different sessions of app,

My current version of code:

import SwiftUI
import WebKit

struct ContentView: View {
  var body: some View {
    WebView().edgesIgnoringSafeArea(.all)
  }
}

struct WebView: UIViewRepresentable {
  func makeUIView(context: Context) -> WKWebView {
    let webView = WKWebView()
    webView.scrollView.isScrollEnabled = false
    return webView
  }

  func updateUIView(_ webView: WKWebView, context: Context) {
    let liveView = "https://example.com/projectname/index.html"
    if let url = URL(string: liveView) {
       let request = URLRequest(url: url)
       webView.load(request)
    }
  }
}

#if DEBUG
struct ContentView_Previews : PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}
#endif

Set the configuration webView.configuration.websiteDataStore.httpCookieStore

see this answer for an example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM