在WKwebView中加载Javascript在设备上不起作用

时间:2017-05-02 16:23:11

标签: javascript html ios swift3 wkwebview

此类具有Web视图加载逻辑。此类设置Web视图的所有属性,并且有一个回调函数,当调用javascript函数时将调用该函数,该函数将在本地存储在项目中。我正在尝试在webView(WKwebView)中加载javascript,它在模拟器中工作正常但在设备上不起作用。

class BM_DescriptionViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,UIWebViewDelegate,WKScriptMessageHandler {
        var webview = WKWebView()

        override func viewDidLoad() {
            super.viewDidLoad()
            self.setUpForTableView()
            self.title = NSLocalizedString("Dev", comment: "")

            // Custom back button
            BM_CustomBackButton.createBackButton(&navigationItem.leftBarButtonItem, tapGestureRecognizer: UITapGestureRecognizer(target: self, action: #selector(self.backToPreviousController)), type: BM_CustomBackButtonType.backWhiteArrow)
            setUpWebView()
        }

        // this function set all the properties of webview
        func setUpWebView() {
            //prepare
            if let path = Bundle.main.path(forResource: "file",
                                           ofType: ".html") {
                let url = URL(fileURLWithPath: path)
                let theAbsoluteURLString: String = url.absoluteString

                //changes from last post
                let theConfiguration = WKWebViewConfiguration()
                let theuserCOntentController = WKUserContentController()
                theuserCOntentController.add(self, name: "interOp")
                theConfiguration.userContentController = theuserCOntentController
                self.webview  = WKWebView(frame:self.view.frame,
                                          configuration: theConfiguration)
                //end of changes

                if let code = self.productCode, let brand = self.brand {
                    let queryString: String = "?brand=\(brand)&code=\(code)"
                    let absoluteURLwithQueryString: String = theAbsoluteURLString + queryString
                    if let finalURL = URL(string: absoluteURLwithQueryString) {
                        let urlRequest = URLRequest(url: finalURL)
                        webview.load(urlRequest as URLRequest)
                    }
                }

                self.view.addSubview(webview)
            }

        }

        // call back function 
        func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
            print("got message: \(message.body)")
        }

    }
}

0 个答案:

没有答案