WKWebView

#import <WebKit/WebKit.h>

self.userContentController = [[WKUserContentController alloc] init];

    self.webViewConfig = [[WKWebViewConfiguration alloc] init];

    self.webViewConfig.userContentController = self.userContentController;

    self.webViewConfig.allowsInlineMediaPlayback = YES;

    if (@available(iOS 10.0, *)) {

        self.webViewConfig.mediaTypesRequiringUserActionForPlayback = NO; // 允许点击后播放video,

    }

    self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) configuration:self.webViewConfig];

    [self.view addSubview:self.webView];

    UIEdgeInsets padding = UIEdgeInsetsMake(0, 0, 0, 0);

    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.edges.equalTo(self.view).with.insets(padding);

    }];

    

    self.webView.UIDelegate = self;

    self.webView.navigationDelegate = self;

    self.webView.allowsBackForwardNavigationGestures = YES;

    self.webView.allowsLinkPreview = YES;

    [self.webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];

    [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];

#pragma mark - event response

// 计算wkWebView进度条

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context

{

    if (object == self.webView && [keyPath isEqualToString:@"estimatedProgress"]) {

        CGFloat newprogress = [[change objectForKey:NSKeyValueChangeNewKey] doubleValue];

        self.progressView.hidden = NO;

        [self.progressView setProgress:newprogress animated:YES];

        if (newprogress >= 1.0f) {

            self.progressView.hidden = YES;

            [self.progressView setProgress:0 animated:YES];

        }

    }else if (object == self.webView && [keyPath isEqualToString:@"title"]) {

        self.title = self.webView.title;

    }

    else {

        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];

    }

}

- (void)dealloc

{

    DebugLog(@"webview销毁");

    [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];

    [self.webView removeObserver:self forKeyPath:@"title" context:nil];

}

#pragma mark --WebViewDelegate

- (void)userContentController:(WKUserContentController *)userContentController

      didReceiveScriptMessage:(WKScriptMessage *)message {

    //message.body传值的内容。

    //前端需要用 window.webkit.messageHandlers.注册的方法名.postMessage({body:传输的数据} 来给native发送消息

    //    window.webkit.messageHandlers.refreshApp.postMessage({body: 'hello world!'});

    if ([message.name isEqualToString:h5_close]) {

        

        [self.navigationController popViewControllerAnimated:YES];

    }else if ([message.name isEqualToString:h5_sendAdUrl]) {

        WebBViewController *webVC = [[WebBViewController alloc] init];

        webVC.remoteUrl = message.body;

        [self.navigationController pushViewController:webVC animated:YES];

    }else if ([message.name isEqualToString:h5_triplePoints])

    {

        self.points = [NSString stringWithFormat:@"%@",message.body];

        [self loadRewardVideoAdWithSlotID:ADREWARD_ID];

        [SVProgressHUD show];

    }

}

// 网页开始加载

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {

    NSLog(@"123");

}

// 网页完成加载

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {

    NSLog(@"456");

    [self.webView.scrollView.mj_header endRefreshing];

    if (__startTimer) {

        __startTimer = NO;

        [self.timer invalidate];

        self.timer = nil;

        __currentTime = 0;

        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateCurrentPlayedTime) userInfo:nil repeats:YES];

        [self.timer fire];

    }

}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler

{

    NSLog(@"请求的地址是:%@",navigationAction.request.URL.absoluteString);

    if ([navigationAction.request.URL.absoluteString rangeOfString:@"yidianzixun"].location != NSNotFound) {

        __startTimer = YES;

    }

    decisionHandler(WKNavigationActionPolicyAllow);

}

// 网页加载出错

- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {

    [self.webView.scrollView.mj_header endRefreshing];

}

//作为js中confirm接口的实现,需要有提示信息以及两个相应事件, 确认及取消,并且在completionHandler中回传相应结果,确认返回YES, 取消返回NO

//参数 message为  js 方法 confirm(<message>) 中的<message>

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

    

    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];

}

//可以显示网页中的alert中的信息

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

    

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

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

        completionHandler();

    }]];

    [self presentViewController:alert animated:YES completion:NULL];

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值