2012-07-13 17 views
10

sigo recibiendo este error en JavaScript cuando se trata de pasar un poco de JSON a un UIWebView:“SyntaxError: EOF inesperado” en la evaluación de JavaScript en iOS UIWebView

SyntaxError: Unexpected EOF

No hay un número de línea o nombre de archivo disponible en window.onerror pero Ya he comprobado todos los archivos referenciados, y están bien.

estoy usando MonoTouch EvaluateJavaScript método que es equivalente a ObjC stringByEvaluatingJavaScriptFromString::

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

Funciona muy bien en la entrada JSON “simple”, pero se rompe en objetos de mayor tamaño.
¿Qué podría salir mal?

Respuesta

14

Antes de pasar una NSString a un UIWebView, asegúrese de evitar los saltos de línea, así como cotizaciones individuales/dobles:

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]; 
+0

En mi caso (macOS 10.11+, 4.x Swift), en sustitución de '\ '' con '\\\ '' no es necesario. – keyOfVv

Cuestiones relacionadas