【问题标题】:"Inject" Objective-C data into a UIWebView that loads a local HTML file?将 Objective-C 数据“注入”到加载本地 HTML 文件的 UIWebView 中?
【发布时间】:2011-12-10 14:55:03
【问题描述】:

我正在尝试使用本地 HTML/CSS 加载 UIWebView,该 UIWebView 构建为看起来像营养标签。问题是,食物的数据位于我的 iPhone 应用程序中。我是否必须将我所有的 HTML 放入一个巨大的 NSString 对象并将我的数据连接到其中,或者是否有一种方法可以从本地 .html 文件加载 HTML,但以某种方式“注入”存储在 Objective- C进了吗?

【问题讨论】:

    标签: iphone objective-c uiwebview


    【解决方案1】:

    如果要注入的数据是“安全的”,您可以将“巨大的 NSString 对象”构造为格式字符串,并用%@ 标记,然后使用stringWithFormat: 一次性执行注入。这就是我在 TidBITS 新闻应用程序中构建页面的方式,使用的片段都来自 RSS。真的很无痛。

    【讨论】:

    • 是的,我本来打算使用stringWithFormat:,但我只是希望我不必这样做。当我压缩 HTML 时,它并没有那么大,所以我认为这就是我要使用的。
    【解决方案2】:

    您可以使用 NSData 的方法 dataWithContentsOfFile 加载基本的 html,然后使用 javascript 以您需要的方式修改 html。

    代码看起来像这样(使用这个example):

    NSString *path = [[NSBundle mainBundle] pathForResource:@"food" ofType:@"html"]; 
    NSData *data = [NSData dataWithContentsOfFile:path];
    if (data) {  
        [webView loadData:data MIMEType:@"text/html" textEncodingName:@"UTF-8"];  
    }
    [webView stringByEvaluatingJavaScriptFromString:@"var script = document.createElement('script');"
                         "script.type = 'text/javascript';"
                         "script.text = \"function myFunction() { "
                            "var field = document.getElementById('field_3');"
                            "field.value='Calling function - OK';"
                         "}\";"
                         "document.getElementsByTagName('head')[0].appendChild(script);"];
    
    [webView stringByEvaluatingJavaScriptFromString:@"myFunction();"];
    

    【讨论】:

      【解决方案3】:

      我会做两者的混合 - 在您加载的应用程序中拥有一个 HTML 文件,然后在将其提供给 UIWebView 之前替换其中的某些字符串。因此,例如,您可以拥有这样的文件

      <html>
      <head>
      <title><!--foodName--></title>
      </head>
      <body>
      <h1><!--foodName--></h1>
      <p>Calories / 100g: <!--foodCalories--></p>
      </body>
      </html>
      

      您可以将其加载到 Cocoa 中,然后将您的特殊占位符 cmets 替换为您想要的实际值。

      NSDictionary *substitutions = [NSDictionary dictionaryWithObjectsAndKeys: 
                                     @"Carrots", @"foodName",
                                     [NSNumber numberWithInt:20], @"foodCalories",
                                     // add more as needed
                                     nil];
      
      NSMutableString *html = [NSMutableString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"foodCard" ofType:@"html"] 
                                                               encoding:NSUTF8StringEncoding
                                                                  error:nil];
      
      for(NSString *substitutionKey in substitutions)
      {
          NSString *substitution = [[substitution objectForKey:substitutionKey] description];
          NSString *searchTerm = [NSString stringWithFormat:@"<!--%@-->", substitutionKey];
      
          [html replaceOccurrencesOfString:searchTerm withString:substitution options:0 range:NSMakeRange(0, [html length])];
      }
      
      [webView loadHTMLString:html baseURL:[[NSBundle mainBundle] resourceURL]];
      

      【讨论】:

        【解决方案4】:

        从 iOS 2 开始,您可以在 UIWebView 子类中使用 - (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script 在您的 webview 中执行 JS 脚本。这是从应用程序的“Objective-C 部分”注入数据的最佳方式。

        参考:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIWebView_Class/#//apple_ref/occ/instm/UIWebView/stringByEvaluatingJavaScriptFromString

        【讨论】:

          猜你喜欢
          • 2011-06-06
          • 2011-10-27
          • 2018-05-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-02
          • 2021-04-21
          相关资源
          最近更新 更多