2012-07-13 14 views
10

continuo a ricevere questo errore in JavaScript quando si tenta di passare un po 'JSON per un UIWebView:“SyntaxError: EOF imprevisto” quando si valuta JavaScript in iOS UIWebView

SyntaxError: Unexpected EOF

Non c'è un numero di linea o il nome del file disponibile in window.onerror ma Ho già controllato tutti i file di riferimento, e stanno bene.

sto usando MonoTouch EvaluateJavaScript metodo che è equivalente a objC stringByEvaluatingJavaScriptFromString::

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

funziona bene su input JSON “semplice”, ma si rompe a oggetti più grandi.
Cosa potrebbe andare storto?

risposta

14

Prima di passare un NSString ad un UIWebView, essere sicuri di sfuggire a capo così come le doppie virgolette/singoli:

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

Nel mio caso (MacOS 10.11+, 4.x Swift), in sostituzione di '\ '' con '\\\ '' non è necessario. – keyOfVv

Problemi correlati