WKWebView单个界面添加请求头

https://www.jianshu.com/p/14b9ea4bf1d4

https://github.com/Yeatse/NSURLProtocol-WebKitSupport/blob/master

重点在这

- (void)setUrl:(NSURL *)url {

    _url = url;

    HWWeakSelf(weakSelf)

//    NSURLRequest *request = [NSURLRequest requestWithURL:weakSelf.url];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:weakSelf.url];

    #pragma mark - 添加请求头 (例子需要根据自己的需求修改)

    NSTimeInterval timeInterval = [[NSDate date] timeIntervalSince1970];//当前时间距离

    NSString *key = [NSString stringWithFormat:@"%.0f0001#j0ZAqg",timeInterval];

 

    [request setValue:[HWDataManager getMD5HashWithMessage:key] forHTTPHeaderField:@"key"];

    [request setValue:[NSString stringWithFormat:@"%.0f000", timeInterval] forHTTPHeaderField:@"times"];

    [_webView loadRequest:request];

}

 

 

 

 

 

 

 

 

 

 

 

 

 

.h文件

#import <UIKit/UIKit.h>

#import <WebKit/WebKit.h>

#pragma mark - 尺寸相关

#define HWScreenW [UIScreen mainScreen].bounds.size.width

#define HWScreenH [UIScreen mainScreen].bounds.size.height

#define HWWeakSelf(weakSelf)  __weak __typeof(&*self)weakSelf = self;  // 弱引用

#ifdef DEBUG // 开发

#define HWLog(...) NSLog(@"%s %d \n%@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])

#else // 生产

#define HWLog(...) //NSLog(@"%s %d \n%@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])

#endif

@interface HWBaseWebViewController : UIViewController

/** <#注释#> */

@property(nonatomic, strong) NSURL *url;

/** <#注释#> */

@property(nonatomic, strong) WKWebView *webView;

@end

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

.m文件

#import "HWBaseWebViewController.h"

 

#define HWAdapterY (HWSystemVersion >= 11?kDevice_Is_iPhoneX?88:64:64)

@interface HWBaseWebViewController ()<WKNavigationDelegate>

/** <#注释#> */

@property(nonatomic, strong) UIView *mainView;

/** <#注释#> */

@property(nonatomic, strong) UIProgressView *progressView;

@end

 

@implementation HWBaseWebViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    [self addMainView];

    [self creatWebView];

    self.view.backgroundColor = [UIColor whiteColor];

}

 

#pragma mark - 添加进度条

- (void)addMainView {

    HWWeakSelf(weakSelf)

    UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, HWScreenW, HWScreenH)];

    mainView.backgroundColor = [UIColor clearColor];

    _mainView = mainView;

    [weakSelf.view addSubview:mainView];

    UIProgressView *progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, [HWTools HWAutoY:self], HWScreenW, 1)];

    progressView.progress = 0;

    _progressView = progressView;

    [weakSelf.view addSubview:progressView];

}

- (void)creatWebView {

    HWWeakSelf(weakSelf)

//    _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, [HWTools HWAutoY:self], HWScreenW, HWScreenH - [HWTools HWAutoY:self])];

    _webView = [[WKWebView alloc] initWithFrame:self.view.bounds];

    _webView.backgroundColor = [UIColor whiteColor];

    _webView.navigationDelegate = weakSelf;

    _webView.scrollView.bounces = NO;

    [weakSelf.mainView addSubview:_webView];

    // 添加观察者

    [_webView addObserver:weakSelf forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL]; // 进度

    [_webView addObserver:weakSelf forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL]; // 标题

}

- (void)setUrl:(NSURL *)url {

    _url = url;

    HWWeakSelf(weakSelf)

//    NSURLRequest *request = [NSURLRequest requestWithURL:weakSelf.url];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:weakSelf.url];

 

    #pragma mark - 添加请求头 (例子需要根据自己的需求修改)

    NSTimeInterval timeInterval = [[NSDate date] timeIntervalSince1970];//当前时间距离

    NSString *key = [NSString stringWithFormat:@"%.0f0001#j0ZAqg",timeInterval];

 

    [request setValue:[HWDataManager getMD5HashWithMessage:key] forHTTPHeaderField:@"key"];

    [request setValue:[NSString stringWithFormat:@"%.0f000", timeInterval] forHTTPHeaderField:@"times"];

 

    [_webView loadRequest:request];

}

 

// 页面加载失败时调用

- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {

    

}

// 页面加载完毕时调用

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {

    

}

#pragma mark - 监听加载进度

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

    HWWeakSelf(weakSelf)

    if ([keyPath isEqualToString:@"estimatedProgress"]) {

        if (object == _webView) {

            [weakSelf.progressView setAlpha:1.0f];

            [weakSelf.progressView setProgress:weakSelf.webView.estimatedProgress animated:YES];

            if(weakSelf.webView.estimatedProgress >= 1.0f) {

                [UIView animateWithDuration:0.3 delay:0.3 options:UIViewAnimationOptionCurveEaseOut animations:^{

                    [weakSelf.progressView setAlpha:0.0f];

                } completion:^(BOOL finished) {

                    [weakSelf.progressView setProgress:0.0f animated:NO];

                }];

            }

        } else {

            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];

        }

    }  else if ([keyPath isEqualToString:@"title"]) {

        HWLog(@"%@",weakSelf.webView.title);

        if (object == weakSelf.webView) {

            weakSelf.title = weakSelf.webView.title;

        } else {

            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];

        }

    } else {

        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];

    }

}

// 当对象即将销毁的时候调用

- (void)dealloc {

    NSLog(@"webView释放");

//    HWWeakSelf(weakSelf)

    [_webView removeObserver:self forKeyPath:@"estimatedProgress"];

    [_webView removeObserver:self forKeyPath:@"title"];

    _webView.navigationDelegate = nil;

}

#pragma mark - WKNavigationDelegate

#pragma mark - 截取当前加载的URL

//- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {

//    HWWeakSelf(weakSelf)

//    NSURL *URL = navigationAction.request.URL;

//    HWLog(@"%@", URL);

//    if (![[NSString stringWithFormat:@"%@", weakSelf.url] isEqualToString:[NSString stringWithFormat:@"%@", URL]]) { // 不相等

//        //        weakSelf.navigationView.titleLabel.text = @"攻略详情";

//        HWStrategyDetailsViewController *vc = [[HWStrategyDetailsViewController alloc] init];

//        vc.url = URL;

//        [weakSelf.navigationController pushViewController:vc animated:YES];

//        //        self.button.hidden = NO;

//        //        _webView.height = HWScreenH-64-50;

//        //        self.collectionButton.hidden = NO;

//        //        self.forwardingButton.hidden = NO;

//        decisionHandler(WKNavigationActionPolicyCancel);

//    }else {

//        weakSelf.navigationView.titleLabel.text = weakSelf.title;

//        decisionHandler(WKNavigationActionPolicyAllow); // 必须实现 不然会崩溃

//    }

//}

@end

 

posted @ 2019-09-27 06:39  sundaysios  阅读(1746)  评论(0编辑  收藏  举报