Ama*_*rsh 27 javascript html5 uiwebview
我有以下无辜的代码(index.html),但它不加载该code.js文件.如果我在HTML页面中复制/粘贴文件的内容,它的效果很好.两者index.html和code.js文件都在同一目录中.这是代码段.任何帮助都会很棒.
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>
Run Code Online (Sandbox Code Playgroud)
ViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
// load the first webpage
[homePageWebView loadRequest:[NSURLRequest requestWithURL:[NSURLfileURLWithPath:
[[NSBundle mainBundle] pathForResource:@"index"ofType:@"html"] isDirectory:NO]]];
}
Run Code Online (Sandbox Code Playgroud)
Lin*_*nda 60
我发现Xcode认为需要编译js文件.
在Xcode中打开项目.转到"目标"并查看"编译源".如果你看到你的js文件夹中,将它移动到"复制包资源"文件夹,从"编译源"文件夹中删除.
如果iPhone已安装用于测试,请从iPhone中删除该应用.转到Xcode中的Build菜单并清除所有目标.然后重新编译.
立即检查您的HTML文件是否实际看到您的js文件.
琳达
您需要将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];
}
Run Code Online (Sandbox Code Playgroud)
这样的事情对你有用.
我也遇到了同样的问题:我在将文件添加到XCode时通过简单的操作克服了它我做了一个小的更改,如下图所示:
在下图中,一个用于添加HTML,Javascript和CSS等文件,第二个用于一般XCode文件.


| 归档时间: |
|
| 查看次数: |
16309 次 |
| 最近记录: |