重庆迈尖科技有限公司
2021-08-18 f35cb784f7edb05b113494e86a00268ef902ae89
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//
//  WEBViewController.m
//  BuWanVideo2.0
//
//  Created by apple on 16/9/18.
//  Copyright © 2016年 com.yeshi.buwansheque.ios. All rights reserved.
//
 
#import "WEBViewController.h"
#import <WebKit/WebKit.h>
 
@interface WEBViewController ()<WKNavigationDelegate>{
    UILabel *webtitle;//网页显示的标题
}
 
@property (strong, nonatomic) WKWebView *webView;
@property (strong, nonatomic) UILabel *titleView;
@property (strong, nonatomic) UILabel *urlView;
@property (strong, nonatomic) UIButton *backBt;
@property (strong, nonatomic) UIButton *reloadBt;
 
@end
 
@implementation WEBViewController
 
- (void)viewDidLoad{
    [super viewDidLoad];
    
    //做的假导航栏
    _titleView=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, KScreenW, 64)];
    _titleView.backgroundColor=kGlobalMainColor;
    _titleView.userInteractionEnabled=YES;
    [self.view addSubview:_titleView];
    //添加网页链接
    _urlView=[[UILabel alloc] initWithFrame:CGRectMake(50, 34, KScreenW-100, 24)];
    [_urlView setFont:[UIFont systemFontOfSize:12]];
    [_urlView setTextColor:[UIColor whiteColor]];
    _urlView.userInteractionEnabled=YES;
    [self.view addSubview:_urlView];
    
    //网页视图
    _webView=[[WKWebView alloc] initWithFrame:CGRectMake(0, 64, KScreenW, KScreenH-64)];
    _webView.contentMode=UIViewContentModeScaleAspectFill;
    _webView.configuration.allowsInlineMediaPlayback=YES;
    
    [_webView setNavigationDelegate:self];
    [_webView setMultipleTouchEnabled:YES];
    [_webView setAutoresizesSubviews:YES];
    [_webView.scrollView setAlwaysBounceVertical:YES];
    // [_webView setCustomUserAgent:@"Mozilla/5.0 (iPhone; CPU iPhone OS 12_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1.1 Mobile/15E148 Safari/604.1"];
    // 获取当前UserAgent, 并对其进行修改
    [_webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id userAgent, NSError * _Nullable error) {
        if ([userAgent isKindOfClass:[NSString class]]) {
            NSString *systemUserAgent =@"";
            systemUserAgent = [NSString stringWithFormat:@"%@ %@", userAgent, @"Safari/604.1" ];
            // 关键代码, 必须实现这个方法, 否则第一次打开UA还是原始值, 待第二次打开webview才是正确的UA;
            [_webView setCustomUserAgent:systemUserAgent];
            
        }
    }];
    
    
    // 这行代码可以是侧滑返回webView的上一级,而不是跟控制器(*指针对侧滑有效)
    [_webView setAllowsBackForwardNavigationGestures:true];
    [self.view addSubview:_webView];
    
    //添加返回按钮
    _backBt=[[UIButton alloc] initWithFrame:CGRectMake(8, 22, 32, 32)];
    [_backBt setImage:[UIImage imageNamed:@"详情页面返回"] forState:UIControlStateNormal];
    [_backBt addTarget:self action:@selector(backButtonPushed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_backBt];
    
    //添加刷新按钮
    _reloadBt=[[UIButton alloc] initWithFrame:CGRectMake(KScreenW-40, 22, 32, 32)];
    [_reloadBt setImage:[UIImage imageNamed:@"网页刷新"] forState:UIControlStateNormal];
    [_reloadBt addTarget:self action:@selector(reloadButtonPushed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_reloadBt];
    
  
    
    //监听标题
    [_webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
    [_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL];
    
   
    
    
    [self loadwebViews];
}
 
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //保持屏幕常亮
    [[UIApplication sharedApplication] setIdleTimerDisabled:YES];
}
 
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    //强制装换屏幕
    NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
    [[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
    
    NSNumber *PortraitorientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:PortraitorientationUnknown forKey:@"orientation"];
    
    //取消屏幕常亮
    [[UIApplication sharedApplication] setIdleTimerDisabled:NO];
    
    //状态栏重现
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
}
 
- (void)backButtonPushed:(id)sender{
    if ([_webView canGoBack]&&![_webView.backForwardList.backList[0].title isEqualToString:webtitle.text]) {
        [_webView goToBackForwardListItem:_webView.backForwardList.backList[0]];
        [SVProgressHUD dismiss];
    }else{
        [self dismissViewControllerAnimated:YES completion:^{
            [SVProgressHUD dismiss];
        }];
    }
}
 
- (void)reloadButtonPushed:(id)sender{ 
    [SVProgressHUD dismiss];
    [_webView reload];
}
 
-(void)loadwebViews{
    NSURLRequest *req = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:_url]];
    [_webView loadRequest:req];
    _urlView.text=_url;
}
 
 
 
#pragma mark -WKNavigationDelegate
// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
    
}
// 当内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
    
}
// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    
}
// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation{
    
}
 
-(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
    WKNavigationActionPolicy policy = WKNavigationActionPolicyAllow;
    @try {
        NSString *url;
        url = navigationAction.request.URL.absoluteString;
        NSLog(@"链接:%@",url);
       
        
        if([url hasPrefix:@"iqiyi"]){
            if ([[UIApplication sharedApplication]
                canOpenURL:[NSURL URLWithString:url]]){
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
            }
        }else{
            
            
            /*
             如果是域名,判断itunes的host链接 ;
             如果是scheme跳转,就判断是否是 App Store 的 scheme
             */
            
            if([[navigationAction.request.URL host] isEqualToString:@"itunes.apple.com"] || [[navigationAction.request.URL scheme] isEqualToString:@"itms-apps"]) {
                if ( [[UIApplication sharedApplication] openURL:navigationAction.request.URL]) {
                    policy = WKNavigationActionPolicyCancel;
                    NSLog(@"已跳转至App Store");
                }
            }
            
        }
    } @catch (NSException *exception) {
        
    } @finally {
        
    }
    decisionHandler(policy);
    
}
 
 
-(BOOL)shouldAutorotate{
    return YES;
}
//支持的方向 因为界面A我们只需要支持竖屏
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    _titleView.frame=CGRectMake(0, 0, KScreenWp, 64);
    _urlView.frame=CGRectMake(50, 34, KScreenWp-100, 24);
    _webView.frame=CGRectMake(0, 64, KScreenWp, KScreenHp-64);
    _backBt.frame=CGRectMake(8, 22, 32, 32);
    _reloadBt.frame=CGRectMake(KScreenWp-40, 22, 32, 32);
    return _orMake;
}
 
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:@"title"]) {
        if (object == _webView) {
            NSString *title = _webView.title;
            if(webtitle==nil){
                webtitle=[[UILabel alloc] initWithFrame:CGRectMake(50, 15, KScreenW-100, 32)];
                [webtitle setFont:[UIFont systemFontOfSize:14]];
                [webtitle setTextColor:[UIColor whiteColor]];
                [webtitle setTextAlignment:NSTextAlignmentLeft];
                [_titleView addSubview:webtitle];
            }
            webtitle.text =title;
        } else {
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        }
    }else if ([keyPath isEqualToString:@"estimatedProgress"]) {
        if (object == _webView) {
            //   double progress = _webView.estimatedProgress;
            //   if(progress!=1){
            //       [SVProgressHUD showProgress:progress status:@"加载中"];
            //   }else{
            //       [SVProgressHUD dismiss];
            //   }
        } else {
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        }
    }
}
 
-(void)dealloc{
    [_webView removeObserver:self forKeyPath:@"estimatedProgress"];
    [_webView removeObserver:self forKeyPath:@"title"];
}
 
@end