WKWebView跳转后的页面URL获取处理。直接贴代码。
DDBWebViewController.h
//
// DDBWebViewController.h
// DDBIOS
//
// Created by liwx on 2018/3/14.
// Copyright © 2018年 io.lwx. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface DDBWebViewController : DDBViewController
@property (nonatomic, strong) NSString *urlString;
@property (nonatomic, strong) NSString *titleName;//标题
@end
DDBWebViewController.m
//
// DDBWebViewController.m
// DDBIOS
//
// Created by liwx on 2018/3/14.
// Copyright © 2018年 io.lwx. All rights reserved.
//
#import "DDBWebViewController.h"
#import <WebKit/WebKit.h>
@interface DDBWebViewController ()<WKUIDelegate,WKNavigationDelegate>
@property (weak, nonatomic)WKWebView *webView;
@property (nonatomic, strong) NSString *originUrlString;//原始URL
@end
@implementation DDBWebViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
WKWebView *web = [[WKWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:web];
[web mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
typeof(self) weakSelf = self;
web.UIDelegate = self;
web.navigationDelegate = weakSelf;
self.webView = web;
self.originUrlString = self.urlString.copy;
if (self.urlString.length) {
if (![self.urlString hasPrefix:@"http"]) {
self.urlString = [NSString stringWithFormat:@"http://%@",self.urlString];
}
if ([self.urlString containsString:@"?"]) {
if (![self.urlString containsString:@"fromApp=1"]) {
self.urlString = [self.urlString stringByAppendingString:@"&fromApp=1"];
}
} else {
self.urlString = [self.urlString stringByAppendingString:@"?fromApp=1"];
}
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.urlString]];
[self.webView loadRequest:request];
}
[self initRightItem];
[self initBackItem];
[self.webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
//修改滚动速度
for (UIView *v in self.webView.subviews) {
if ([v isKindOfClass:NSClassFromString(@"WKScrollView")]) {
UIScrollView *sv = (UIScrollView *)v;
sv.decelerationRate = UIScrollViewDecelerationRateFast;
}
}
}
#pragma mark KVO的监听代理
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
//网页title
if ([keyPath isEqualToString:@"title"]) {
if (object == self.webView) {
self.titleName = self.webView.title;
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
#pragma mark 移除观察者
- (void)dealloc
{
[self.webView removeObserver:self forKeyPath:@"title"];
}
- (void)initBackItem {
UIButton *backItem = [UIButton buttonWithType:UIButtonTypeCustom];
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"back_arrow_white"]];
[backItem addSubview:img];
backItem.frame = CGRectMake(0, 0, 60, 44);
img.centerY = backItem.centerY;
[backItem addTarget:self action:@selector(backButtonAction) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backItem];
}
- (void)backButtonAction {
// //返回
// if ([self.webView canGoBack]) {
// [self.webView goBack];
// return;
// }
[self.navigationController popViewControllerAnimated:YES];
}
- (void)initRightItem {
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
[rightButton setImage:[UIImage imageNamed:@"share-white"] forState:UIControlStateNormal];
[rightButton setImage:[UIImage imageNamed:@"share-white"] forState:UIControlStateSelected];
[rightButton setImage:[UIImage imageNamed:@"share-white"] forState:UIControlStateHighlighted];
//rightButton.showsTouchWhenHighlighted = YES;
[rightButton addTarget:self action:@selector(shareAction) forControlEvents:UIControlEventTouchUpInside];
[self.navigationBar addSubview:rightButton];
rightButton.frame = CGRectMake(SCREEN_WIDTH - 44, 0, 44, 44);
}
- (void)shareAction {
//分享URL
[UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
[self sharedToPlatform:platformType];
}];
}
- (void)sharedToPlatform:(UMSocialPlatformType)type {
//
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// 类似 UIWebView的 -webView: shouldStartLoadWithRequest: navigationType:
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation {
NSLog(@"aaaa redirect....");
}
// 在收到响应后,决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
NSLog(@"在收到响应后:%@",navigationResponse.response.URL.absoluteString);
//允许跳转
decisionHandler(WKNavigationResponsePolicyAllow);
//不允许跳转
//decisionHandler(WKNavigationResponsePolicyCancel);
}
// 在发送请求之前,决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
NSString *url = navigationAction.request.URL.absoluteString;
if (navigationAction.targetFrame.isMainFrame) {
NSLog(@"target is main ... %@",url);
if (navigationAction.sourceFrame.mainFrame) {
NSLog(@"source is main...%@",url);
//是原始url 放行
if ([self.urlString isEqualToString:url]) {
decisionHandler(WKNavigationActionPolicyAllow);
NSLog(@"放行bbbbbbbbbbbbbbbbb...%@",url);
return;
}
//不是原始url
if ([url isEqualToString:self.originUrlString]) {
//不是跳转后的url 放行
decisionHandler(WKNavigationActionPolicyAllow);
return;
}
//push后 修改后的url 放行
if ([url containsString:@"fromApp=1"]) {
decisionHandler(WKNavigationActionPolicyAllow);
return;
}
//跳转
NSLog(@"跳转aaaaaaaaaaaaaaaaaaaaaa...%@",url);
decisionHandler(WKNavigationActionPolicyCancel);
DDBWebViewController *pwVC = [[DDBWebViewController alloc] init];
pwVC.urlString = url;
[self.navigationController pushViewController:pwVC animated:YES];
return;
} else {
NSLog(@"source is not main...%@",url);
}
} else {
NSLog(@"target is not main ... %@",url);
}
decisionHandler(WKNavigationActionPolicyAllow);
NSLog(@"在发送请求之前:%@",navigationAction.request.URL.absoluteString);
}
@end