简体   繁体   English

UIWebView不加载外部Javascript文件

[英]UIWebView doesn't load external Javascript file

I have the following innocent-looking code (index.html), but it doesn't load the code.js file. 我有以下无辜的代码(index.html),但它不加载code.js文件。 It works well if I copy/paste the contents of the file in the HTML page. 如果我在HTML页面中复制/粘贴文件的内容,它的效果很好。 Both index.html and code.js files are in the same directory. index.htmlcode.js文件都在同一目录中。 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]]];
}

I have found that Xcode thinks that the js file needs to be compiled. 我发现Xcode认为需要编译js文件。

Open the project in Xcode. 在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. 如果在该文件夹中看到js文件,请将其移至“Copy Bundle Resources”文件夹并从“Compile Sources”文件夹中删除。

Delete the app from your iPhone if it is installed already for testing. 如果iPhone已安装用于测试,请从iPhone中删除该应用。 Go to the Build menu in Xcode and Clean all Targets. 转到Xcode中的Build菜单并清除所有目标。 Then recompile. 然后重新编译。

Check now to see if your HTML file actually sees your js file now. 立即检查您的HTML文件是否实际看到您的js文件。

Linda 琳达

You need to read the html file into a string and set the baseURL when loading so relative paths can be understood. 您需要将html文件读入字符串并在加载时设置baseURL,以便了解相对路径。

NSError* error;
NSString* html = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"] encoding:NSASCIIStringEncoding error:&error];

[webview loadHTMLString:html baseURL:[self getBaseURL]];

- (NSURL*) getBaseURL {
    NSString* path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
    NSRange r = [path rangeOfString:@"/" options:NSBackwardsSearch];
    path = [path substringToIndex:r.location];

    path = [path stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
    path = [path stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    path = [NSString stringWithFormat:@"file:/%@//",path];
    return  [NSURL URLWithString:path];
}

Something like that should work for you. 这样的事情对你有用。

To supplement Linda's Answer here is where you find the files: 为了补充Linda的答案,您可以在这里找到文件:

在此输入图像描述

I also faced the same problem: I overcame it by doing the simple thing when adding the files to XCode I did a small change that is shown in the figure below: 我也遇到了同样的问题:我在将文件添加到XCode时通过简单的操作克服了它我做了一个小的更改,如下图所示:

In the figure below, one is for adding HTML, Javascript and CSS like files and the second is for general XCode files. 在下图中,一个用于添加HTML,Javascript和CSS等文件,第二个用于一般XCode文件。

用于HTML,Javascript和CSS文件

对于一般的XCode文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM