简体   繁体   English

为什么在调用方法stringByEvaluatingJavaScriptFromString时以下Objective-C代码会停止?

[英]Why does the following Objective-C code stop when calling method stringByEvaluatingJavaScriptFromString?

I use the following Objective-C code in my iPhone App: 我在iPhone应用程序中使用以下Objective-C代码:

NSLog(@"Before JS string definition");
NSString* jsString = [NSString stringWithFormat:@"alert('ok');"];
NSLog(@"Before JS call");
@try {
    [self.viewController.webView stringByEvaluatingJavaScriptFromString:jsString];
}
@catch (id theException) {
    NSLog(@"%@", theException);
}
NSLog(@"JS call string was: %@", jsString);

When I run it, I get in the logs: 运行它时,我进入了日志:

2012-12-13 16:10:34.743 MyApp.com[2328:907] Before JS string definition
2012-12-13 16:10:34.744 MyApp.com[2328:907] Before JS call

So, the last NSLog call doesn't get executed. 因此,最后一个NSLog调用不会执行。 Obviously the call to stringByEvaluatingJavaScriptFromString somehow hangs there. 显然,对stringByEvaluatingJavaScriptFromString的调用以某种方式挂在那里。 No error message in the logs. 日志中没有错误消息。 No exception. 没有例外。 Nothing. 没有。

What's going on? 这是怎么回事? How can I debug this? 我该如何调试?

I would expect that alert() is a synchronous call and will not return until after the user has dismissed the message box. 我希望alert()是一个同步调用,直到用户关闭消息框后才返回。 The method to evaluate JavaScript will not return until after the JavaScript has finished. 在JavaScript完成之前,不会返回评估JavaScript的方法。

I can't see how can you benefit from this code (don't see the purpose, you could generate your own alerts, only if you have some complex javascript logic over just a simple and straight alert();). 我看不到如何从此代码中受益(看不到目的,只有在简单且直接的alert();上具有一些复杂的javascript逻辑时,您才能生成自己的警报)。 But note that stringByEvaluatingJavaScriptFromString has a limitation of 10secs for execution and 10mb of ram. 但是请注意, stringByEvaluatingJavaScriptFromString的执行限制为10秒,RAM限制为10mb。 Maybe something here is the issue, but as far as I could remember it gives an error to the log of some kind if these restrictions met. 也许这是问题所在,但据我所记得,如果满足这些限制,它将给某种日志带来错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM