我在iOS 8中使用WKWebView显示从Javascript调用的警告对话框时遇到了一些麻烦。在创建标准WKWebView并加载HTML文件后,我在页面上有一个按钮,用于创建带有一些文本的简单警报。这适用于UIWebView和Google Chrome / Safari,但似乎无法在WKWebView中使用。任何帮助表示赞赏。
我的设置如下:
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
config.allowsInlineMediaPlayback = YES;
config.mediaPlaybackRequiresUserAction = false;
_wkViewWeb = [[WKWebView alloc] initWithFrame:_viewWeb.frame config];
_wkViewWeb.scrollView.scrollEnabled = NO;
NSString *fullURL = @"file://.../TestSlide.html";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
[_wkViewWeb loadRequest:request];
html具有以下功能:
<SCRIPT Language="JavaScript">
function alertTest() {
alert("Testing Alerts");
}
</SCRIPT>
还有一个按钮:
<b>Test Alerts: <input type="button" value="Alert Popup" onclick="alertTest()"><br></b> <br>
此设置适用于UIWebView和常规浏览器,但在WKWebView中不起作用。我在配置中遗漏了什么吗?我是否应该使用其中一个WK代表来控制警报/确认对话框行为?谢谢。
答案 0 :(得分:60)
要解决此问题,您需要一个WKUIDelegate用于您的Web视图。代表有责任决定是否应该以何种方式显示警报。您需要实现此警报,确认和文本输入(提示)。
以下是未经页面网址或安全功能验证的示例代码:
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:message
message:nil
preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) {
completionHandler();
}]];
[self presentViewController:alertController animated:YES completion:^{}];
}
中的更多内容
答案 1 :(得分:39)
Swift 3 ,实现了所有3个可选功能:
func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo,
completionHandler: @escaping () -> Void) {
let alertController = UIAlertController(title: nil, message: message, preferredStyle: .actionSheet)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
completionHandler()
}))
present(alertController, animated: true, completion: nil)
}
func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo,
completionHandler: @escaping (Bool) -> Void) {
let alertController = UIAlertController(title: nil, message: message, preferredStyle: .actionSheet)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
completionHandler(true)
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (action) in
completionHandler(false)
}))
present(alertController, animated: true, completion: nil)
}
func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo,
completionHandler: @escaping (String?) -> Void) {
let alertController = UIAlertController(title: nil, message: prompt, preferredStyle: .actionSheet)
alertController.addTextField { (textField) in
textField.text = defaultText
}
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
if let text = alertController.textFields?.first?.text {
completionHandler(text)
} else {
completionHandler(defaultText)
}
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (action) in
completionHandler(nil)
}))
present(alertController, animated: true, completion: nil)
}
答案 2 :(得分:12)
只需展开一点,WKWebView
要求您自己显示提醒,提示和确认。通过成为WKUIDelegate
:
#import <WebKit/WebKit.h>
@interface MyController : UIViewController<WKUIDelegate>
然后分配代表:
web.UIDelegate = self;
然后你需要实际实现警报,提示和确认。我创建WKWebViewPanelManager.h/m作为一个简单的实现,所以这就是我的工作:
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler {
[WKWebViewPanelManager presentAlertOnController:self.view.window.rootViewController title:@"Alert" message:message handler:completionHandler];
}
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler {
[WKWebViewPanelManager presentConfirmOnController:self.view.window.rootViewController title:@"Confirm" message:message handler:completionHandler];
}
- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * __nullable result))completionHandler {
[WKWebViewPanelManager presentPromptOnController:self.view.window.rootViewController title:@"Prompt" message:prompt defaultText:defaultText handler:completionHandler];
}
当然,您可以过滤掉不良警报/确认/提示请求。
答案 3 :(得分:11)
这就是swift
func webView(webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String,
initiatedByFrame frame: WKFrameInfo, completionHandler: () -> Void) {
let alertController = UIAlertController(title: message, message: nil,
preferredStyle: UIAlertControllerStyle.Alert);
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel) {
_ in completionHandler()}
);
self.presentViewController(alertController, animated: true, completion: {});
}
答案 4 :(得分:2)
这是 Swift 4.2
中的代码func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String,
initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
let alertController = UIAlertController(title: message, message: nil,
preferredStyle: UIAlertController.Style.alert);
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.cancel) {
_ in completionHandler()}
);
self.present(alertController, animated: true, completion: {});
}
答案 5 :(得分:2)
向 WKWebview 添加首选项,以在 viewDidLoad()中启用javascript。
// enable JS
webView.configuration.preferences.javaScriptEnabled = true
在 viewDidLoad()中将 WKWebview 用户界面代理注册为
self.webView.uiDelegate = self
在您的班级中实现以下委托。
func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message:
String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () ->
Void) {
let alertController = UIAlertController(title: message,message: nil,preferredStyle:
.alert)
alertController.addAction(UIAlertAction(title: "OK", style: .cancel) {_ in
completionHandler()})
self.present(alertController, animated: true, completion: nil)
}