【问题标题】:Adding 2 .js files and a .css file to UIWebView in xcode6在 xcode6 中将 2 个 .js 文件和一个 .css 文件添加到 UIWebView
【发布时间】:2014-10-10 19:42:09
【问题描述】:

我正在尝试通过 HTML 文件(正在找到)获取 2 个 .js 文件和一个 .css 文件以加载到 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..

任何在正确方向等方面的帮助都会很棒,非常感谢..

【问题讨论】:

    标签: javascript ios css objective-c uiwebview


    【解决方案1】:

    你必须指定 baseURL 作为你的包路径

    [self.webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
    

    另外,javascript 字符串应该是这样的

    NSString *script = @"<script src="TheScript4.js"></script>";
    

    但是您将此脚本附加到 html 文件的末尾,该文件将在 &lt;/html&gt; 结束标记之后,因此它可能无法执行。

    【讨论】:

      猜你喜欢
      • 2012-12-26
      • 2017-10-10
      • 2013-03-22
      • 2014-10-29
      • 1970-01-01
      • 2020-12-17
      • 2020-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多