ios - stringByEvaluatingJavaScriptFromString : make my user interface become unresponsive

标签 ios objective-c grand-central-dispatch

我使用 GCD 在 UIWebView 中运行 javascript,当它是正常的 javascipt 时,一切似乎都找到了,但是当涉及到“警报”时,弹出模态视图使我的用户界面变得无响应。

这是我在 UIWebViewDelegate 方法中的代码。

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{

    NSLog(@"RECEIVED");
    BOOL re =[WebParserStrategy shouldConsiderAsRequest:request];
    NSLog(@"--receive Request: %@ jumpTo:%@", request.URL.absoluteString, re == YES ? @"YES" : @"NO!!");
    if(re == NO)
    {
        dispatch_async(parserRequestToMessageQueue, ^{

            NSString *msg;
            NSDictionary *param;
            [WebParserStrategy transferRequest:request toMessage:&msg withParam:&param];
            WebObserverChain *chain = [self.messageObservers objectForKey:msg];
            if(chain == nil)
                ;//NSLog(@"!!!unknow message:%@ not found in message list", msg);
            else {
                dispatch_async(dispatch_get_main_queue(), ^{

                     [self.webView stringByEvaluatingJavaScriptFromString:@"alert('')"];          
                });
            }
        });
}
return re;}

最佳答案

这是因为 Web 警报是同步/阻塞的,而您是在整个应用程序的主队列上执行此操作。只需使用 UIAlertView 或找到一些其他解决方案来解决您尝试通过警报完成的任务。

由于您无法控制 UIWebView 执行 Objective-C 提供的 javascript 的并发性,因此您应该避免进行任何执行速度不是非常非常快的调用。否则,您的整个应用程序的主线程将受制于最不可靠的 UIKit 类之一。

关于ios - stringByEvaluatingJavaScriptFromString : make my user interface become unresponsive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11801773/

相关文章:

objective-c - 如何用apple bonjour for iOS读取TXT记录

mysql - Objective-C Apple Mach-O 链接器错误

objective-c - 如何在 swift 文件中使用 HOST_VM_INFO_COUNT

ios - UIAlertView 中显示未捕获的 NSException

ios - 如何清理 xcode 构建缓存?

iOS - 如何包含可以通过项目的相对路径引用的 javascript 文件?

iOS 在应用程序更新时更新 coreData

ios - 在应用速率大于 2 倍时从 avplayer 播放生涩

ios - 异步 API 调用(解析)是否仅使用 GCD? iOS

swift - 一次多次调用 DispatchSemaphore 的 wait() 是否安全?