美文网首页
WKWebView_1的VC.m

WKWebView_1的VC.m

作者: seventhboy | 来源:发表于2017-11-22 18:44 被阅读10次

    import "WKWebViewViewController.h"

    import <WebKit/WebKit.h>

    @interface WKWebViewViewController ()
    <
    WKScriptMessageHandler,
    WKUIDelegate

    @property (strong, nonatomic) WKWebView *wkWebView;

    @end

    @implementation WKWebViewViewController

    • (void)viewDidLoad {
      [super viewDidLoad];
      // Do any additional setup after loading the view.
    [self initWKWebView];
    

    }

    pragma mark

    pragma mark - Configuration

    • (void)initWKWebView {

      WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
      configuration.userContentController = [WKUserContentController new];

      self.wkWebView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:configuration];
      //绑定html的btn点击事件 去掉click
      [configuration.userContentController addScriptMessageHandler:self name:@"ShowMessageFromWKWebView"];

      NSString *webViewURLStr = [[NSBundle mainBundle] pathForResource:@"index.html" ofType:nil];
      NSURL *fileURL = [NSURL fileURLWithPath:webViewURLStr];
      [self.wkWebView loadFileURL:fileURL allowingReadAccessToURL:fileURL];
      self.wkWebView.UIDelegate = self;
      [self.view addSubview:self.wkWebView];
      }

    pragma mark

    pragma mark - WKScriptMessageHandler

    • (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {

      NSLog(@"body:%@", message.body);

      if ([message.name isEqualToString:@"ShowMessageFromWKWebView"]) {
      [self showMessageWithParams:message.body];
      }
      }

    pragma mark

    pragma mark - WKUIDelegate

    /*
    *响应JS里的alert提醒
    /
    /

    function asyncAlert(alertStr) {
    setTimeout(function() {
    alert(alertStr);
    }, 1);
    }
    */

    • (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:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
      completionHandler();
      }]];

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

    pragma mark

    pragma mark - Show Message

    • (void)showMessageWithParams:(NSDictionary *)dict {

      if (![dict isKindOfClass:[NSDictionary class]]) {
      return;
      }

      NSString *messageStr = [dict objectForKey:@"message"];
      NSString *titleStr = [dict objectForKey:@"title"];
      NSLog(@"title:%@", titleStr);
      NSLog(@"messageStr:%@", messageStr);

      // do it

      // 将结果返回给js
      NSString *returnJSStr = [NSString stringWithFormat:@"showMessageFromWKWebViewResult('%@')", @"message传到OC成功,message传到OC成功,message传到OC成功,message传到OC成功,message传到OC成功,message传到OC成功"];
      [self.wkWebView evaluateJavaScript:returnJSStr completionHandler:^(id _Nullable result, NSError * _Nullable error) {

        NSLog(@"%@,%@", result, error);
      

      }];
      }

    @end

    相关文章

      网友评论

          本文标题:WKWebView_1的VC.m

          本文链接:https://www.haomeiwen.com/subject/dfuovxtx.html