al
liurenjie
2021-07-03 1b73d5d0b495f2d643c2523e1b8399f925b4f702
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//
//  PPTVController.m
//  BuWanVideo2.0
//
//  Created by Aeline on 2021/2/27.
//  Copyright © 2021 com.yeshi.buwansheque.ios. All rights reserved.
//
 
#import "PPTVController.h"
#import "PPTVNavView.h"
#import "PPTVPppView.h"
#import "PPTVBottomVIpVIew.h"
#import <WebKit/WebKit.h>
 
#import "dsbridge.h"
#import "JSAPI.h"
 
@interface PPTVController () <WKNavigationDelegate, WKUIDelegate, PPTVDelegate>
 
@property (nonatomic, nullable, strong) DWKWebView *webView;
@property (nonatomic, nullable, strong) PPTVNavView *viewPPTVNav;
@property (nonatomic, nullable, strong) PPTVPppView *viewPPTVPop;
@property (nonatomic, nullable, strong) PPTVBottomVIpVIew *viewPPTVVip;
 
@property (nonatomic, nullable, strong) JSAPI *jsAPI;
 
@property (nonatomic, nullable, copy) NSString *name;
@property (nonatomic, nullable, copy) NSString *content;
@end
 
@implementation PPTVController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadViewConfig];
}
 
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
 
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    
    
}
 
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
  
}
 
- (void)loadViewConfig {
    [self.view addSubview:self.viewPPTVNav];
    self.viewPPTVNav.name = _parms[@"name"];
    self.viewPPTVNav.content = _parms[@"playUrl"];
    [self.view addSubview:self.webView];
    [self.view addSubview:self.viewPPTVPop];
    [self.view addSubview:self.viewPPTVVip];
    
    [self.webView addJavascriptObject:[[JSAPI alloc] init] namespace:@"yestv"];
    [self.webView setDebugMode:true];
    [self.webView customJavascriptDialogLabelTitles:@{@"alertTitle":@"Notification",@"alertBtn":@"OK"}];
    
    //192.168.3.135
    
    NSString *url = self.parms[@"playUrl"];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    [self.webView loadRequest:request];
    
    if (@available(iOS 13, *)) {
   
    } else {
        //将要进入全屏
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startFullScreen) name:UIWindowDidResignKeyNotification object:nil];
        //退出全屏
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endFullScreen) name:UIWindowDidBecomeHiddenNotification object:nil];
    }
    
    //[self.webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
        //修改滚动速度
}
 
-(void)startFullScreen {
    //[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
    UIApplication *application = [UIApplication sharedApplication];
    [application setStatusBarOrientation: UIInterfaceOrientationLandscapeRight];
    application.keyWindow.transform=CGAffineTransformMakeRotation(M_PI/2);
    CGRect frame = [UIScreen mainScreen].applicationFrame;
    application.keyWindow.bounds = CGRectMake(0, 0, frame.size.height + 20, frame.size.width);
}
 
-(void)endFullScreen {
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
    UIApplication *application = [UIApplication sharedApplication];
    [application setStatusBarOrientation: UIInterfaceOrientationLandscapeRight];
    CGRect frame = [UIScreen mainScreen].applicationFrame;
    application.keyWindow.bounds = CGRectMake(0, 0, frame.size.width, frame.size.height + 20);
    [UIView animateWithDuration:0 animations:^{
        application.keyWindow.transform=CGAffineTransformMakeRotation(M_PI * 2);
    }];
}
 
//- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
//    //网页title
//    if ([keyPath isEqualToString:@"title"]) {
//        if (object == self.webView) {
//            self.viewPPTVNav.name = self.webView.title;
//
//        } else {
//            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
//        }
//
//     } else {
//        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
//     }
//}
 
#pragma mark --------- PPTVDelegate ---------
- (void)backVc {
    [self.navigationController popViewControllerAnimated:YES];
}
 
- (void)moreSelect {
    self.viewPPTVPop.hidden = NO;
}
 
- (void)selectType:(NSString *)type {
    if ([type isEqualToString:@"刷新"]) {
        [self.webView reload];
        
    } else  if ([type isEqualToString:@"复制链接"]) {
        [self copyUrl];
        
    }else  if ([type isEqualToString:@"用其他浏览器打开"]) {
        if (@available(iOS 10, *)) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:_content?:@""] options:@{} completionHandler:^(BOOL success) {}];
            
        } else {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:_content?:@""]];
        }
    }
}
 
- (void)copyUrl {
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = _content?:@"";
    [SVProgressHUD showWithStatus:@"复制成功"];
}
 
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
    
    NSLog(@"");
}
 
// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
}
 
// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
    
}
 
// 当内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
    [self performSelector:@selector(afterDelay) withObject:nil afterDelay:0.5];
}
 
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void(^)(WKNavigationActionPolicy))decisionHandler {
    
    NSString *strRequest = navigationAction.request.URL.absoluteString;
    
    if ([strRequest containsString:@"acmd.api.pptv.com"] && [strRequest containsString:@"bwysdqmovie"]) {
        self.content = strRequest;
        self.viewPPTVNav.content = strRequest;
    }
    
    decisionHandler(WKNavigationActionPolicyAllow);//允许跳转
}
 
- (void)afterDelay {
    [self.webView evaluateJavaScript:@"javascript:adPlayFinish()" completionHandler:^(id _Nullable object, NSError * _Nullable error) {}];
    
    NSString *str = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"js"ofType:@"txt"]   encoding:NSUTF8StringEncoding error:nil];
    [self.webView evaluateJavaScript:str completionHandler:^(id _Nullable object, NSError * _Nullable error) {
        NSLog(@"");
    }];
}
 
- (DWKWebView *)webView {
    if (!_webView) {
        // 0.网页配置对象
        WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
        // 1.原生与JS交互管理
        WKUserContentController *userContentController = [[WKUserContentController alloc] init];
        
        /// 解决循环引用
        // 0.在viewdisAppear方法中
        //        [self.wkWebview.configuration.userContentController removeScriptMessageHandlerForName:@"ScanAction"];
        //        [userContentController addScriptMessageHandler:self name:@"ScanAction"];
        
        // 1.继承系统的NSProxy
        //        [userContentController addScriptMessageHandler:(id<WKScriptMessageHandler>)[XPZ_Proxy proxyWithTarget:self] name:@"ScanAction"];
        
        // 2.自定义NSProxy
        //        [userContentController addScriptMessageHandler:(id<WKScriptMessageHandler>)[XPZ_CustomProxy proxyWithTarget:self] name:@"ScanAction"];
        // 添加
        config.userContentController = userContentController;
        
        // 3.WKWebview设置
        WKPreferences *prefer = [[WKPreferences alloc] init];
        //设置是否支持javaScript 默认是支持的
        prefer.javaScriptEnabled = true;
        // // 在iOS上默认为NO,表示是否允许不经过用户交互由javaScript自动打开窗口
        prefer.javaScriptCanOpenWindowsAutomatically = YES;
        // 添加
        config.preferences = prefer;
        config.preferences.minimumFontSize = 0.0f;
        // 是使用h5的视频播放器在线播放, 还是使用原生播放器全屏播放
        config.allowsInlineMediaPlayback = YES;
        //设置视频是否需要用户手动播放  设置为NO则会允许自动播放
        config.mediaTypesRequiringUserActionForPlayback = YES;
        //设置是否允许画中画技术 在特定设备上有效
        config.allowsPictureInPictureMediaPlayback = YES;
        //设置请求的User-Agent信息中应用程序名称 iOS9后可用
        config.applicationNameForUserAgent = @"ChinaDailyForiPad";
        
        config.suppressesIncrementalRendering = YES;
        
        _webView = [[DWKWebView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.viewPPTVNav.frame), KScreenW, KScreenH - CGRectGetHeight(self.viewPPTVNav.frame)) configuration:config];
        
        _webView.UIDelegate = self;
        _webView.navigationDelegate = self;
        // 是否允许手势左滑返回上一级, 类似导航控制的左滑返回
        _webView.allowsBackForwardNavigationGestures = YES;
        //可返回的页面列表, 存储已打开过的网页
        //WKBackForwardList * backForwardList = [_webView backForwardList];
    }
    return _webView;
}
 
- (PPTVNavView *)viewPPTVNav {
    if (!_viewPPTVNav) {
        _viewPPTVNav = [[PPTVNavView alloc] initWithFrame:CGRectMake(0, 0, KScreenW, kStatusBarH + ALNavBarH)];
        _viewPPTVNav.delegate = self;
    }
    return _viewPPTVNav;
}
 
- (PPTVPppView *)viewPPTVPop {
    if (!_viewPPTVPop) {
        _viewPPTVPop = [[PPTVPppView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.viewPPTVNav.frame), KScreenW, KScreenH - CGRectGetHeight(self.viewPPTVNav.frame))];
        _viewPPTVPop.hidden = YES;
        _viewPPTVPop.delegate = self;
    }
    return _viewPPTVPop;
}
 
- (PPTVBottomVIpVIew *)viewPPTVVip {
    if (!_viewPPTVVip) {
        _viewPPTVVip = [[PPTVBottomVIpVIew alloc] initWithFrame:CGRectMake(0, 0, KScreenW, KScreenH)];
       //_viewPPTVVip.hidden = YES;
       // _viewPPTVVip.delegate = self;
    }
    return _viewPPTVVip;
}
@end