问题描述
我有一个带有 Webview
和 TabBar
的 Xcode 项目,我可以使用 TabBar 在 WebView 之间切换.我的问题是,当我将东西放入 lieferworld.de 下的 ShoppingCard 并切换TabBar 到我的购物卡 url 中的项目不可见.我该如何解决这个问题?ShoppingCard URL 以 .php 结尾.下面是实现的代码
I have a Xcode Project with a Webview
and a TabBar
and with the TabBar I can switch between WebViews.My Problem is that when I put something in my ShoppingCard under lieferworld.de and switch with the TabBar to my Shopping Card url the Items in there are not Visible. How can I solve this? the ShoppingCard URL ends with .php. Below is the code which is implemented
这里还有一个视频,你可以看到错误:
Here is also a Video were you can see the error:
Viewhome:
import UIKit
import WebKit
class viewHome: UIViewController, WKUIDelegate {
@IBOutlet var webViewHome: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webViewHome = WKWebView(frame: .zero, configuration: webConfiguration)
webViewHome.uiDelegate = self
webViewHome.configuration.preferences.javaScriptEnabled = true
//webViewHome.configuration.preferences.javaEnabled = true
view = webViewHome
}
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://lieferworld.de")
let request = URLRequest(url: url!)
webViewHome.configuration.preferences.javaScriptEnabled = true
//webViewHome.configuration.preferences.javaEnabled = true
webViewHome.load(request)
}
@IBAction func GoBackHome(_ sender: Any) {
if webViewHome.canGoBack {
webViewHome.goBack()
}
}
@IBAction func GoForwardHome(_ sender: Any) {
if webViewHome.canGoForward {
webViewHome.goForward()
}
}
}
ViewShopping |购物车类:
import UIKit
import WebKit
class viewShopping: UIViewController, WKUIDelegate {
@IBOutlet var webViewShopping: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webViewShopping = WKWebView(frame: .zero, configuration: webConfiguration)
webViewShopping.uiDelegate = self
//webViewShopping.configuration.preferences.javaEnabled = true
webViewShopping.configuration.preferences.javaScriptEnabled = true
view = webViewShopping
}
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://lieferworld.de/warenkorb.php")
let request = URLRequest(url: url!)
webViewShopping.configuration.preferences.javaScriptEnabled = true
//webViewShopping.configuration.preferences.javaEnabled = true
webViewShopping.load(request)
}
@IBAction func goBackShoppingCart(_ sender: Any) {
if webViewShopping.canGoBack {
webViewShopping.goBack()
}
}
@IBAction func goForwardShoppingCart(_ sender: Any) {
if webViewShopping.canGoForward {
webViewShopping.goForward()
}
}
@IBAction func webViewRefresh(_ sender: Any) {
webViewShopping.reload()
}
}
推荐答案
您的数据不会在两个 UIViewController 之间共享,因为您的 WKWebView 是彼此的不同实例,并且不共享相同的数据.就好像您在 Web 浏览器中打开了两个不同的选项卡.如果您已登录,并且购物车数据存储在服务器上,那么在您刷新购物车页面时它应该可以工作.如果它是基于 cookie 的,您需要确保这些 cookie 在两个 Web 视图中是通用的.相反,使用选项卡重定向到购物车的页面,而不是创建新的 View Controller 实例,或者将 WKWebView 的实例放入可以在两个选项卡之间共享的静态变量中.您必须对该视图进行一些隐藏/显示,以便您看不到它正在加载,但这可以通过委托进行处理.
Your data is not being shared between the two UIViewControllers because your WKWebViews are different instances of each other and don't share the same data. It's as if you opened two different tabs in your web browser. If you're logged in, and the cart data is stored on the server, it should work, if you refresh the cart's page. If it's cookie based, you'll need to ensure those cookies are common among the two web views. Instead, use the tab to redirect to the cart's page instead of creating a new View Controller instance, or put the instance of the WKWebView into a static variable that can be shared between the two tabs. You'll have to do a bit of hiding/showing of that view so that you don't see it loading, but that can be handled via the delegate.
如果数据存储在服务器上,您只需在 VC 中的 viewDidAppear() 覆盖函数中重新加载网页即可.这真的取决于你的目的,但这些是一些可行的建议.
If the data is stored on the server, you can simple just reload the webpage within the viewDidAppear() overridden function in your VC. It really depends on what you're going for, but those are a few suggestions that'll work.
这篇关于Xcode Wkwebview 未加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!