ios wkwebview弹框_iOS WKWebView的javascript alert 不弹的解决方案

- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler{

UIAlertController*alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message?:@""preferredStyle:UIAlertControllerStyleAlert];

[alertController addAction:([UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {

completionHandler();

}])];

[self presentViewController:alertController animated:YES completion:nil];

}- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler{//DLOG(@"msg = %@ frmae = %@",message,frame);

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message?:@""preferredStyle:UIAlertControllerStyleAlert];

[alertController addAction:([UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action) {

completionHandler(NO);

}])];

[alertController addAction:([UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {

completionHandler(YES);

}])];

[self presentViewController:alertController animated:YES completion:nil];

}- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString *_Nullable))completionHandler{

UIAlertController*alertController = [UIAlertController alertControllerWithTitle:prompt message:@""preferredStyle:UIAlertControllerStyleAlert];

[alertController addTextFieldWithConfigurationHandler:^(UITextField *_Nonnull textField) {

textField.text=defaultText;

}];

[alertController addAction:([UIAlertAction actionWithTitle:@"完成" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {

completionHandler(alertController.textFields[0].text?:@"");

}])];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android和iOS平台上,可以通过WebView的接口来进行JavaScript和原生代码之间的数据交互。以下是Android和iOS平台上的一些常用方法: ### Android平台 #### 1. WebView.addJavascriptInterface() 该方法可以将Java对象注册到WebView中,使得JavaScript可以通过该对象调用Java中的方法。具体使用方法如下: ```java // 创建一个Java对象 public class MyJavaObject { @JavascriptInterface public void showToast(String message) { Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show(); } } // 将Java对象注册到WebViewwebView.addJavascriptInterface(new MyJavaObject(), "myJavaObject"); ``` 在JavaScript中,可以通过`window.myJavaObject`来调用MyJavaObject对象中的`showToast()`方法,例如: ```javascript window.myJavaObject.showToast('Hello World!'); ``` #### 2. WebView.evaluateJavascript() 该方法可以在WebView中执行JavaScript代码,并将执行结果传递给指定的回调函数。具体使用方法如下: ```java webView.evaluateJavascript("javascript:alert('Hello World!')", new ValueCallback<String>() { @Override public void onReceiveValue(String value) { // value为JavaScript代码的执行结果 } }); ``` 在上面的例子中,我们通过WebView执行了一段JavaScript代码,即弹出一个提示框,然后将执行结果传递给了回调函数。 ### iOS平台 #### 1. WKWebView.evaluateJavaScript() 该方法可以在WKWebView中执行JavaScript代码,并将执行结果传递给指定的回调函数。具体使用方法如下: ```objective-c [webView evaluateJavaScript:@"alert('Hello World!')" completionHandler:^(id result, NSError *error) { // result为JavaScript代码的执行结果 }]; ``` 在上面的例子中,我们通过WKWebView执行了一段JavaScript代码,即弹出一个提示框,然后将执行结果传递给了回调函数。 #### 2. WKScriptMessageHandler 该协议定义了一个方法,用于处理从JavaScript发送给原生代码的消息。具体使用方法如下: 首先,我们需要创建一个遵循WKScriptMessageHandler协议的对象,并将其注册到WKWebView中: ```objective-c // 创建一个WKUserContentController对象 WKUserContentController *userContentController = [[WKUserContentController alloc] init]; // 创建一个遵循WKScriptMessageHandler协议的对象 MyMessageHandler *messageHandler = [[MyMessageHandler alloc] init]; // 将messageHandler对象注册到userContentController中 [userContentController addScriptMessageHandler:messageHandler name:@"myMessageHandler"]; // 创建一个WKWebViewConfiguration对象,并将userContentController设置为它的userContentController属性 WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init]; configuration.userContentController = userContentController; // 创建一个WKWebView对象,并将configuration设置为它的configuration属性 WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:configuration]; [self.view addSubview:webView]; ``` 然后,在MyMessageHandler对象中实现`userContentController(_:didReceive:)`方法,用于处理从JavaScript发送过来的消息: ```objective-c @interface MyMessageHandler : NSObject <WKScriptMessageHandler> @end @implementation MyMessageHandler - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message { // 在这里处理从JavaScript发送过来的消息 NSLog(@"Received message from JavaScript: %@", message.body); } @end ``` 在JavaScript中,可以通过`window.webkit.messageHandlers`对象来发送消息给原生代码,例如: ```javascript window.webkit.messageHandlers.myMessageHandler.postMessage('Hello World!'); ``` 在上面的例子中,我们向名为myMessageHandler的MessageHandler对象发送了一条消息,即字符串'Hello World!'。当原生代码接收到该消息时,会执行MyMessageHandler对象中的`userContentController(_:didReceive:)`方法,并输出接收到的消息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值