折腾:
【记录】ReactJS的H5利用WebViewJavascriptBridge和iOS的app互相调用
期间,好像碰到的问题是:
iOS的App中,使用了WKWebView去加载页面
ReactJS生成的H5的JS中,使用alert去弹框打印信息
结果始终不显示alert弹框
iOS WKWebView alert
iOS WKWebView not showing javascript alert() dialog – Stack Overflow
需要自己去实现:
alert
confirm
TextIput
(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]; } |
ios – WKWebView – Javascript Confirm and Alert not working – Stack Overflow
iOS WKWebView not showing javascript alert() dialog – Stack Overflow
所以,是需要iOS原生端自己去实现的。
具体写法,参考上述帖子里面代码即可。
比如:
ObjC:
– (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:^{}]; } |
或:
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) } |
转载请注明:在路上 » 【已解决】iOS的APP中WKWebView中alert弹框不显示