【问题标题】:How to load .js file into HTML in UIWebview in Objective-C如何在Objective-C的UIWebview中将.js文件加载到HTML中
【发布时间】:2018-05-04 02:48:18
【问题描述】:

我正在现有的 iOS 应用中制作一些波浪动画。它由Objective-C语言实现。 所以,我有 .Js 和 HTML 页面。在 HTML 页面中,我正在加载 .js 文件并尝试使用 UIWebview 加载 HTML 文件。

但是,总是显示空白屏幕,我也搜索了很多论坛,我发现只有加载HTML页面才能找到,我没有找到将.js文件加载到UIWebview的HTML页面。

// 以下是我的代码。

@property (nonatomic, weak) IBOutlet UIWebView *animationView;

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *filePathh=[[NSBundle mainBundle]pathForResource:@"voicewave" ofType:@"html" inDirectory:nil];

    NSLog(@"%@",filePathh);
    NSString *htmlstring=[NSString stringWithContentsOfFile:filePathh encoding:NSUTF8StringEncoding error:nil];

 //   [_animationView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePathh]]];
    [_animationView loadHTMLString:htmlstring baseURL:nil];
}

而HTML代码是

<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <style>
        .container {
        background: rgba(232, 17, 17, 0);
        }
    </style>
<body>
<div id="container-ios9" class="container"></div>
<script src="wave9.js"></script>
<script>
         var $wave_ios9 = document.getElementById('container-ios9');
         var SW9 = new Wave9({
                         height: 40,
                         speed: 0.2,
                         amplitude: 0.1,
                         container: $wave_ios9,
                         autostart: false,
         });
      </script>
</body>
</html>

谁能建议我,如何解决这个问题并在 Webview 中加载我的动画代码。即使我尝试过使用 WkWebview,但结果还是一样。

如果它只有一个没有 .js 的 Html 文件,它会加载,但是,如果我将 .js 文件包含到 html 文件中,它会显示空白屏幕。

【问题讨论】:

  • @Savitha 对这个问题有什么想法吗?

标签: html ios objective-c iphone webview


【解决方案1】:

您的 HTML 文件看起来不错

使用loadRequest而不是loadHTMLString将其加载到Webview中

    - (void)viewDidLoad {
        [super viewDidLoad];
        NSString *filePathh=[[NSBundle mainBundle]pathForResource:@"voicewave" ofType:@"html" inDirectory:nil];

        NSLog(@"%@",filePathh);
        NSURL *url = [NSURL fileURLWithPath: filePathh];
        NSURLRequest* request = [NSURLRequest requestWithURL:url];
        [_animationView loadRequest:request];
    }

确保 .html 和 .js 文件在同一个文件夹中,并将此文件夹添加到您的项目中 - 出现提示时选择创建文件夹引用,而不是组。

应该这样做。

【讨论】:

  • 我跟着你的步骤,但是,还是一样的空白屏幕来了。
  • 你在浏览器中测试过吗?在您的 xcode 项目中,选择 html 文件 -> 在 finder 中显示。在 Safari 中打开它,看看它是否可以正常工作。
  • 我终于发现,它的问题是 HTML 没有正确加载 .js 文件。
  • 你的js运行成功了吗?因为对我来说,html文件正在运行,但js由于某种原因无法正常工作。
【解决方案2】:

我正在使用下面的代码加载本地 html 文件和 html 中包含的 js。它的工作没有任何问题。可能你遇到的问题是加载html字符串的时候没有设置baseUrl,所以webview不知道去哪里找js文件。

NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"html_filename" ofType:@"html"];
NSString *htmlStr = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
WKWebView *wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[wkWebView loadHTMLString:htmlStr baseURL:[NSURL fileURLWithPath:[htmlPath stringByDeletingLastPathComponent]]];

【讨论】:

  • 同样的空白屏幕来了。
【解决方案3】:

问题是 js 文件代码没有正确加载 HTML 文件。这就是问题所在。我们终于修好了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-21
    • 2011-06-06
    • 2011-10-27
    • 2013-01-18
    • 1970-01-01
    • 2015-10-01
    • 2015-10-01
    • 1970-01-01
    相关资源
    最近更新 更多