最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - alert() not working in WKWebview evaluateJavaScript() - Stack Overflow

programmeradmin4浏览0评论

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.

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
    }
    ...
}

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.

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

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.

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
    }
    ...
}

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.

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)
     {
     }
}))
Share Improve this question edited Jun 2, 2017 at 2:22 LF-DevJourney asked Apr 25, 2017 at 2:33 LF-DevJourneyLF-DevJourney 28.5k30 gold badges166 silver badges316 bronze badges 6
  • 1 You have no mention of the runJavaScriptAlertPanelWithMessage delegate method. Can add that code to your question? – Onato Commented Jun 1, 2017 at 21:52
  • I didn't use that method in my code. The UIAlertController can works in my code, but the evaluateJavaScript can not alert() – LF-DevJourney Commented Jun 2, 2017 at 1:56
  • 1 That's why it doesn't work then. This method is called when javascript fires and alert(). It is then up to you in this method to decide what to do about it. See the example in the answer linked below. – Onato Commented Jun 2, 2017 at 1:58
  • To sum what Onato means: The linked question is relevant. The flow would be like this: You evaluate JS with an alert(), this triggers the webview to call its delegate's runJavaScriptAlertPanelWithMessage. If that is not implemented, the alert won't open. Whether you call the JS alert() from something passed to evaluateJavaScript or the site calls it itself (i.e. it's already in its code) doesn't matter. I wonder why you'd believe there's a difference? – Gero Commented Jun 2, 2017 at 7:25
  • 1 @KrisRoofe No problem, it's all good. Just wanted to make sure you get a solution (and Onato the well-deserved accept). :) WKWebView is indeed still a bit weird in some places (more powerful than UIWebView, but still not done with everything, sadly) so it can be confusing. – Gero Commented Jun 2, 2017 at 11:30
 |  Show 1 more comment

3 Answers 3

Reset to default 4 +50

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

First of all you need to implement the required WKUIDelegate methods, in your case you need to implement:

optional func webView(_ webView: WKWebView, 
    runJavaScriptAlertPanelWithMessage message: String, 
         initiatedByFrame frame: WKFrameInfo, 
        completionHandler: @escaping () -> Void)
{
    // parameter **message** will hold your actual alert message.

    // Write Your Customised code to display the alert message
}

It's my fault here. I've not much experience with WKWebview. I've experience with android's XWalkview, and there execute alert needn't to implement alert with java code. So here I also missed thought I don't need to implementation the alert delegate with swift.

From answer of Onato, I learned how swift execute the alert prompt and confirm, I lost the implementation of these delegate. So I refer to this answer, add below implementation, everything works.

func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo,
             completionHandler: @escaping () -> Void) {

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

    present(alertController, animated: true, completion: nil)
}


func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo,
             completionHandler: @escaping (Bool) -> Void) {

    let alertController = UIAlertController(title: nil, message: message, preferredStyle: .actionSheet)

    alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
        completionHandler(true)
    }))

    alertController.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (action) in
        completionHandler(false)
    }))

    present(alertController, animated: true, completion: nil)
}


func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo,
             completionHandler: @escaping (String?) -> Void) {

    let alertController = UIAlertController(title: nil, message: prompt, preferredStyle: .actionSheet)

    alertController.addTextField { (textField) in
        textField.text = defaultText
    }

    alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
        if let text = alertController.textFields?.first?.text {
            completionHandler(text)
        } else {
            completionHandler(defaultText)
        }
    }))

    alertController.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (action) in
        completionHandler(nil)
    }))

    present(alertController, animated: true, completion: nil)
}
发布评论

评论列表(0)

  1. 暂无评论