KWebview中不起作用EvaluationJavaScrip

KWebview中不起作用EvaluationJavaScrip

本文介绍了alert()在WKWebview中不起作用EvaluationJavaScript()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我不明白为什么我的问题被标记为 ,首先,我使用validateJavaScript执行javascript代码,因为问题标题表明它与该问题明显不同.更重要的是,我注意到我在问题正文的末尾尝试了该问题的答案,但是没有成功.

I don't why my question is marked as duplicate of this one, first I execute javascript code with evaluateJavaScript as the question title shows which it's apparently different from that quesiton. What's more I've noted that I've try the answer in that question without no success at the end of my question body.

我使用wkwebview.evaluateJavaScript()函数在swift3的wkwebview中执行javascript.但是alert()并未打开警报对话框.而且没有错误和问题显示.虽然我可以使用evaluateJavaScript()执行javascript代码来修改页面内容.

I use wkwebview.evaluateJavaScript() funciton to execute javascript in the wkwebview of swift3. But the alert() didnot open the alert dialog. And there is no errors and issues shows. While I can use evaluateJavaScript() to execute javascript code to modify the page content.

class WebViewController: UIViewController, WKScriptMessageHandler, WKNavigationDelegate, WKUIDelegate, UIScrollViewDelegate {
    var wk:WKWebView!
    self.wk.navigationDelegate = self
    self.wk.uiDelegate = self
    self.wk.scrollView.delegate = self
    self.wk.customUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:53.0) Gecko/20100101 Firefox/53.0"

    ...
    override func viewDidLoad() {
        super.viewDidLoad()
        ...
        let config = WKWebViewConfiguration()
        self.wk = WKWebView(frame: CGRect(x: frame.minX, y: frame.minY+20, width: frame.width, height: frame.height-70), configuration: config)
        self.wk.navigationDelegate = self
        ...
    }


    ...
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        print("Finished navigation to url \(String(describing: webView.url))")
        //self.wk.evaluateJavaScript("document.getElementById('test').innerHTML = 'sssssssssssssss';", completionHandler: nil)          //this works well
        self.wk.evaluateJavaScript("alert('aaaaaaa');", completionHandler: nil)     //this not show the alert dialog
    }
    ...
}

我还引用了此帖子和,而那个问题不是关于validateJavaScript的.我为WebViewController添加WKUIDelegate并将self.wk.uiDelegate = self添加到我的viewDidLoad(),但是没有任何变化.

I also refer to this post and answer, while that question is not on evaluateJavaScript. I add WKUIDelegate for my WebViewController and add self.wk.uiDelegate = self to my viewDidLoad(), but nothing changes.

在console.log()下面添加了日志,将日志放在控制台中,而alert()不会弹出对话框.而且UIAlertController也可以工作.

added, below the console.log() put the log in the console, while alert() not pop up the dialog. And the UIAlertController also works.

self.wk.evaluateJavaScript("alert('aa');console.log('1234');var rect = document.getElementById('liveMovie').getBoundingClientRect();[rect.left, rect.top];") {
    (result, error) -> Void in
    if((result) != nil)
    {
        self.player?.view?.frame.origin.x = (result as! Array)[0]
        self.player?.view?.frame.origin.y = (result as! Array)[1]
    }
}

let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
     if(true)
     {
     }
}))

推荐答案

此答案中有一个有效的示例.看来您可能未正确实现WKUIDelegate方法.

There is a working example in this answer. It seems you mightn't have implemented the WKUIDelegate method correctly.

这篇关于alert()在WKWebview中不起作用EvaluationJavaScript()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 15:31