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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 161,780评论 4 369
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,424评论 1 305
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 111,397评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,576评论 0 218
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,997评论 3 295
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,945评论 1 224
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,107评论 2 317
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,850评论 0 208
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,625评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,804评论 2 253
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,285评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,613评论 3 261
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,291评论 3 242
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,164评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,963评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,096评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,886评论 2 278

推荐阅读更多精彩内容

  • 关于WKWebview与UIWebview的简单对比,可以参考我去年写的一篇文章WKWbeview的初步使用。这次...
    ZYiDa阅读 19,408评论 2 10
  • 前言 关于UIWebView的介绍,相信看过上文的小伙伴们,已经大概清楚了吧,如果有问题,欢迎提问。 本文是本系列...
    CoderLF阅读 8,861评论 2 12
  • WKWebView基类封装 项目一直使用UIWebView,相关基类方法封装使用比较完善,业务量重,想整体替换为W...
    IntMan阅读 649评论 0 0
  • 千头万绪,一时不知该从何说起。 那么,就先从女生工作这件事开始吧。 我没有怎么见过不工作的女生,即所谓的全职太太,...
    晴朗的小乐乐阅读 710评论 1 4
  • 十五章回顾 梁西席在陈柯家和自己家来回跑,半个月不到,黑眼圈都要长到脸上去了。每天一副睡眠不足的状态,上课的时候...
    巫其格阅读 7,420评论 52 89