为什么我不能在UIWebView中关闭或关闭Javascript警报? [英] Why can't I close or dismiss a Javascript alert in UIWebView?

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

问题描述

情况:我通过 UIWebView 方法<$ c调用Javascript 提醒 $ c> stringByEvaluatingJavaScriptFromString:喜欢这个......

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.

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

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警报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆