UIWebView 不加载外部 Javascript 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3606272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:41:46  来源:igfitidea点击:

UIWebView doesn't load external Javascript file

javascripthtmluiwebview

提问by Amarsh

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

我有以下看起来无害的代码 (index.html),但它没有加载code.js文件。如果我将文件的内容复制/粘贴到 HTML 页面中,则效果很好。这两个index.htmlcode.js文件在同一目录下。这是代码片段。任何帮助都会很棒。

index.html :

索引.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:

视图控制器.m:

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

回答by Linda

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

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

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.

在 Xcode 中打开项目。转到“目标”并查看“编译源”。如果您在该文件夹中看到您的 js 文件,请将其移动到“复制捆绑资源”文件夹并从“编译源”文件夹中删除它。

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.

如果已安装用于测试的应用程序,请从您的 iPhone 中删除该应用程序。转到 Xcode 中的 Build 菜单并清理所有目标。然后重新编译。

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

现在检查以查看您的 HTML 文件现在是否真的看到了您的 js 文件。

Linda

琳达

回答by skorulis

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.

这样的事情应该对你有用。

回答by NSRover

To supplement Linda's Answer here is where you find the files:

为了补充琳达的答案,您可以在此处找到文件:

enter image description here

在此处输入图片说明

回答by Teja Swaroop

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 文件。

for HTML, Javascript and CSS files

用于 HTML、Javascript 和 CSS 文件

For general XCode files

对于一般 XCode 文件