在iOS UIWebView中评估JavaScript时出现“SyntaxError:Unexpected EOF” [英] “SyntaxError: Unexpected EOF” when evaluating JavaScript in iOS UIWebView

查看:732
本文介绍了在iOS UIWebView中评估JavaScript时出现“SyntaxError:Unexpected EOF”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将一些JSON传递给 UIWebView 时,我在JavaScript中一直收到此错误:

I keep getting this error in JavaScript when trying to pass some JSON to a UIWebView:


SyntaxError:意外的EOF

SyntaxError: Unexpected EOF

window.onerror中没有可用的行号或文件名但我已经检查了所有引用的文件,它们没问题。

There is no line number or filename available in window.onerror but I've already checked all referenced files, and they are fine.

我正在使用MonoTouch EvaluateJavaScript 方法,相当于ObjC stringByEvaluatingJavaScriptFromString:

I'm using MonoTouch EvaluateJavaScript method which is equivalent to ObjC stringByEvaluatingJavaScriptFromString::

webView.EvaluateJavascript(
    "Viewer.init($('#page'), " + json.ToString() + ");"
);

它在简单JSON输入上工作得很好但在较大的对象处断开。

什么可能出错?

It works just fine on "simple" JSON input but breaks at larger objects.
What could go wrong?

推荐答案

在将NSString传递给UIWebView之前,请确保转义换行符以及单个/双引号:

Before passing an NSString to a UIWebView, be sure to escape newlines as well as single/double quotes:

NSString *html = @"<div id='my-div'>Hello there</div>";

html = [html stringByReplacingOccurrencesOfString:@"\'" withString:@"\\\'"];
html = [html stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
html = [html stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"];
html = [html stringByReplacingOccurrencesOfString:@"\r" withString:@""];

NSString *javaScript = [NSString stringWithFormat:@"injectSomeHtml('%@');", html];
[_webView stringByEvaluatingJavaScriptFromString:javaScript];

这篇关于在iOS UIWebView中评估JavaScript时出现“SyntaxError:Unexpected EOF”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆