1

我在我的 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);

当我运行它时,我会进入日志:

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

因此,最后一次 NSLog 调用不会被执行。显然,对stringByEvaluatingJavaScriptFromString的调用以某种方式挂在那里。日志中没有错误消息。没有例外。没有。

这是怎么回事?我该如何调试呢?

4

2 回答 2

1

我希望 alert() 是一个同步调用,并且在用户关闭消息框之前不会返回。在 JavaScript 完成之前,评估 JavaScript 的方法不会返回。

于 2012-12-13T15:28:01.157 回答
0

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();). But note that stringByEvaluatingJavaScriptFromString has a limitation of 10secs for execution and 10mb of ram. 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.

于 2012-12-13T15:32:30.697 回答