我正在尝试获取 2 个 .js 文件和一个 .css 文件以通过 HTML 文件(正在找到)加载到 iOS 上的 UIWebView 应用程序中..但它不起作用..
文件已经复制到 Build Phases 等下的 Copy Bundle Resources 上。
这是目前在 ViewController.m 上的代码
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"game1" ofType:@"html" inDirectory:nil];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
//Append javascript
NSString *script = @"<script src>TheScript4.js</script>";
htmlString = [htmlString stringByAppendingString:script];
[self.webview loadHTMLString:htmlString baseURL:nil];
}
我正在运行 xcode6..
在正确方向等方面的任何帮助都会很棒,非常感谢..
最佳答案
您必须指定 baseURL 作为您的包路径
[self.webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
此外,javascript 字符串应该是这样的
NSString *script = @"<script src="TheScript4.js"></script>";
但是您将此脚本附加到 html 文件的末尾,即 </html>
之后结束标记,因此它可能不会执行。
关于javascript - 在 xcode6 中向 UIWebView 添加 2 个 .js 文件和一个 .css 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26306942/