【问题标题】:Can't load JavaScript from Resource in UIWebView无法从 UIWebView 中的资源加载 JavaScript
【发布时间】:2011-02-06 02:52:41
【问题描述】:

我正在使用此代码将本地 HTML 文件加载到 iOS 中的 UIWebView 中

NSString *urlAddress = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];

NSURL *url = [NSURL fileURLWithPath:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

[myWebView loadRequest:requestObj];

页面工作正常,HTML 中链接的图像加载正常。但是,从页面链接的 JavaScript 没有加载。

HTML、图像和 JavaScript 都位于同一个文件夹中。 (我项目中的资源/Html)

如何让 JavaScript 加载?

【问题讨论】:

    标签: iphone ipad ios uiwebview


    【解决方案1】:

    问题是 XCode 向我显示警告:

    警告:没有规则来处理文件 '$(PROJECT_DIR)/html/orientation.js' sourcecode.javascript 类型的 架构 i386

    This answer 包含解决方案。基本上,我不得不告诉 XCode 不要尝试构建 JavaScript 文件,并手动将它们拖到“Copy Bundle Resources”目录。

    【讨论】:

      【解决方案2】:

      以下代码用于加载在 web 视图中包含 javascript 文件的 html 文件 这里 web 是 web 视图对象, index 是 html 页面

      Web.delegate = self;
      
      [Web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-09
        • 2013-08-15
        • 1970-01-01
        • 2018-06-01
        • 1970-01-01
        • 2012-07-26
        • 2013-03-15
        • 1970-01-01
        相关资源
        最近更新 更多