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

Posted 刚刚下课

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WKWebView获取页面title和加载进度值相关的知识,希望对你有一定的参考价值。

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

实现功能,获取的WebView的标题和进度值


// WebCheckViewController.m

// FamilySchoolPro

//

// 15/11/23由yzq创建。

//版权所有©2015年renxiaoxu。版权所有。

//

#import“WebCheckViewController.h”

#进口

@interface WebCheckViewController()

@属性(非原子,强大)WKWebView * webView;

@property(非原子,强大)UIProgressView * progressView;

@结束

@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


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

//

//返回_webView;

//

- (UIProgressView *)progressView

    如果(!_ 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;

- (无效)backBookAction

    if([self.webViewcanGoBack])

        [self.webViewgoBack];

    其他

        [self.navigationController popViewControllerAnimated:YES];

   

- (无效)closeBookAction


    [self.navigationController popViewControllerAnimated:YES];

//记得取消监听

- (void)dealloc

    [self.webView removeObserver:self forKeyPath:@“estimatedProgress”];

    [self.webView removeObserver:self forKeyPath:@“title”];

#pragma标记 - 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)对象更改:(NSDictionary *)更改上下文:(void *)context

    if(object == self.webView && [keyPathisEqualToString:@“estimatedProgress”])

        CGFloatnewprogress = [[changeobjectForKey:NSKeyValueChangeNewKey] doubleValue];

        if(newprogress == 1)

            self.progressView.hidden = YES;

            [self.progressViewsetProgress:0animated:NO];

        其他

            self.progressView.hidden = NO;

            [self.progressViewsetProgress:newprogressanimated:YES];

       

   

    //网页标题

    ELSEIF([keyPathisEqualToString:@ “标题”])

   

        如果(object == self.webView)

       

            self.title = self.webView.title;

       

        其他

       

            [superobserveValueForKeyPath:keyPathofObject:objectchange:changecontext:上下文];

       

   

    其他

   

        [superobserveValueForKeyPath:keyPathofObject:objectchange:changecontext:上下文];

   

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

    if(!navigationAction.targetFrame.isMainFrame)

        [webViewloadRequest:navigationAction.request];

   

    return nil;

- (void)didReceiveMemoryWarning

    [super didReceiveMemoryWarning];

    //处理任何可以重新创建的资源。

/ *

 #pragma mark - 导航


 //在基于故事板的应用程序中,您通常会在导航前做一些准备工作

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

 //使用[segue destinationViewController]获取新的视图控制器。

 //将选定的对象传递给新的视图控制器。

 

 * /

@结束

以上是关于WKWebView获取页面title和加载进度值的主要内容,如果未能解决你的问题,请参考以下文章

iOS WKWebView添加网页加载进度条(转)

SwiftUI如何监听WKWebView加载网页的进度

使用WKWebView自适应屏幕遇到的问题以及最后解决的方法

iOS WKWebView 加载进度条导航栏返回&关闭 (Swift 4)

如何在 SwiftUI 中观察 WKWebView 的加载进度?

swift 笔记:iOS与JavaScript的交互(三):WKWebView 2.显示加载进度