143 lines
4.2 KiB
Mathematica
143 lines
4.2 KiB
Mathematica
|
|
//
|
||
|
|
// PYWebController.m
|
||
|
|
// youzhu
|
||
|
|
//
|
||
|
|
// Created by 谢培艺 on 16/1/27.
|
||
|
|
// Copyright © 2016年 iphone5solo. All rights reserved.
|
||
|
|
//
|
||
|
|
|
||
|
|
#import "PYWebController.h"
|
||
|
|
#import "YJWebProgressLayer.h"
|
||
|
|
|
||
|
|
#import "UIBarButtonItem+PYExtension.h"
|
||
|
|
#import "PYHUD.h"
|
||
|
|
#import <WebKit/WebKit.h>
|
||
|
|
#import "MTShareService.h"
|
||
|
|
|
||
|
|
@interface PYWebController () <WKNavigationDelegate>
|
||
|
|
/** 加载进度条 */
|
||
|
|
@property (nonatomic, strong) YJWebProgressLayer *webProgressLayer;
|
||
|
|
/** 网络加载失败提示 */
|
||
|
|
@property (nonatomic, strong) PYHUD *loadFailView;
|
||
|
|
/** 当前请求 */
|
||
|
|
@property (nonatomic, strong) NSURLRequest *request;
|
||
|
|
|
||
|
|
@end
|
||
|
|
|
||
|
|
@implementation PYWebController
|
||
|
|
|
||
|
|
+ (instancetype)webViewControllerWithUrl:(NSString *)url title:(NSString *)title
|
||
|
|
{
|
||
|
|
PYWebController *vc = [[PYWebController alloc] init];
|
||
|
|
vc.webTitle = title;
|
||
|
|
vc.url = url;
|
||
|
|
return vc;
|
||
|
|
}
|
||
|
|
|
||
|
|
- (PYHUD *)loadFailView
|
||
|
|
{
|
||
|
|
if (!_loadFailView) {
|
||
|
|
PYHUD *loadFailView = [[PYHUD alloc]initWithFrame:self.view.bounds];
|
||
|
|
loadFailView.indicatorBackGroundColor = [UIColor clearColor];
|
||
|
|
loadFailView.messageLabel.text = @"网络迷路了...\n快点我刷新下";
|
||
|
|
loadFailView.backgroundColor = [UIColor whiteColor];
|
||
|
|
loadFailView.customImage = [UIImage imageNamed:@"networkError"];
|
||
|
|
__weak typeof(self) weakSelf = self;
|
||
|
|
[loadFailView refreshWhenNwrworkError:^(PYHUD *hud) {
|
||
|
|
[weakSelf.webView loadRequest:self.request];
|
||
|
|
}];
|
||
|
|
_loadFailView = loadFailView;
|
||
|
|
}
|
||
|
|
return _loadFailView;
|
||
|
|
}
|
||
|
|
|
||
|
|
- (void)viewDidLoad {
|
||
|
|
[super viewDidLoad];
|
||
|
|
|
||
|
|
// 设置UI
|
||
|
|
[self setupUI];
|
||
|
|
}
|
||
|
|
|
||
|
|
- (void)setupUI {
|
||
|
|
|
||
|
|
self.view.backgroundColor = [UIColor whiteColor];
|
||
|
|
_webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT)];
|
||
|
|
_webView.navigationDelegate = self;
|
||
|
|
_webView.backgroundColor = [UIColor whiteColor];
|
||
|
|
self.progressColor = THEME_COLOR;
|
||
|
|
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.url]];
|
||
|
|
self.request = request;
|
||
|
|
[_webView loadRequest:request];
|
||
|
|
_webProgressLayer = [[YJWebProgressLayer alloc] init];
|
||
|
|
_webProgressLayer.frame = CGRectMake(0, self.navigationController.navigationBar.height - 2, self.view.frame.size.width, 2);
|
||
|
|
_webProgressLayer.strokeColor = self.progressColor.CGColor;
|
||
|
|
[self.navigationController.navigationBar.layer addSublayer:_webProgressLayer];
|
||
|
|
[self.view addSubview:_webView];
|
||
|
|
|
||
|
|
if (self.canShare) {
|
||
|
|
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:ImageNamed(@"share") style:UIBarButtonItemStyleDone target:self action:@selector(shareDidClick)];
|
||
|
|
}
|
||
|
|
|
||
|
|
self.navigationItem.title = self.webTitle;
|
||
|
|
}
|
||
|
|
|
||
|
|
- (void)setCanShare:(BOOL)canShare
|
||
|
|
{
|
||
|
|
_canShare = canShare;
|
||
|
|
|
||
|
|
if (!canShare) {
|
||
|
|
self.navigationItem.rightBarButtonItem = nil;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
- (void)shareDidClick
|
||
|
|
{
|
||
|
|
if (_shareAction) {
|
||
|
|
_shareAction();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
MTShareMedia *media = [[MTShareMedia alloc] init];
|
||
|
|
media.url = [NSURL URLWithString:@"https://wwww.qq.com"];
|
||
|
|
media.title = @"分享标题";
|
||
|
|
media.content = @"这个是分享的内容";
|
||
|
|
[MTService(MTShareService) showShareToolBarWithMedia:media];
|
||
|
|
}
|
||
|
|
|
||
|
|
- (void)viewWillAppear:(BOOL)animated
|
||
|
|
{
|
||
|
|
[super viewWillAppear:animated];
|
||
|
|
|
||
|
|
_webProgressLayer.hidden = NO;
|
||
|
|
}
|
||
|
|
|
||
|
|
- (void)viewWillDisappear:(BOOL)animated
|
||
|
|
{
|
||
|
|
[super viewWillDisappear:animated];
|
||
|
|
|
||
|
|
_webProgressLayer.hidden = YES;
|
||
|
|
}
|
||
|
|
|
||
|
|
#pragma mark - UIWebViewDelegate
|
||
|
|
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
|
||
|
|
[self.loadFailView hide];
|
||
|
|
[_webProgressLayer startLoad];
|
||
|
|
}
|
||
|
|
|
||
|
|
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
|
||
|
|
[_webProgressLayer finishedLoadWithError:nil];
|
||
|
|
}
|
||
|
|
|
||
|
|
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
|
||
|
|
[_webProgressLayer finishedLoadWithError:error];
|
||
|
|
// 网络加载异常
|
||
|
|
[self.loadFailView showAtView:self.webView hudType:JHUDLoadingTypeCustomAnimations];
|
||
|
|
}
|
||
|
|
|
||
|
|
- (void)dealloc {
|
||
|
|
[_webProgressLayer closeTimer];
|
||
|
|
[_webProgressLayer removeFromSuperlayer];
|
||
|
|
_webProgressLayer = nil;
|
||
|
|
}
|
||
|
|
|
||
|
|
@end
|