//
// IntegralMallViewController.m
// M
//
// Created by zsl on 2018/4/19.
// Copyright © 2018年 zsl. All rights reserved.
//
#import "IntegralMallViewController.h"
#import
@interfaceIntegralMallViewController ()
/**新版WebView*/
@property(nonatomic,weak) WKWebView *webView;
/**进度条*/
@property(nonatomic,weak) UIProgressView *progressView;
/**返回按钮*/
@property(nonatomic,strong) UIBarButtonItem *backItem;
/**关闭按钮*/
@property(nonatomic,strong) UIBarButtonItem *closeItem;
@end
@interfaceNSURLRequest (InvalidSSLCertificate)//https证书
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host;
+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString *)host;
@end
@implementationIntegralMallViewController
#pragma mark - 添加关闭按钮
- (void)addLeftButton {
self.navigationItem.leftBarButtonItem =self.backItem;
}
#pragma mark - init
/**
* 返回按钮
*/
- (UIBarButtonItem *)backItem {
if(!_backItem) {
_backItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageWithOriginalName:@"root_cancel"]style:UIBarButtonItemStylePlain target:selfaction:@selector(backNative)];
self.navigationItem.leftBarButtonItem = _backItem;
}
return_backItem;
}
/**
* 点击返回的方法
*/
- (void)backNative {
//判断是否有上一层H5页面
if([self.webViewcanGoBack]) {
//如果有则返回
[self.webViewgoBack];
//同时设置返回按钮和关闭按钮为导航栏左边的按钮
self.navigationItem.leftBarButtonItems = @[self.backItem, self.closeItem];
}else{
[selfcloseNative];
}
}
/**
* 关闭按钮
*/
- (UIBarButtonItem*)closeItem {
if (!_closeItem) {
_closeItem = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(closeNative)];
}
return _closeItem;
}
/**
* 关闭H5页面,直接回到原生页面
*/
- (void)closeNative {
[self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
/**1、添加返回按钮*/
[self addLeftButton];
/**2、初始化webView*/
[self setupWebView];
/**3、初始化进度条*/
[self setupProgressView];
}
#pragma mark - 初始化
- (void)setupWebView {
// 创建WKWebView
WKWebView *webView = [[WKWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
_webView= webView;
self.webView.navigationDelegate = self;
self.webView.allowsBackForwardNavigationGestures = YES;
// 设置访问的URL
NSURL *url = [NSURL URLWithString:@"https://www.jianshu.com/writer#/notebooks/22697260/notes/26857069"];
// 根据URL创建请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
// WKWebView加载请求
[_webView loadRequest:request];
// 将WKWebView添加到视图
[self.viewaddSubview:_webView];
[self.webView addObserver:self forKeyPath:@"estimatedProgress" options:0 context:nil];
/**添加title观察者*/
[self.webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];
}
/**
* 初始化进度条
*/
- (void)setupProgressView {
UIProgressView *progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
_progressView= progressView;
self.progressView.frame=CGRectMake(0,1,kScreenWidth,5);
[self.progressView setTrackTintColor:[UIColor colorWithRed:240.0/255
green:240.0/255
blue:240.0/255
alpha:1.0]];
self.progressView.progressTintColor= ZTCOLOR;
[self.view addSubview:self.progressView];
}
#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView*)webView didStartProvisionalNavigation:(WKNavigation*)navigation {
//开始加载的时候,让进度条显示
self.progressView.hidden = NO;
}
- (void)webView:(WKWebView*)webView didCommitNavigation:(WKNavigation*)navigation {
}
- (void)webView:(WKWebView*)webView didFinishNavigation:(WKNavigation*)navigation {
}
#pragma mark - KVO
//kvo 监听进度
-(void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void*)context {
if([keyPathisEqualToString:@"estimatedProgress"] ) {
[self.progressViewsetAlpha:1.0f];
BOOLanimated =self.webView.estimatedProgress>self.progressView.progress;
[self.progressViewsetProgress:self.webView.estimatedProgressanimated:animated];
if(self.webView.estimatedProgress>=1.0f) {
[UIView animateWithDuration:0.3f delay:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{
[self.progressViewsetAlpha:0.0f];
}completion:^(BOOLfinished) {
[self.progressViewsetProgress:0.0fanimated:NO];
}];
}
} elseif([keyPathisEqualToString:@"title"]) {//网页title
if(object ==self.webView) {
self.title=self.webView.title;
} else{
[superobserveValueForKeyPath:keyPathofObject:objectchange:changecontext:context];
}
}else{
[superobserveValueForKeyPath:keyPathofObject:objectchange:changecontext:context];
}
}
- (void)dealloc {
[self.webView removeObserver:self forKeyPath:@"title"];
[self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
self.webView.navigationDelegate = nil;
[self.webView setUIDelegate:nil];
}
@end