123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- //
- // KDPWebViewViewController.m
- // KuDianProject
- //
- // Created by admin on 2019/7/8.
- // Copyright © 2019 KDP. All rights reserved.
- //
- #import "KDPWebViewViewController.h"
- #import <WebKit/WebKit.h>
- @interface KDPWebViewViewController ()<WKUIDelegate,WKNavigationDelegate>
- @property (nonatomic, strong) WKWebView *webView;
- @property (nonatomic, strong) UIProgressView *processView;
- @property (nonatomic, strong) UIButton *backButton;
- @end
- @implementation KDPWebViewViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- [self setContentView];
- [self loadWebPage];
- }
- - (void)setContentView{
- self.navBar.backgroundColor = [UIColor whiteColor];
- self.navBar.navTitleLabel.textColor = [UIColor colorWithHex:0x333333];
- // self.backButton = [UIButton buttonWithType:UIButtonTypeSystem];
- // [self.backButton setImage:[UIImage imageNamed:@"return_black"] forState:UIControlStateNormal];
- // self.backButton.frame = CGRectMake(0, 0, 40, 40);
- // [self.backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
- // [self.navBar addSubview:self.backButton];
-
- [self.navBar addleftReturnButton:self selector:@selector(backAction:)];
-
- WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
- config.allowsInlineMediaPlayback = NO;
- self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, KDNavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-KDNavBarHeight) configuration:config];
- self.webView.backgroundColor = [UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0];
- self.webView.navigationDelegate = self;
- self.webView.UIDelegate = self;
- //使用kvo监听进度
- [self.webView addObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress)) options:0 context:nil];
- //手势触摸滑动
- self.webView.allowsBackForwardNavigationGestures = YES;
- //自适应
- [self.webView sizeToFit];
- [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.webUrl]]];
- [self.view addSubview:self.webView];
-
- self.processView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, KDNavBarHeight, SCREEN_WIDTH, 2)];
- [self.processView setTrackTintColor:[UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0]];
- self.processView.progressTintColor = [UIColor baseColor];
- [self.view addSubview:self.processView];
-
- }
- - (void)loadWebPage{
- if (@available(iOS 9.0, *)) {
- NSArray * types=@[WKWebsiteDataTypeCookies,WKWebsiteDataTypeLocalStorage];
-
- NSSet *websiteDataTypes= [NSSet setWithArray:types];
- NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
-
- [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
-
- }];
- }
- // NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.url]];
- NSString *urlStr = [self.webUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr]
- cachePolicy:NSURLRequestReloadIgnoringCacheData
- timeoutInterval:15.0];
- [self.webView loadRequest:theRequest];
- }
- - (void)setWebUrl:(NSString *)webUrl{
- _webUrl = webUrl;
- }
- - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
- //开始加载的时候,让进度条显示
- self.processView.hidden = NO;
- self.processView.progress = 0;
- self.processView.alpha = 1.0;
- [UIView animateWithDuration:0.8 animations:^{
- self.processView.progress = 0.6;
-
- }];
- }
- - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
- NSURL *url = navigationAction.request.URL;
- UIApplication *app = [UIApplication sharedApplication];
-
- // 打开appstore
- if ([url.absoluteString containsString:@"https://itunes.apple.com/cn/app/"])
- {
- if ([app canOpenURL:url])
- {
- //设备系统为IOS 10.0或者以上的
- if (@available(iOS 10.0, *)) {
- [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
- } else {
- [[UIApplication sharedApplication] openURL:url];
- }
-
- decisionHandler(WKNavigationActionPolicyCancel);
- return;
- }
- }
- decisionHandler(WKNavigationActionPolicyAllow);
- }
- - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
- self.navBar.navTitleLabel.text = webView.title;
- }
- -(void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:animated];
- [UIApplication sharedApplication].statusBarStyle=UIStatusBarStyleDefault;
- self.navBar.navTitleLabel.textColor = [UIColor whiteColor];
- }
- -(void)viewWillDisappear:(BOOL)animated
- {
- [super viewWillDisappear:animated];
- [UIApplication sharedApplication].statusBarStyle=UIStatusBarStyleLightContent;
- }
- - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
- if ([keyPath isEqualToString:NSStringFromSelector(@selector(estimatedProgress))] && object == self.webView ) {
- [self.processView setAlpha:1.0];
-
- [self.processView setProgress:self.webView.estimatedProgress ];
- if (self.webView.estimatedProgress >= 1.0) {
- [UIView animateWithDuration:0.7 animations:^{
- [self.processView setProgress:1.0 animated:YES];
- [self.processView setAlpha:0.0];
-
- }];
-
- }
- }else{
- [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
- }
- }
- - (void)backAction:(UIButton *)sender{
- if (self.presentingViewController) {
- [self dismissViewControllerAnimated:YES completion:nil];
- } else{
- [self.navigationController popViewControllerAnimated:YES];
- }
- }
- //观察的移除
- - (void)dealloc{
- [self.webView removeObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress))];
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- @end
|