javascript - UIWebview javascript 仅在发布版本中失败

标签 javascript ios objective-c uiwebview

我正在尝试将我的 Backbone 应用程序的 javascript 手动注入(inject)到 UIWebview 中(为了避免客户端在应用程序启动时下载 1MB 的 JS 文件)。

下面的代码在应用程序的 DEBUG 版本中运行得非常好,但是一旦构建了 Ad Hoc 发布版本并对其进行测试,应用程序就无法正确加载。我认为这是某处的 JS 错误,但我不知道如何调试在发布版本中运行的 UIWebview(据我所知,Safari 开发工具只能在调试中使用)。

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    // Inject the JS
    NSLog(@"Injecting JS 1 from disk");
    NSString *path = [[NSBundle mainBundle] pathForResource:@"backbone_application" ofType:@"js"];
    NSString *code = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSString *jsCode = [NSString stringWithFormat:@"var script=document.createElement('script'); script.type='text/javascript'; script.text=\"%@\"; document.head.appendChild(script);", code];
    [self.webPortal stringByEvaluatingJavaScriptFromString:jsCode]
}

关于为什么这会在 RELEASE 模式下失败的任何想法? 关于如何在发布版本中测试 UIWebview javascript 错误的任何建议?

最佳答案

尝试删除以下行:

NSString *jsCode = [NSString stringWithFormat:@"var script=document.createElement('script'); script.type='text/javascript'; script.text=\"%@\"; document.head.appendChild(script);", code];

所以你会得到这个:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    // Inject the JS
    NSLog(@"Injecting JS 1 from disk");
    NSString *path = [[NSBundle mainBundle] pathForResource:@"backbone_application" ofType:@"js"];
    NSString *code = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    [self.webPortal stringByEvaluatingJavaScriptFromString:code]
}

关于javascript - UIWebview javascript 仅在发布版本中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18305872/

相关文章:

javascript - 在 Yii 中,将 PHP 变量传递给 JavaScript

javascript - 如何监听点击事件并在点击时找到父div?

ios - 在 UITableViewCell 的 UIButton 位置插入 UIView

iOS - NSTimeZone 的 3 个字母时区缩写

objective-c - 为什么我应该使用 Aspect Ratio NSLayoutConstraint?

javascript - AJAX 调用后 PHP 没有返回响应

javascript - nightwatchjs - 如何等到ajax调用完成

iOS 应用程序在安装和中断安装期间显示错误警报

ios - 视频全屏在 ios Swift AVPlayer 中不起作用?

ios - UIButton 的事件 touchUpInside 不起作用