为什么我不能在 UIWebView 中关闭或关闭 Javascript 警报?

Why can#39;t I close or dismiss a Javascript alert in UIWebView?(为什么我不能在 UIWebView 中关闭或关闭 Javascript 警报?)

本文介绍了为什么我不能在 UIWebView 中关闭或关闭 Javascript 警报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:我通过 UIWebView 方法 stringByEvaluatingJavaScriptFromString: 像这样调用 Javascript alert...

Situation: I invoke a Javascript alert through the UIWebView method stringByEvaluatingJavaScriptFromString: like this...

[myWebView stringByEvaluatingJavaScriptFromString:@"alert('FOOBAR');"];

问题: iOS 按预期显示FOOBAR"提示,但点击关闭"按钮不会关闭提示.

Problem: iOS displays an alert saying "FOOBAR" as expected, but tapping on the "Close" button does not dismiss the alert.

为什么我无法关闭 Javascript 警报?如何让它关闭?

Why can't I close the Javascript alert? How do I get it to close?

推荐答案

这个问题让我对问题有了最深刻的理解……

This question gave me the most insight to the problem...

GCD 和 webView 的死锁

要点是处理 stringByEvaluatingJavaScriptFromString: 方法中的 JS 的线程和处理 iOS 警报视图的线程可能相互阻塞,导致关闭"按钮无响应.

The gist is that the thread handling the JS from the stringByEvaluatingJavaScriptFromString: method and the thread handling the iOS alert view are probably blocking each other, making the "Close" button unresponsive.

我的解决方法是用 setTimeout 推迟 JS alert,类似这样...

My workaround is to defer the JS alert with a setTimeout, something like this...

NSString *jsMyAlert = @"setTimeout(function(){alert('FOOBAR');}, 1);";

[myWebView stringByEvaluatingJavaScriptFromString:jsMyAlert];

为了避免任何死锁风险,最好让 UIWebView 触发 UIAlertView 而不是依靠 UIWebView 来处理JS 警报.不过,上述解决方法适用于大多数调试目的.

To avoid any risk of deadlock, it might be better to have the UIWebView trigger an UIAlertView rather than rely on UIWebView to handle the JS alert. The workaround above would be suitable for most debugging purposes though.

这篇关于为什么我不能在 UIWebView 中关闭或关闭 Javascript 警报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:为什么我不能在 UIWebView 中关闭或关闭 Javascript 警报?

基础教程推荐