点击事件无法在iOS

时间:2016-12-19 08:32:52

标签: javascript jquery ios wkwebview

一切看起来都不错但是当我申请点击或执行任何其他事件时,在iOS WKWebView中无法满足要求。我假设代码中没有错误。但我想我需要设置一些我不知道的东西。 我需要帮助才能解决这个问题。如果有任何技巧或替代方案,请建议。

import UIKit
import WebKit

class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {

    //Outlets
    @IBOutlet weak var containerViewForWKWebView : UIView! //Set background color to very light grey color.

    //Variables
    var webView: WKWebView?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        //Initializing webView
        webView = WKWebView()
        //Setting delegate and other additional settings for webView
        webView?.isUserInteractionEnabled = true
        webView?.navigationDelegate = self
        webView?.uiDelegate = self
        webView?.scrollView.bounces = false
        webView?.backgroundColor = UIColor.black //Setting webView background color to black.

        //Now adding webView inside containerViewForWKWebView.
        containerViewForWKWebView.addSubview(webView!)
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        //Updating layout for webView.
        let frame = CGRect(x: 0.0, y: 0.0, width: containerViewForWKWebView.bounds.width, height: containerViewForWKWebView.bounds.height)
        webView?.frame = frame
        view.layoutIfNeeded()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func webPageForCheckboxTest(_ sender: UIButton) {

        loadLocalWebPageFromBundleMain(webPageFileName: "local-web-page-for-checkbox")
    }

    @IBAction func intertnetPageForCheckboxTest(_ sender: UIButton) {

        let webSiteAddress = URL(string: "http://juntolife.com/for_testing/web-page-for-checkbox.html")
        let myURLRequest = URLRequest(url: webSiteAddress!)
        webView?.load(myURLRequest)
    }

    @IBAction func webPageForClickEventTest(_ sender: UIButton) {

        loadLocalWebPageFromBundleMain(webPageFileName: "local-web-page-for-clickevent")
    }

    @IBAction func internetPageForClickEventTest(_ sender: UIButton) {

        let webSiteAddress = URL(string: "http://juntolife.com/for_testing/web-page-for-clickevent.html")
        let myURLRequest = URLRequest(url: webSiteAddress!)
        webView?.load(myURLRequest)
    }

    func loadLocalWebPageFromBundleMain(webPageFileName: String) {

        var htmlString: String!
        let webPageURLFromBundleMain = Bundle.main.url(forResource: webPageFileName, withExtension: "html")
        let baseURL = webPageURLFromBundleMain?.deletingLastPathComponent()
        print("Base URL of Bundle Main: \(baseURL)")

        do {
            let fileContent = try String(contentsOf: webPageURLFromBundleMain!, encoding: String.Encoding.utf8)
            htmlString = fileContent
        } catch let error as NSError {
            print("Failed getting content of the file: \(webPageURLFromBundleMain), Error: " + error.localizedDescription)
            htmlString = ""
        }

        if htmlString != "" {
            webView?.loadHTMLString(htmlString, baseURL: baseURL)
        } else {
            print("Local web page file is not found in bundle main.")
            htmlString = "<!DOCTYPE html><html><body><h3>Local web page file is not found in bundle main.</h3></body></html>"
        }
    }
}

本地-网页换clickevent.html

<!doctype html>
<html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
        <title>Title</title>
    </head>
    <body style="background-color:#bce0ff;">
        <div>
            <h3 style="text-align: center;"><strong>Local Web Page For Click Event Test</strong></h3>

            <p>Link outside content editable: <a href="#"> This is a dummy link</a> Please click it to see it is working or not.</p>
        </div>

        <div contenteditable="true" id="content" style="background-color:#ffffff;">
            <p>Link inside content editable: <a href="#"> This is a dummy link</a> Please click it to see it is working or not.</p>
        </div>
        <script>
            document.querySelector("body").addEventListener('click', function(e) {
              var anchor = e.target.closest('a');
              if(anchor !== null) {
                anchor.innerHTML = Date();
              }
            }, false);
        </script>
    </body>
</html>

本地-网页换checkbox.html

<!doctype html>
<html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
        <title>Title</title>
    </head>
    <body style="background-color:#bce0ff;">
        <div>
            <h3 style="text-align: center;"><strong>Local Web Page For Check Box Test</strong></h3>

            <p>Check box outside content editable: <input type="checkbox" /> This is a checkbox input. Please check it to see it is working or not.</p>
        </div>

        <div contenteditable="true" id="content" style="background-color:#ffffff;">
            <p>Check box inside content editable: <input type="checkbox" /> This is a checkbox input. Please check it to see it is working or not.</p>
        </div>
    </body>
</html>

0 个答案:

没有答案