iOS 封装WKWebView页面带加载进度条

来源:互联网 发布:python能做成包吗 编辑:程序博客网 时间:2024/06/07 10:11
DEMO下载地址:https://github.com/dangxiaoyin/WKWebView

#import < UIKit/UIKit.h>


@interface CCWebViewController : UIViewController


@property (strong, nonatomic) NSURL *homeUrl;


 

+ (void)showWithContro:(UIViewController *)contro withUrlStr:(NSString *)urlStr withTitle:(NSString *)title;


@end



/**---------------------- .m 文件 --------------------------*/
/**---------------------- .m 文件 --------------------------*/


#import < WebKit/WebKit.h>

#import "CCWebViewController.h"


#define IOS8x ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0)

#define WebViewNav_TintColor ([UIColor orangeColor])

@interface CCWebViewController ()<</span>UIWebViewDelegate,UIActionSheetDelegate,WKNavigationDelegate>

@property (assign, nonatomic) NSUInteger loadCount;

@property (strong, nonatomic) UIProgressView *progressView;

@property (strong, nonatomic) UIWebView *webView;

@property (strong, nonatomic) WKWebView *wkWebView;

@end


@implementation CCWebViewController


 

+ (void)showWithContro:(UIViewController *)contro withUrlStr:(NSString *)urlStr withTitle:(NSString *)title {

    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    CCWebViewController *webContro = [CCWebViewController new];

    webContro.homeUrl = [NSURL URLWithString:urlStr];

    webContro.title = title;

    [contro.navigationController pushViewController:webContro animated:YES];

}


- (void)viewDidLoad {

    [super viewDidLoad];

    self.edgesForExtendedLayout = UIRectEdgeNone;

    self.view.backgroundColor = [UIColor whiteColor];

    [self configUI];

    [self configBackItem];

    [self configMenuItem];

}


- (void)configUI {

    

    // 进度条

    UIProgressView *progressView = [[UIProgressView allocinitWithFrame:CGRectMake(00,self.view.frame.size.width0)];

    progressView.tintColor = WebViewNav_TintColor;

    progressView.trackTintColor = [UIColor whiteColor];

    [self.view addSubview:progressView];

    self.progressView = progressView;

    

    // 网页

    if (IOS8x) {

        WKWebView *wkWebView = [[WKWebView allocinitWithFrame:self.view.bounds];

        wkWebView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;

        wkWebView.backgroundColor = [UIColor whiteColor];

        wkWebView.navigationDelegate = self;

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

        

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

        NSURLRequest *request = [NSURLRequest requestWithURL:_homeUrl];

        [wkWebView loadRequest:request];

        self.wkWebView = wkWebView;

    }else {

        UIWebView *webView = [[UIWebView allocinitWithFrame:self.view.bounds];

        webView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;

        webView.scalesPageToFit = YES;

        webView.backgroundColor = [UIColor whiteColor];

        webView.delegate = self;

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

        

        NSURLRequest *request = [NSURLRequest requestWithURL:_homeUrl];

        [webView loadRequest:request];

        self.webView = webView;

    }

}


- (void)configBackItem {

    

    // 导航栏的返回按钮

    UIImage *backImage = [UIImage imageNamed:@"cc_webview_back"];

    backImage = [backImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

    UIButton *backBtn = [[UIButton allocinitWithFrame:CGRectMake(002522)];

    [backBtn setTintColor:WebViewNav_TintColor];

    [backBtn setBackgroundImage:backImage forState:UIControlStateNormal];

    [backBtn addTarget:self action:@selector(backBtnPressed:)forControlEvents:UIControlEventTouchUpInside];

    

    UIBarButtonItem *colseItem = [[UIBarButtonItem allocinitWithCustomView:backBtn];

    self.navigationItem.leftBarButtonItem = colseItem;

}


- (void)configMenuItem {

    

    // 导航栏的菜单按钮

    UIImage *menuImage = [UIImage imageNamed:@"cc_webview_menu"];

    menuImage = [menuImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

    UIButton *menuBtn = [[UIButton allocinitWithFrame:CGRectMake(00520)];

    [menuBtn setTintColor:WebViewNav_TintColor];

    [menuBtn setImage:menuImage forState:UIControlStateNormal];

    [menuBtn addTarget:self action:@selector(menuBtnPressed:)forControlEvents:UIControlEventTouchUpInside];

    

    UIBarButtonItem *menuItem = [[UIBarButtonItem allocinitWithCustomView:menuBtn];

    self.navigationItem.rightBarButtonItem = menuItem;

}


- (void)configColseItem {

    

    // 导航栏的关闭按钮

    UIButton *colseBtn = [[UIButton alloc]initWithFrame:CGRectMake(004444)];

    [colseBtn setTitle:@"关闭" forState:UIControlStateNormal];

    [colseBtn setTitleColor:WebViewNav_TintColor forState:UIControlStateNormal];

    [colseBtn addTarget:self action:@selector(colseBtnPressed:)forControlEvents:UIControlEventTouchUpInside];

    [colseBtn sizeToFit];

    

    UIBarButtonItem *colseItem = [[UIBarButtonItem allocinitWithCustomView:colseBtn];

    NSMutableArray *newArr = [NSMutableArrayarrayWithObjects:self.navigationItem.leftBarButtonItem,colseItem, nil];

    self.navigationItem.leftBarButtonItems = newArr;

}


#pragma mark - 普通按钮事件


// 返回按钮点击

- (void)backBtnPressed:(id)sender {

    if (IOS8x) {

        if (self.wkWebView.canGoBack) {

            [self.wkWebView goBack];

            if (self.navigationItem.leftBarButtonItems.count == 1) {

                [self configColseItem];

            }

        }else {

            [self.navigationController popViewControllerAnimated:YES];

        }

    }else {

        if (self.webView.canGoBack) {

            [self.webView goBack];

            if (self.navigationItem.leftBarButtonItems.count == 1) {

                [self configColseItem];

            }

        }else {

            [self.navigationController popViewControllerAnimated:YES];

        }

    }

}


// 菜单按钮点击

- (void)menuBtnPressed:(id)sender {

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:selfcancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"safari打开",@"复制链接",@"分享",@"刷新", nil];

    [actionSheet showInView:self.view];

}


// 关闭按钮点击

- (void)colseBtnPressed:(id)sender {

    [self.navigationController popViewControllerAnimated:YES];

}


#pragma mark - 菜单按钮事件


- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {

    

    NSString *urlStr = _homeUrl.absoluteString;

    if (IOS8x) urlStr = self.wkWebView.URL.absoluteString;

    else urlStr = self.webView.request.URL.absoluteString;

    if (buttonIndex == 0) {

        

        // safari打开

        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];

    }else if (buttonIndex == 1) {

        

        // 复制链接

        if (urlStr.length > 0) {

            [[UIPasteboard generalPasteboardsetString:urlStr];

            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"已复制链接到黏贴板"message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"知道了", nil];

            [alertView show];

        }

    }else if (buttonIndex == 2) {

        

        // 分享

        //[self.wkWebView evaluateJavaScript:@"这里写js代码" completionHandler:^(id reponse, NSError * error) {

            //NSLog(@"返回的结果%@",reponse);

        //}];

        NSLog(@"这里自己写,分享url%@",urlStr);

    }else if (buttonIndex == 3) {

        

        // 刷新

        if (IOS8x) [self.wkWebView reload];

        else [self.webView reload];

        

    }

}


#pragma mark - wkWebView代理


// 如果不添加这个,那么wkwebview跳转不了AppStore

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

    if ([webView.URL.absoluteString hasPrefix:@"https://itunes.apple.com"]) {

        [[UIApplication sharedApplicationopenURL:navigationAction.request.URL];

        decisionHandler(WKNavigationActionPolicyCancel);

    }else {

        decisionHandler(WKNavigationActionPolicyAllow);

    }

}


// 计算wkWebView进度条

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

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

        CGFloat newprogress = [[change objectForKey:NSKeyValueChangeNewKeydoubleValue];

        if (newprogress == 1) {

            self.progressView.hidden = YES;

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

        }else {

            self.progressView.hidden = NO;

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

        }

    }

}


// 记得取消监听

- (void)dealloc {

    if (IOS8x) {

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

    }

}


#pragma mark - webView代理


// 计算webView进度条

- (void)setLoadCount:(NSUInteger)loadCount {

    _loadCount = loadCount;

    if (loadCount == 0) {

        self.progressView.hidden = YES;

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

    }else {

        self.progressView.hidden = NO;

        CGFloat oldP = self.progressView.progress;

        CGFloat newP = (1.0 - oldP) / (loadCount + 1) + oldP;

        if (newP > 0.95) {

            newP = 0.95;

        }

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

    }

}


- (void)webViewDidStartLoad:(UIWebView *)webView {

    self.loadCount ++;

}


- (void)webViewDidFinishLoad:(UIWebView *)webView {

    self.loadCount --;

}


- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {

    self.loadCount --;

}


@end

0 0
原创粉丝点击