OGeek|极客世界-中国程序员成长平台

标题: iOS:JavaScript 无法在 WKWebView 上运行 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:28
标题: iOS:JavaScript 无法在 WKWebView 上运行

我使用 WKWebView 来显示一个网页。当我触摸页面上的按钮时,一些 javascript 代码应该可以工作并向我显示警报 View 。但它不起作用。如果我在 Safari 上打开该页面,它运行良好。这是我的代码

WKWebViewConfiguration *configuration = [WKWebViewConfiguration new];
configuration.preferences.javaScriptCanOpenWindowsAutomatically = YES;
configuration.preferences.javaScriptEnabled = YES;

_webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:configuration];
self.view = _webView;
NSURLRequest *req = [NSURLRequest requestWithURL:[HPBNetworkBusiness getExamPageURL]];
[_webView loadRequest:req];
[_webView addObserver:self forKeyPath"URL" options:NSKeyValueObservingOptionNew context:nil];



Best Answer-推荐答案


WKWebView 不会自动显示 JavaScript 警报。会通过通知 webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:completionHandler: 定义在 WKUIDelegate 上。您可以使用传入的信息通过 UIAlertController 显示 native 警报。

关于iOS:JavaScript 无法在 WKWebView 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35149251/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4