.h
@property (nonatomic, strong) NSString *webUrl;
.m
#import "UIBarButtonItem+Item.h"
#import <WebKit/WebKit.h>
#import <JavaScriptCore/JavaScriptCore.h>
@property (nonatomic, strong) WKWebView *webView;
/** 返回按钮图片名 **/
@property (nonatomic, strong) NSString *backImageName;
/** 导航栏返回字体颜色 */
@property (nonatomic, strong) UIColor *navColor;
@implementation WebViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self getUi];
}
- (void)back{
if ([self.webView canGoBack]) {
/// 网页可以返回 就进行网页返回
[self.webView goBack];
} else{
[self.navigationController popViewControllerAnimated:YES];
}
}
- (void)backLatst{
[self.navigationController popViewControllerAnimated:YES];
}
- (void)getUi{
self.view.backgroundColor = COLOR(whiteColor);
[self.view addSubview:self.webView];
self.backImageName = @"白色返回";
UIColor *navBackColor = [UIColor new];
CGFloat lineHeight = 0;
self.navColor = COLOR(whiteColor);
if (self.navigationController.navigationBar.barTintColor == COLOR(whiteColor) ) {
self.backImageName = @"返回";
self.navColor = GetColorWithHexadecimal(0x111111, 1);
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, MAINSCREEN_WIDTH, 0.5)];
view.backgroundColor = GetColorWithHexadecimal(0xd0d0d0, 1);
[self.view addSubview:view];
lineHeight = 0.5;
}
UIBarButtonItem *item = [UIBarButtonItem backItemWithImage:IMGNAME(self.backImageName) titleColor:self.navColor addTarget:self action:@selector(back) edgInsetStyle:MKButtonEdgeInsetsStyleLeft title:@"返回"];
self.navigationItem.leftBarButtonItem = item;
[self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
MLEFT(0);
MRIGHT(0);
MBOTTON(0);
MTOP(lineHeight);
}];
self.navigationController.navigationBar.tintColor = COLOR(whiteColor);
}
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{
WKFrameInfo *frameInfo = navigationAction.targetFrame;
if (![frameInfo isMainFrame]) {
[webView loadRequest:navigationAction.request];
}
return nil;
}
#pragma mark - WKNavigationDelegate
// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
REFRESHSATR(@"")
}
// 内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
REFRESHSTOP
//返回按钮
UIBarButtonItem *back = [UIBarButtonItem backItemWithImage:IMGNAME(self.backImageName) titleColor:self.navColor addTarget:self action:@selector(back) edgInsetStyle:MKButtonEdgeInsetsStyleLeft title:@"返回"];
//关闭按钮
UIBarButtonItem *close = [UIBarButtonItem closeItmeWithTitle:@"关闭" font:17 andColor:self.navColor addTarget:self action:@selector(backLatst)];
if ([webView canGoBack]) {
self.navigationItem.leftBarButtonItems = @[back,close];
} else{
self.navigationItem.leftBarButtonItems = @[back];
}
}
// 页面加载完成时调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
REFRESHSTOP
self.navigationItem.title = webView.title;
}
-(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
NSURL *url = navigationAction.request.URL;
NSString *scheme = [url scheme];
if (![scheme isEqualToString:@"http"] && ![scheme isEqualToString:@"https"]) {
REFRESHSTOP
dispatch_async(dispatch_get_global_queue(0, 0), ^{
if (@available(iOS 10.0, *)){
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
}];
} else{
[[UIApplication sharedApplication] openURL:url];
}
});
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
if (navigationAction.targetFrame == nil) {
[webView loadRequest:navigationAction.request];
}
decisionHandler(WKNavigationActionPolicyAllow);
}
#pragma mark - Getter/Setter
- (WKWebView *)webView{
if (!_webView) {
WKWebViewConfiguration *config = [WKWebViewConfiguration new];
// NSMutableString *str = [NSMutableString string];
// [str appendString:@"var header = document.getElementsByTagName(\"header\")[0];"];
// //移除头部的导航栏
// [str appendString:@"header.parentNode.removeChild(header);"];
// WKUserScript *script = [[WKUserScript alloc] initWithSource:str injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
// [config.userContentController addUserScript:script];
_webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0,MAINSCREEN_WIDTH, MAINSCREEN_HEIGHT ) configuration:config];
_webView.backgroundColor = COLOR(whiteColor);
_webView.scrollView.showsVerticalScrollIndicator = NO;
_webView.scrollView.showsHorizontalScrollIndicator = NO;
_webView.allowsBackForwardNavigationGestures=YES;
NSString *url = self.webUrl;
if (self.webUrl.length >4 && [[self.webUrl substringToIndex:4] isEqualToString:@"www."]) {
url = [NSString stringWithFormat:@"http://%@",url];
}
NSURL *loadUrl = [NSURL URLWithString:url];
_webView.navigationDelegate = self;
_webView.UIDelegate = self;
NSURLRequest *request = [NSURLRequest requestWithURL:loadUrl];
[_webView loadRequest:request];
}
return _webView;
}
//MARK: - 视图生命周期
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
if (self.navigationController.navigationBar.barTintColor == COLOR(whiteColor)) {
//状态栏字体黑色
UIStatuBlackColor
} else{
//状态栏字体白色
UIStatuWhiteColor
}
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
REFRESHSTOP
}
网友评论