zjf
2023-03-06 392b76515f40376b6d36f40a114850ef63650384
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
//  PBAgreementViewController.m
//  IphoneBIMe
//
//  Created by zjf on 2018/9/5.
//  Copyright © 2018年 ProBIM. All rights reserved.
//
 
#import "PBAgreementViewController.h"
#import <WebKit/WebKit.h>
 
@interface PBAgreementViewController ()<WKNavigationDelegate, WKUIDelegate>
@property (nonatomic, strong) WKWebView *wkWebView;
@property (nonatomic, strong) UIProgressView *progressView;
@end
 
@implementation PBAgreementViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNav];
    [self setupUI];
}
- (void)setupNav {
    self.title = @"管理平台用户协议";
    PBBackNavItem *backNav = [PBBackNavItem backNacItem];
    backNav.title = @"返回";
    [backNav addTarget:self action:@selector(backNavAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backNavItem = [[UIBarButtonItem alloc] initWithCustomView:backNav];
    self.navigationItem.leftBarButtonItem = backNavItem;
}
- (void)backNavAction {
    [self dismissViewControllerAnimated:YES completion:nil];
}
 
- (void)setupUI {
    self.progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 64, MainScreenWidth, 2)];
    self.progressView.tintColor = WarningColor;
    self.progressView.trackTintColor = [UIColor whiteColor];
    [self.view addSubview:self.progressView];
    
    [self.wkWebView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    NSString *urlString = @"http://policy.biaddti.com/policy.pdf";
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    request.timeoutInterval = 15.0f;
    [self.wkWebView loadRequest:request];
}
 
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    if (object == self.wkWebView && [keyPath isEqualToString:@"estimatedProgress"]) {
        CGFloat newprogress = [[change objectForKey:NSKeyValueChangeNewKey] doubleValue];
        self.progressView.alpha = 1.0f;
        [self.progressView setProgress:newprogress animated:YES];
        if (newprogress >= 1.0f) {
            [UIView animateWithDuration:0.1f
                                  delay:0.1f
                                options:UIViewAnimationOptionCurveEaseOut
                             animations:^{
                                 self.progressView.alpha = 0.0f;
                             }
                             completion:^(BOOL finished) {
                                 [self.progressView setProgress:0 animated:NO];
                             }];
        }
    } else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}
 
#pragma mark - WKWKNavigationDelegate Methods
//开始加载
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    self.progressView.hidden = NO;
    [YJProgressHUD showProgress:@"" inView:self.view];
}
 
//加载完成
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    [YJProgressHUD hide];
}
 
//加载失败
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    [YJProgressHUD showFailed:@"加载失败" inview:self.view];
}
 
//页面跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    decisionHandler(WKNavigationActionPolicyAllow);
}
 
- (WKWebView *)wkWebView {
    if (!_wkWebView) {
        _wkWebView = [[WKWebView alloc] initWithFrame:self.view.bounds];
        _wkWebView.navigationDelegate = self;
        _wkWebView.UIDelegate = self;
        [self.view addSubview:_wkWebView];
    }
    return _wkWebView;
}
 
 
 
@end