美文网首页
WKWebView的简单实用

WKWebView的简单实用

作者: 阳光下的叶子呵 | 来源:发表于2021-01-21 10:57 被阅读0次
    
    #import "MyWebViewViewController.h"
    #import <Webkit/WebKit.h>
    
    
    @interface MyWebViewViewController ()<WKNavigationDelegate, WKScriptMessageHandler>
    
    @property (nonatomic, strong) WKWebView *webView;
    
    @end
    
    @implementation MyWebViewViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        
        self.webView.navigationDelegate = self;
        [self.view addSubview:self.webView];
        
        // 设置偏好设置
    //        WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    //        // 默认为0
    //        config.preferences.minimumFontSize = 10;
    //        //是否支持JavaScript
    //        config.preferences.javaScriptEnabled = YES;
    //        //不通过用户交互,是否可以打开窗口
    //        config.preferences.javaScriptCanOpenWindowsAutomatically = NO;
        
    //    NSURL *baseURL = [[NSBundle mainBundle] bundleURL];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://baidu.com"]];
    //    [request addValue:[self readCurrentCookieWithDomain:@"http://baidu.com"] forHTTPHeaderField:@"Cookie"];
    //    [_webView loadFileURL:request allowingReadAccessToURL:baseURL];
    
        [_webView loadRequest:request]; // 加载webView
        
        /*
        WKUserContentController *userCC = config.userContentController;
        //JS调用OC 添加处理脚本
        [userCC addScriptMessageHandler:self name:@"showMessage"];
        [userCC addScriptMessageHandler:self name:@"clickBack"];
        */
    ///  在添加下拉刷新时:self.webView.scrollView
        __weak typeof(self) weakSelf = self;
        self.webView.scrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakSelf.webView loadRequest:request];
            
            [weakSelf.webView.scrollView.mj_header endRefreshing];
        }];
    }
    
    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        
        /// 添加脚本处理程序 注意遵循WKScriptMessageHandler协议
        [self.webView.configuration.userContentController addScriptMessageHandler:self name:@"showMessage"];
        [self.webView.configuration.userContentController addScriptMessageHandler:self name:@"clickBack"];
    }
    
    - (void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
        
        /// 注意:addScriptMessageHandler很容易引起循环引用,导致控制器无法被释放,所以必须在vc销毁前把它移除
        [self.webView.configuration.userContentController removeScriptMessageHandlerForName:@"showMessage"];
        [self.webView.configuration.userContentController removeScriptMessageHandlerForName:@"clickBack"];
    }
    
    
    
    // 获取js传递的数据
    - (void)userContentController:(nonnull WKUserContentController *)userContentController didReceiveScriptMessage:(nonnull WKScriptMessage *)message {
        /// 两个主要参数 message.name:JS那边的方法名  message.body:JS传过来的参数 为id类型 NSArray,NSDictionary,NSString等等
        
    //    NSLog(@"message.body = %@", message.body);
        
        if ([message.name isEqualToString:@"showMessage"]) {
            
            NSLog(@"message.body = %@", message.body);
            
        } else if ([message.name isEqualToString:@"clickBack"]) {
            
            NSLog(@"message.body = %@", message.body);
        }
         
        
        
    }
    
    //- (void)encodeWithCoder:(nonnull NSCoder *)coder {
    //    <#code#>
    //}
    //
    //- (void)traitCollectionDidChange:(nullable UITraitCollection *)previousTraitCollection {
    //    <#code#>
    //}
    //
    //- (void)preferredContentSizeDidChangeForChildContentContainer:(nonnull id<UIContentContainer>)container {
    //    <#code#>
    //}
    //
    //- (CGSize)sizeForChildContentContainer:(nonnull id<UIContentContainer>)container withParentContainerSize:(CGSize)parentSize {
    //    <#code#>
    //}
    //
    //- (void)systemLayoutFittingSizeDidChangeForChildContentContainer:(nonnull id<UIContentContainer>)container {
    //    <#code#>
    //}
    //
    //- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(nonnull id<UIViewControllerTransitionCoordinator>)coordinator {
    //    <#code#>
    //}
    //
    //- (void)willTransitionToTraitCollection:(nonnull UITraitCollection *)newCollection withTransitionCoordinator:(nonnull id<UIViewControllerTransitionCoordinator>)coordinator {
    //    <#code#>
    //}
    //
    //- (void)didUpdateFocusInContext:(nonnull UIFocusUpdateContext *)context withAnimationCoordinator:(nonnull UIFocusAnimationCoordinator *)coordinator {
    //    <#code#>
    //}
    //
    //- (void)setNeedsFocusUpdate {
    //    <#code#>
    //}
    //
    //- (BOOL)shouldUpdateFocusInContext:(nonnull UIFocusUpdateContext *)context {
    //    <#code#>
    //}
    //
    //- (void)updateFocusIfNeeded {
    //    <#code#>
    //}
    
    // 懒加载
    - (WKWebView *)webView {
        if (!_webView) {
            _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.size.width, self.view.size.height)];
        }
        return _webView;
    }
    
    @end
    
    
    

    相关文章

      网友评论

          本文标题:WKWebView的简单实用

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