简体   繁体   English

从Objective-C将大数据传递到UIWebView JavaScript

[英]Passing large data to UIWebView javascript from Objective-C

I've got an app which uses a UIWebView to display an HTML page, loaded off a server. 我有一个应用程序,该应用程序使用UIWebView来显示从服务器加载的HTML页面。 This works great, including the Objective-C part that loads additional data, however, what does not work is passing data to the JavaScript running in the WebView. 这非常有用,包括用于加载其他数据的Objective-C部分,但是,不起作用的是将数据传递给WebView中运行的JavaScript。

What I've tried is: 我试过的是:

NSString * sigstring = [NSString stringWithFormat:@"updatedSig('%@');", [thisSignature base64EncodedString]];
sigstring = [sigstring stringByReplacingOccurrencesOfString:@"\n" withString:@""];

NSLog(@"WV: %@",[[[(AppDelegate*)[[UIApplication sharedApplication] delegate] viewController] webView] stringByEvaluatingJavaScriptFromString:sigstring]);

The updatedSig function echos to the console whatever is passed in. 无论传入什么,updatedSig函数都会向控制台回显。

However, in this case, it returns nothing to the console, when there should be data there. 但是,在这种情况下,当那里应该有数据时,它不会向控制台返回任何内容。

To test for other issues, I've also tried 为了测试其他问题,我也尝试过

NSString * sigstring = [NSString stringWithFormat:@"updatedSig('%@');", @"AbsolutelyAnything"];
sigstring = [sigstring stringByReplacingOccurrencesOfString:@"\n" withString:@""];

NSLog(@"WV: %@",[[[(AppDelegate*)[[UIApplication sharedApplication] delegate] viewController] webView] stringByEvaluatingJavaScriptFromString:sigstring]);

This worked perfectly, it displays "AbsolutelyAnything" in the console just fine. 效果很好,它在控制台中显示“ AbsolutelyAnything”就好。

Any help would be much appreciated. 任何帮助将非常感激。

Is your updated signature greater than 10MB or take more than 10 seconds to execute? 您更新后的签名是否大于10MB或执行超过10秒? The documentation mentions those limitations. 文档提到了这些限制。

Also keep in mind that the result of stringByEvaluatingJavaScriptFromString is the executed Javascript, so if something goes wrong while executing you will likely just receive nil. 还请记住, stringByEvaluatingJavaScriptFromString的结果是已执行的Javascript,因此,如果在执行过程中出现问题,您可能只会收到nil。 Is the NSLog executing at all in the first example? 在第一个示例中,NSLog是否完全执行?

Assuming you are obeying the limitations, my recommendation would be to insert alerts in the Javascript to ensure that it's executing as you expect. 假设您遵守这些限制,我的建议是在Javascript中插入警报,以确保其按预期执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM