在 iOS 中通过 WKWebView 上的 HTML 文件加载 Javascript 文件 [英] Load Javascript files through HTML file on WKWebView in iOS

查看:29
本文介绍了在 iOS 中通过 WKWebView 上的 HTML 文件加载 Javascript 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过 HTML 文件加载 Javascript 文件.我按照以下屏幕截图所示的顺序构建了文件.

I have a requirement of loading Javascript files through HTML file. I have structured files in order shown in below screenshot.

HTML head 标签包含要调用的 Javascript 文件,如下所示:

HTML head tag contains the Javascript files to call as below:

<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<script type="text/javascript" src="AjaxCapturer.js"></script>
<script type="text/javascript" src="authenticate.js"></script>
</head>

所以我一直在使用 UIWebView 来加载页面并正确加载,如下面的屏幕截图所示.

So I have been using UIWebView to load the page and its loading properly as shown in below screenshot.

现在,如果我使用 WKWebView 运行相同的东西,那么页面将继续加载,如下面的屏幕截图所示.

Now if I run same thing using WKWebView then the page is keep on loading as shown in below screenshot.

Javascript 文件正在触发(添加警报),但页面未在 WKWebView 中加载.下面是我的代码逻辑:

Javascript files are firing (added alert) but page does not load in WKWebView. Below is my code logic:

-(void)loadWebVW{

    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    WKPreferences *pref = [[WKPreferences alloc] init];
    pref.javaScriptEnabled = YES;
    pref.javaScriptCanOpenWindowsAutomatically = YES;
    config.preferences = pref;

    CGRect vwFrame = CGRectMake(10, 80, 300, 480);
    WKWebView *webVW = [[WKWebView alloc] initWithFrame:vwFrame configuration:config];
    [self.view addSubview:webVW];
    webVW.navigationDelegate = self;
    webVW.UIDelegate = self;

    NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"offline_Forms"
                                                         ofType:@"html"];
    NSString *html = [NSString stringWithContentsOfFile:htmlPath
                                               encoding:NSUTF8StringEncoding
                                                  error:nil];
    NSURL *baseUrl = [NSURL fileURLWithPath:
                      [NSString stringWithFormat:@"%@/JavaScript",
                       [[NSBundle mainBundle] bundlePath]]];
    [webVW loadHTMLString:html baseURL:baseUrl];

    //or//

    // [webVW loadRequest:[NSMutableURLRequest requestWithURL:[NSURL URLWithString:htmlPath]]];

}

我们是否需要添加任何其他内容以在 WKWebView 中加载它.请帮忙解决这个问题.提前致谢.

Do we need to add any additional stuff to load it in WKWebView. Please help on this. Thanks in advance.

推荐答案

我发现这是 iOS 10.3 中的一个已知问题,稍后在 iOS 10.3.2 中修复,见链接来自 Apple 开发者论坛,了解更多信息.有时我觉得 Simulator 中存在同样的问题,但在物理设备上运行良好.因此,请确保每次在物理设备中验证 WKWebView 功能.

I found that it is a known issue in iOS 10.3 and fixed later in iOS 10.3.2, see link from Apple developer forum for more information. Sometimes I feel that the same issue being existed in Simulator but works fine in physical devices. So make sure every time you verify WKWebView functionality in physical device.

这篇关于在 iOS 中通过 WKWebView 上的 HTML 文件加载 Javascript 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆