问题描述
情况:我通过 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 警报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)