在iOS的WKWebView上通过HTML文件加载Javascript文件
问题描述:
我有通过HTML
文件加载Javascript文件的需求。我按照下面的屏幕截图所示的顺序有结构化文件。在iOS的WKWebView上通过HTML文件加载Javascript文件
HTML
head标签包含Javascript
文件如下呼吁:
<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
正确加载网页和它的加载如下图所示的截图。
现在如果我运行使用WKWebView
那么一回事的页面继续加载如下图所示的截图。
Javascript
文件被烧成(添加警报),但页面不WKWebView
加载。下面是我的代码逻辑:
-(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
。请帮助。提前致谢。
答
我发现它是在iOS
10.3的已知问题,后来在iOS
10.3.2固定,更多信息请参见链接 from Apple developer forum。有时我觉得Simulator
存在同样的问题,但在物理设备上工作正常。因此,请确保每次在物理设备上验证WKWebView
功能。
@randombits,你能帮到这个... –
你有这个@Ganesh Guturi的解决方案吗?因为我也面临同样的问题,如果你已经修好了,你可以发布答案吗?谢谢 –
请在下面看到我的回答@AnilkumariOSdeveloper –