WKWebView获取页面title和加载进度值

排版样式可能不是太好 你可以把以下代码全部复制到一个新的UIViewController里面就行

实现功能,获取webview的title和进度值


//  WebCheckViewController.m

//  FamilySchoolPro

//

//  Created by yzq on 15/11/23.

//  Copyright © 2015年 renxiaoxu. All rights reserved.

//

#import "WebCheckViewController.h"

#import

@interface WebCheckViewController ()

@property (nonatomic, strong) WKWebView *webView;

@property(nonatomic,strong)UIProgressView *progressView;

@end

@implementationWebCheckViewController

- (void)viewDidLoad {

    [super viewDidLoad];


    self.title=self.mytitle;


    if ([self.mytitle isEqualToString:@"联系客户经理"]) {

        //        self.webView.dataDetectorTypes = UIDataDetectorTypeNone;

    }

    UIBarButtonItem *_addressBookItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back_bt_icon"] style:UIBarButtonItemStylePlain target:self action:@selector(backBookAction)];


    _addressBookItem.imageInsets=UIEdgeInsetsMake(0,0,0,0);


    [_addressBookItemsetTitle:@"返回"];


    [_addressBookItemsetTintColor:RGB_MD(120,209,97)];


    [self.navigationController.navigationBar setTintColor:RGB_MD(120, 209, 97)];


    UIBarButtonItem *closeBookItem = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(closeBookAction)];


    NSArray*buttonArray = [[NSArrayalloc]initWithObjects:_addressBookItem,closeBookItem,nil];


    self.navigationItem.leftBarButtonItems = buttonArray;


    self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds];

    [_webView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];

    [self.webView setNavigationDelegate:self];

    [self.webView setUIDelegate:self];

    //    [_webView setMultipleTouchEnabled:YES];

    //    [_webView setAutoresizesSubviews:YES];

    [self.webView.scrollView setAlwaysBounceVertical:YES];

    // 这行代码可以是侧滑返回webView的上一级,而不是根控制器(*只针对侧滑有效)

    //    [_webView setAllowsBackForwardNavigationGestures:true];


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

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

    [self.view insertSubview:self.webView belowSubview:self.progressView];


    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.link]]];


}

-(BOOL)hidesBottomBarWhenPushed{


    return YES;

}

//- (WKWebView *)webView{

//    if (_webView == nil) {

//        _webView = [[WKWebView alloc] initWithFrame:self.view.bounds];

//        [_webView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];

////        [_webView setNavigationDelegate:self];

//        [_webView setUIDelegate:self];

//        [_webView setMultipleTouchEnabled:YES];

//        [_webView setAutoresizesSubviews:YES];

//        [_webView.scrollView setAlwaysBounceVertical:YES];

//        // 这行代码可以是侧滑返回webView的上一级,而不是根控制器(*只针对侧滑有效)

//        [_webView setAllowsBackForwardNavigationGestures:true];

//

////        [_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];

//        [self.view insertSubview:_webView belowSubview:self.progressView];

//    }

//    return _webView;

//}

- (UIProgressView*)progressView

{

    if(!_progressView)

    {

        _progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 5)];

        self.progressView.tintColor= [UIColorgreenColor];

        self.progressView.trackTintColor = [UIColor clearColor];

        [self.viewaddSubview:self.progressView];

    }

    return _progressView;

}

-(void)backBookAction{

    if([self.webViewcanGoBack]) {

        [self.webViewgoBack];

    }else{

        [self.navigationController popViewControllerAnimated:YES];

    }

}

-(void)closeBookAction{


    [self.navigationController popViewControllerAnimated:YES];

}

// 记得取消监听

- (void)dealloc {

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

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

}

#pragma mark - WKNavigationDelegate

// 页面开始加载时调用

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


}

// 当内容开始返回时调用

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


}

// 页面加载完成之后调用

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


}

// 页面加载失败时调用

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


    [[LoadingAnimation sharedInstance] hideLoadingAnimation];


    [PromptAction showErrorPrompt:@"网页加载失败..."];


}

// 接收到服务器跳转请求之后调用

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


}

// 在收到响应后,决定是否跳转

- (void)webView:(WKWebView*)webView decidePolicyForNavigationResponse:(WKNavigationResponse*)navigationResponse decisionHandler:(void(^)(WKNavigationResponsePolicy))decisionHandler{


    NSLog(@"%@",navigationResponse.response.URL.absoluteString);

    //允许跳转

    decisionHandler(WKNavigationResponsePolicyAllow);

    //不允许跳转

    //decisionHandler(WKNavigationResponsePolicyCancel);

}

// 在发送请求之前,决定是否跳转

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


    NSLog(@"%@",navigationAction.request.URL.absoluteString);

    /**

     *  拦截app跳转

     */

    NSString*string = navigationAction.request.URL.absoluteString;


    if(string.length>24) {

        string = [stringsubstringToIndex:23];//截取掉下标23之后的字符串

    }

    if ([string isEqualToString:@"http://itunes.apple.com"]) {

        //不允许跳转

        decisionHandler(WKNavigationActionPolicyCancel);

    }

    //允许跳转

    decisionHandler(WKNavigationActionPolicyAllow);

}

// 计算wkWebView进度条

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

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

        CGFloatnewprogress = [[changeobjectForKey:NSKeyValueChangeNewKey]doubleValue];

        if(newprogress ==1) {

            self.progressView.hidden=YES;

            [self.progressViewsetProgress:0animated:NO];

        }else{

            self.progressView.hidden=NO;

            [self.progressViewsetProgress:newprogressanimated:YES];

        }

    }

    //网页title

    elseif([keyPathisEqualToString:@"title"])

    {

        if(object ==self.webView)

        {

            self.title=self.webView.title;

        }

        else

        {

            [superobserveValueForKeyPath:keyPathofObject:objectchange:changecontext:context];

        }

    }

    else

    {

        [superobserveValueForKeyPath:keyPathofObject:objectchange:changecontext:context];

    }

}

-(WKWebView*)webView:(WKWebView*)webView createWebViewWithConfiguration:(WKWebViewConfiguration*)configuration forNavigationAction:(WKNavigationAction*)navigationAction windowFeatures:(WKWindowFeatures*)windowFeatures

{

    if(!navigationAction.targetFrame.isMainFrame) {

        [webViewloadRequest:navigationAction.request];

    }

    return nil;

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

/*

 #pragma mark - Navigation


 // In a storyboard-based application, you will often want to do a little preparation before navigation

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

 // Get the new view controller using [segue destinationViewController].

 // Pass the selected object to the new view controller.

 }

 */

@end

你可能感兴趣的:(WKWebView获取页面title和加载进度值)