#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 alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, 0)];
progressView.tintColor = WebViewNav_TintColor;
progressView.trackTintColor = [UIColor whiteColor];
[self.view addSubview:progressView];
self.progressView = progressView;
// 网页
if (IOS8x) {
WKWebView *wkWebView = [[WKWebView alloc] initWithFrame: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 alloc] initWithFrame: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 alloc] initWithFrame:CGRectMake(0, 0, 25, 22)];
[backBtn setTintColor:WebViewNav_TintColor];
[backBtn setBackgroundImage:backImage forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(backBtnPressed:)forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *colseItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
self.navigationItem.leftBarButtonItem = colseItem;
}
- (void)configMenuItem {
// 导航栏的菜单按钮
UIImage *menuImage = [UIImage imageNamed:@"cc_webview_menu"];
menuImage = [menuImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIButton *menuBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
[menuBtn setTintColor:WebViewNav_TintColor];
[menuBtn setImage:menuImage forState:UIControlStateNormal];
[menuBtn addTarget:self action:@selector(menuBtnPressed:)forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *menuItem = [[UIBarButtonItem alloc] initWithCustomView:menuBtn];
self.navigationItem.rightBarButtonItem = menuItem;
}
- (void)configColseItem {
// 导航栏的关闭按钮
UIButton *colseBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 44, 44)];
[colseBtn setTitle:@"关闭" forState:UIControlStateNormal];
[colseBtn setTitleColor:WebViewNav_TintColor forState:UIControlStateNormal];
[colseBtn addTarget:self action:@selector(colseBtnPressed:)forControlEvents:UIControlEventTouchUpInside];
[colseBtn sizeToFit];
UIBarButtonItem *colseItem = [[UIBarButtonItem alloc] initWithCustomView: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 generalPasteboard] setString: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 sharedApplication] openURL: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:NSKeyValueChangeNewKey] doubleValue];
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