WKWebView single interface addition request header

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

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

In this focus

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

    _url = url;

    HWWeakSelf(weakSelf)

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

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

    #pragma mark - addition request header (example of the need to modify according to their needs)

    NSTimeInterval timeInterval = [[NSDate date] timeIntervalSince1970]; // current time from

    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 file

#import <UIKit/UIKit.h>

#import <WebKit/WebKit.h>

#pragma mark - size-related

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

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

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

#ifdef DEBUG // Development

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

#else // Production

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

#endif

@interface HWBaseWebViewController : UIViewController

/ ** <# comment #> * /

@property(nonatomic, strong) NSURL *url;

/ ** <# comment #> * /

@property(nonatomic, strong) WKWebView *webView;

@end

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

.m file

#import "HWBaseWebViewController.h"

 

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

@interface HWBaseWebViewController ()<WKNavigationDelegate>

/ ** <# comment #> * /

@property(nonatomic, strong) UIView *mainView;

/ ** <# comment #> * /

@property(nonatomic, strong) UIProgressView *progressView;

@end

 

@implementation HWBaseWebViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    [self addMainView];

    [self creatWebView];

    self.view.backgroundColor = [UIColor whiteColor];

}

 

#pragma mark - add a progress bar

- (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];

    // add observer

    [_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 - addition request header (example of the need to modify according to their needs)

    NSTimeInterval timeInterval = [[NSDate date] timeIntervalSince1970]; // current time from

    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];

}

 

// called when a page fails to load

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

    

}

// called when the page is loaded

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

    

}

#pragma mark - monitor loading progress

- (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];

    }

}

// When an object is about to destroy the call

- (void)dealloc {

    NSLog (@ "webView release");

//    HWWeakSelf(weakSelf)

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

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

    _webView.navigationDelegate = nil;

}

#pragma mark - WKNavigationDelegate

#pragma mark - the interception of the currently loaded 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 = @ "Raiders details";

//        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); // must be achieved or will collapse

//    }

//}

@end

 

Guess you like

Origin www.cnblogs.com/sundaysgarden/p/11595520.html