UIWebView不加载外部Javascript文件 [英] UIWebView doesn't load external Javascript file

查看:81
本文介绍了UIWebView不加载外部Javascript文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下无辜的代码(index.html),但它不加载 code.js 文件。如果我在HTML页面中复制/粘贴文件的内容,效果会很好。 index.html code.js 文件都位于同一个目录中。这是代码片段。任何帮助都会很好。

index.html:

 < ; HTML> 
< head>
< script type =text / javascriptsrc =code.js>< / script>
< / head>
< body onload =drawImage()>
< div>< canvas id =canvaswidth =320height =480>< / canvas>< / div>
< / body>
< / html>

ViewController.m:

   - (void)viewDidLoad {
[super viewDidLoad];
//加载第一个网页
[homePageWebView loadRequest:[NSURLRequest requestWithURL:[NSURLfileURLWithPath:
[[NSBundle mainBundle] pathForResource:@indexofType:@html] isDirectory:NO ]]];
}


解决方案

我发现Xcode认为需要编译js文件。



在Xcode中打开项目。转到目标并查看编译源代码。如果您在该文件夹中看到js文件,请将其移至Copy Bundle Resources文件夹并从Compile Sources文件夹中删除。



从以下位置删除应用程序你的iPhone,如果它已经安装测试。转到Xcode中的Build菜单并清除所有目标。然后重新编译。



立即检查您的HTML文件是否实际看到您的js文件。



Linda


I have the following innocent-looking code (index.html), but it doesn't load the code.js file. It works well if I copy/paste the contents of the file in the HTML page. Both index.html and code.js files are in the same directory. This is the code snippet. Any help would be great.

index.html :

<html>
<head>
<script type="text/javascript" src="code.js"></script>
</head> 
<body onload="drawImage()">
  <div><canvas id="canvas" width="320" height="480"></canvas></div> 
</body>
</html>

ViewController.m:

- (void)viewDidLoad {
  [super viewDidLoad];
// load the first webpage
[homePageWebView loadRequest:[NSURLRequest requestWithURL:[NSURLfileURLWithPath:
  [[NSBundle mainBundle] pathForResource:@"index"ofType:@"html"] isDirectory:NO]]];
}

解决方案

I have found that Xcode thinks that the js file needs to be compiled.

Open the project in Xcode. Go to "Targets" and look in "Compile Sources". If you see your js file in that folder, move it to the "Copy Bundle Resources" folder and delete it from the "Compile Sources" folder.

Delete the app from your iPhone if it is installed already for testing. Go to the Build menu in Xcode and Clean all Targets. Then recompile.

Check now to see if your HTML file actually sees your js file now.

Linda

这篇关于UIWebView不加载外部Javascript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆