Toni lee
Toni lee

Reputation: 485

stringByEvaluatingJavaScriptFromString: make my user interface become unresponsive

I use GCD to run javascript in UIWebView, when it is normal javascipt, everything seems find, but when it comes to "alert", the popup modal view make my user interface become unresponsive.

here is my code in UIWebViewDelegate method.

    - (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;}

Upvotes: 3

Views: 731

Answers (1)

mamackenzie
mamackenzie

Reputation: 1166

This is because web alerts are synchronous/blocking, and you are doing this on the main queue of the whole application. Just use a UIAlertView or find some other solution to what you are trying to accomplish with the alert.

Since you have NO control whatsoever over the concurrency in UIWebView's execution of Objective-C provided javascript, you should avoid making ANY calls that aren't very, very fast to execute. Otherwise your whole app's main thread will be at the mercy of one of the most notoriously unreliable UIKit classes.

Upvotes: 1

Related Questions