Xcode Wkwebview not Loading

2020-08-09 04:10发布

问题:

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:

https://youtu.be/qU3Mu1G7MY0

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 | Shopping Cart Class:

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()
    }

}

回答1:

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.

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.