单军华
2017-07-12 20d1260d26b028897f3c0935c12fc35aa37b2e93
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//
//  BaseWebPage.m
//  airtree
//
//  Created by WindShan on 2016/11/15.
//  Copyright © 2016年 Gloria. All rights reserved.
//
 
#import "BaseWebPage.h"
 
@interface BaseWebPage ()<WKNavigationDelegate>
{
     //MBProgressHUD *HUD;
     NSTimer * netWorkCheckTime;
}
@end
 
@implementation BaseWebPage
 
- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (!_webView)
    {
        _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-60)];
        _webView.navigationDelegate = self;
//        _webView.dataDetectorTypes = UIDataDetectorTypeAll;
//        _webView.scalesPageToFit = YES;//自动对页面进行缩放以适应屏幕
    }
    
    [self.view addSubview:_webView];
    
    //[self loadHtml];
}
 
- (void)dealloc
{
    [_webView stopLoading];
    _webView.navigationDelegate = nil;
}
 
- (void)loadHtml
{
    
    // 10秒定时器
    netWorkCheckTime = [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(checkNetwork) userInfo:nil repeats:NO];
    
    //HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    //HUD.labelText = @"正在加载...";
//    // 隐藏时候从父控件中移除
//    HUD.removeFromSuperViewOnHide = YES;
//    // YES代表需要蒙版效果
//    HUD.dimBackground = YES;
    
    NSURL *url = [[NSURL alloc] initWithString:_urlString];
 
    //NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    
    
    [_webView loadRequest:[NSURLRequest requestWithURL:url]];
    
    //loadHTMLString
    //[_webView loadHTMLString:_urlString baseURL:nil];
    
    //[_webView loadRequest:request];
}
 
-(void) checkNetwork
{
    //if(HUD != nil)
    //    HUD.hidden = YES;
    
    [Global alertMessageEx:@"请检查网络是否正常!" title:@"加载失败" okTtitle:nil cancelTitle:@"OK" delegate:self];
}
 
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
 
#pragma mark - UIWebViewDelegate methods
 
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    LOG_INFO(@"webViewDidFinishLoad");
    //if(HUD != nil)
    //    HUD.hidden = YES;
    
    if(netWorkCheckTime)
       [netWorkCheckTime invalidate];
}
 
 
- (void)webViewDidStartLoad:(UIWebView *)webView{
    
    LOG_INFO(@"webViewDidStartLoad");
    
}
 
 
-(void)webView:(UIWebView*)webView  DidFailLoadWithError:(NSError*)error{
    
    LOG_INFO(@"DidFailLoadWithError");
    //if(HUD != nil)
    //    HUD.hidden = YES;
    
    if(netWorkCheckTime)
        [netWorkCheckTime invalidate];
    
    [Global alertMessage:error.localizedDescription];
    
}
 
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
    return YES;
}
 
#pragma mark - WKNavigationDelegate begin
 
// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
    
}
// 当内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
    
}
// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    //if(HUD != nil)
    //    HUD.hidden = YES;
    
    if(netWorkCheckTime)
        [netWorkCheckTime invalidate];
}
// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation{
    //if(HUD != nil)
    //    HUD.hidden = YES;
    
    if(netWorkCheckTime)
        [netWorkCheckTime invalidate];
    
    [Global alertMessage:navigation.description];
}
 
// 接收到服务器跳转请求之后调用
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{
    
}
 
//// 在收到响应后,决定是否跳转
//- (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{
//    
////    NSLog(@"%@",navigationAction.request.URL.absoluteString);
////    //允许跳转
////    decisionHandler(WKNavigationActionPolicyAllow);
//    //不允许跳转
//    //decisionHandler(WKNavigationActionPolicyCancel);
//}
#pragma mark - WKNavigationDelegate end
/*
#pragma mark - Navigation
 
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
 
@end