无法从UIWebView中的Resource加载JavaScript

时间:2022-09-24 23:13:08

I am loading a local HTML file into a UIWebView in iOS using this code

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

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

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

[myWebView loadRequest:requestObj];

The page works fine, and the image linked within the HTML loads fine. However, the JavaScript linked off of the page is not loading.

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

The HTML, Image and JavaScript are all located in the same folder. (Resources/Html in my Project)

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

How can I get the JavaScript to load?

如何加载JavaScript?

2 个解决方案

#1


7  

The problem was that XCode was showing me a warning:

问题是XCode向我发出警告:

warning: no rule to process file '$(PROJECT_DIR)/html/orientation.js' of type sourcecode.javascript for architecture i386

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

This answer contains the solution. Basically, I had to tell XCode to not try and build the JavaScript files, and manually drag them to the "Copy Bundle Resources" directory.

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

#2


0  

The following code works for loading an html file which has javascript files in the web view Here web is web view object and index is the html page

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

Web.delegate = self;

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

#1


7  

The problem was that XCode was showing me a warning:

问题是XCode向我发出警告:

warning: no rule to process file '$(PROJECT_DIR)/html/orientation.js' of type sourcecode.javascript for architecture i386

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

This answer contains the solution. Basically, I had to tell XCode to not try and build the JavaScript files, and manually drag them to the "Copy Bundle Resources" directory.

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

#2


0  

The following code works for loading an html file which has javascript files in the web view Here web is web view object and index is the html page

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

Web.delegate = self;

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