简体   繁体   English

Objective-c [For循环]在UIWebview中加载HTML并从JavaScript函数检索数据

[英]Objective-c [For loop] load HTML in UIWebview and retrieve data from JavaScript function

I'm making an app in Objective-c for IOS and I need to be able to loop through a bunch of web pages and, using an embedded javascript function return some JSON values. 我正在用Objective-c for IOS开发一个应用程序,我需要能够循环浏览一堆网页,并使用嵌入式javascript函数返回一些JSON值。

I use the pages elsewhere in the app and the javascript works as required. 我在应用程序的其他地方使用了这些页面,并且javascript可以按要求工作。 At this point I do store the JSON response but as user might not view every page the app wont necessarily have all the JSON values to check. 在这一点上,我确实存储了JSON响应,但是由于用户可能无法查看每个页面,因此该应用不一定要检查所有JSON值。

The section of code below is what I'm using to loop through all the pages, load in the HTML string and run the javascript function. 下面的代码部分是我用来遍历所有页面,加载HTML字符串并运行javascript函数的内容。 The problem is that the values always come back empty. 问题在于这些值始终返回为空。

-(void)webViewDidFinishLoad:(UIWebView *)webView {
    NSLog(@"finished loading");
    NSString *jsonValues = [webView stringByEvaluatingJavaScriptFromString:@"getFields()"];
}

for (int i = 1; i <= shifts; i++)
    {
        ontracMultipleWevViewController *RT9909 = [viewControllerDictionary objectForKey:[NSString stringWithFormat:@"Shift %d RT9909", i]];
        if (RT9909 && [RT9909.viewControllers count] > 0) {
            for (ontracRTViewController *r9909 in RT9909.viewControllers) {

                 UIWebView *webView = [[UIWebView alloc] init];
                 [webView setDelegate: self];
                 [webView loadHTMLString:r9909.htmlString baseURL:[NSURL URLWithString:r9909.urlToLoad]];
                [self webViewDidFinishLoad:webView];
                [self.view addSubview:webView];

                if([self webViewDidFinishLoad:webView]){
                    r9909.jsonValues = [webView stringByEvaluatingJavaScriptFromString:@"getFields()"];

                   if(r9909.jsonValues != nil && ![r9909.jsonValues isEqualToString:@""] && rt9909ShiftCount == 0){

                   ........rest of code

NOTE: I get no error or NSLogs to suggest any problems 注意:我没有错误或NSLogs提示任何问题

You need to segregate the 您需要隔离

if([self webViewDidFinishLoad:webView]){
    r9909.jsonValues = [webView stringByEvaluatingJavaScriptFromString:@"getFields()"];

block out of the for-loop & add tags to your webviews. 阻止for-loop并向您的webview添加tags


Implement the 实施

- (void)webViewDidFinishLoad:(UIWebView *)webView;

optional delegate function of UIWebView & put in your UIWebView可选委托函数并放入您的


if(...) {
      r9909.jsonValues = [webView stringByEvaluatingJavaScriptFromString:@"getFields()"];
 }


logic here. 逻辑在这里。
The if check would now be based on the webview.tag that you will assign will creating the webviews inside the for loop . if check现在将基于该webview.tag ,你将分配会产生内部的网页视图for loop

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

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