UIWebView 不加载外部 Javascript 文件

2022-01-19 00:00:00 javascript html uiwebview

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

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:

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:

ViewController.m:

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

推荐答案

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

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

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

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.

如果您的 iPhone 已经安装了该应用以进行测试,请从您的 iPhone 中删除该应用.转到 Xcode 中的 Build 菜单并 Clean all Targets.然后重新编译.

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.

现在检查你的 HTML 文件是否真的能看到你的 js 文件.

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

琳达

相关文章