简体   繁体   English

如何在 WKWebview iOS Swift 中保留 Cookie - iOS 13 SwiftUI

[英]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.我在寻找一个解决方案iOS 13 SDK WKWebview内置的基于应用程序SwiftUI为了使保存的应用程序使用不同的会话之间的cookie。

This problem has been numerously discussed acrossed stackoverflow provided with different soltuions for ObjectiveC and Swift with Storyboards .这个问题已经在为ObjectiveCSwift with Storyboards提供不同解决方案的 stackoverflow 中进行了多次讨论。

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我问社区是否有准备使用的代码如何examlpe提供WKWebView Cookie的持久性上iOS 13 WKWebView应用与SwiftUI使用

To make it clear: my app accesses remote web server with web site designed to be like mobile app.明确地说:我的应用程序访问远程 Web 服务器,其网站设计为类似于移动应用程序。 It's not local web app with need to manipulate cookies locally.它不是需要在本地操作 cookie 的本地 Web 应用程序。 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,只需在页面的Jquery JS代码中从远程服务器对网站中的 cookie 进行操作,以便在应用程序的不同会话中工作和保存,

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设置配置webView.configuration.websiteDataStore.httpCookieStore

see this answer for an example请参阅此答案以获取示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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