将特定方法从UIWebView迁移到WKWebView

时间:2018-11-01 20:40:25

标签: swift uiwebview wkwebview

我已经看到了有关将UIWebView移至WKWebView like this and this的一些一般性问题。

我有一个特定的用例,它在imageMap中导航。第一种方法与类无关,并且易于转换。我了解WKWebView中的等效方法,但是我看不到这对我有什么帮助。

我正在使用下面的代码,并且效果很好,但是我希望保持最新状态,如果可能的话,请迁移到WKWebKit。

func makeImageMap() {

        var imagePath = Bundle.main.resourcePath
        imagePath = imagePath?.replacingOccurrences(of: "/", with: "//")
        imagePath = imagePath?.replacingOccurrences(of: " ", with: "%20")

        let filePath = Bundle.main.path(forResource: "metro-2015", ofType: "html")
        let htmlData = NSData(contentsOfFile: filePath ?? "") as Data?
        if let aData = htmlData, let aPath = URL(string: "file:/\(imagePath ?? "")//") {
            webView.load(aData, mimeType: "text/html", textEncodingName: "UTF-8", baseURL: aPath)
        }
    }


    func webView(_ aWebView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebView.NavigationType) -> Bool {


        let url: URL? = request.url
        let stationCode = url?.fragment
        if stationCode != nil && (stationCode?.count ?? 0) > 0 {

            for i in 0..<stations.count {
                let station = stations[i]

                codeString = station.code
                if (codeString == stationCode) {
                    print(#function, "MATCH", station.name!)
                    showActionSheet()
                }
            }

        }

        return true
    }

0 个答案:

没有答案