al
Aeline
2021-03-08 ce4eaddf9eb835f01e6bf5845a063d306f322b24
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
//
//  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) 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];
    
    //网页视图
    _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的上一级,而不是跟控制器(*指针对侧滑有效)
    [_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];
}
 
 
 
#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{
    
}
 
 
-(BOOL)shouldAutorotate{
    return YES;
}
//支持的方向 因为界面A我们只需要支持竖屏
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    _titleView.frame=CGRectMake(0, 0, KScreenWp, 64);
    _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, 22, KScreenW-100, 32)];
                [webtitle setFont:[UIFont systemFontOfSize:14]];
                [webtitle setTextColor:[UIColor whiteColor]];
                [webtitle setTextAlignment:NSTextAlignmentCenter];
                [_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