//
|
// PPTVController.m
|
// BuWanVideo2.0
|
//
|
// Created by Aeline on 2021/2/27.
|
// Copyright © 2021 com.yeshi.buwansheque.ios. All rights reserved.
|
//
|
|
#import "PPTVController.h"
|
#import "LoggingViewController.h"
|
#import "BuyVipController.h"
|
|
#import "PPTVNavView.h"
|
#import "PPTVPppView.h"
|
#import "PPTVBottomVIpVIew.h"
|
#import <WebKit/WebKit.h>
|
|
#import "dsbridge.h"
|
#import "JSAPI.h"
|
#import <BUAdSDK/BUAdSDK.h>
|
|
@interface PPTVController () <WKNavigationDelegate, WKUIDelegate, PPTVDelegate, BUNativeExpressFullscreenVideoAdDelegate>
|
|
@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;
|
@property (nonatomic, strong) BUNativeExpressFullscreenVideoAd *fullscreenAd;
|
@end
|
|
@implementation PPTVController
|
|
- (void)viewDidLoad {
|
[super viewDidLoad];
|
[self addObservers];
|
[self loadViewConfig];
|
|
NSDictionary *dictionary = [[NSUserDefaults standardUserDefaults] objectForKey:@"buUserInfo"];
|
NSDate *datenow = [NSDate date]; // 现在时间
|
NSInteger timeSp = [datenow timeIntervalSince1970]*1000;
|
if(dictionary)
|
{
|
if ([dictionary[@"vipExpireTime"] integerValue] <= timeSp) {
|
if ([YTHsharedManger startManger].ad[@"videoDetailFullVideo"] && [YTHsharedManger startManger].ad[@"videoDetailFullVideo"]!=[NSNull null] && [[YTHsharedManger startManger].ad[@"videoDetailFullVideo"][@"type"] isEqualToString:@"csj"]) {
|
[self loadFullscreenVideoAd];
|
}
|
}
|
}else{
|
if ([YTHsharedManger startManger].ad[@"videoDetailFullVideo"] && [YTHsharedManger startManger].ad[@"videoDetailFullVideo"]!=[NSNull null] && [[YTHsharedManger startManger].ad[@"videoDetailFullVideo"][@"type"] isEqualToString:@"csj"]) {
|
[self loadFullscreenVideoAd];
|
}
|
}
|
//[self tryPlayFinish];
|
}
|
|
- (void)dealloc
|
{
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
}
|
|
- (void)viewWillAppear:(BOOL)animated {
|
[super viewWillAppear:animated];
|
[self.navigationController setNavigationBarHidden:YES animated:animated];
|
//是否登录
|
if([[NSUserDefaults standardUserDefaults] boolForKey:@"userOnLine"]){
|
[self refrshData];
|
}
|
}
|
|
- (void)viewWillDisappear:(BOOL)animated {
|
[super viewWillDisappear:animated];
|
[self.navigationController setNavigationBarHidden:NO animated:animated];
|
|
}
|
|
- (void)addObservers {
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(login) name:@"h5Login" object:nil];
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tryPlayFinish) name:@"tryPlayFinish" object:nil];
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccess) name:@"RELOAD_DATA" object:nil];
|
}
|
|
- (void)loadViewConfig {
|
[self.view addSubview:self.viewPPTVNav];
|
self.viewPPTVNav.name = _parms[@"name"];
|
self.viewPPTVNav.content = _parms[@"playUrl"];
|
[self.view addSubview:self.webView];
|
[[UIApplication sharedApplication].keyWindow addSubview:self.viewPPTVPop];
|
[[UIApplication sharedApplication].keyWindow addSubview:self.viewPPTVVip];
|
|
NSString *url = self.parms[@"playUrl"];
|
[self loadWebView:url];
|
|
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)loadFullscreenVideoAd {
|
self.fullscreenAd = [[BUNativeExpressFullscreenVideoAd alloc] initWithSlotID:@"946515710"];
|
self.fullscreenAd.delegate = self;
|
[self.fullscreenAd loadAdData];
|
}
|
|
//此方法在视频广告素材加载成功时调用。
|
- (void)nativeExpressFullscreenVideoAdDidLoad:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd {
|
|
}
|
|
//当视频广告素材加载失败时调用此方法。
|
- (void)nativeExpressFullscreenVideoAd:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd didFailWithError:(NSError *_Nullable)error {
|
|
}
|
|
//此方法在成功呈现nativeExpressAdView时调用。
|
- (void)nativeExpressFullscreenVideoAdViewRenderSuccess:(BUNativeExpressFullscreenVideoAd *)rewardedVideoAd {
|
|
}
|
|
//当nativeExpressAdView无法呈现时,将调用此方法
|
- (void)nativeExpressFullscreenVideoAdViewRenderFail:(BUNativeExpressFullscreenVideoAd *)rewardedVideoAd error:(NSError *_Nullable)error {
|
|
}
|
//当视频缓存成功时调用此方法
|
- (void)nativeExpressFullscreenVideoAdDidDownLoadVideo:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd {
|
[fullscreenVideoAd showAdFromRootViewController:self];
|
}
|
|
//此方法在用户单击“跳过”按钮时调用
|
- (void)nativeExpressFullscreenVideoAdDidClickSkip:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd {
|
|
}
|
|
//此方法在视频广告即将关闭时调用。
|
- (void)nativeExpressFullscreenVideoAdWillClose:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd {
|
|
}
|
|
//关闭视频广告时调用此方法
|
- (void)nativeExpressFullscreenVideoAdDidClose:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd {
|
|
}
|
|
//此方法在视频广告播放完成或发生错误时调用。
|
- (void)nativeExpressFullscreenVideoAdDidPlayFinish:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd didFailWithError:(NSError *_Nullable)error {
|
|
|
}
|
|
- (void)refrshData {
|
@ weakify(self)
|
[[YTHNetInterface startInterface] getLoginUserInfoWithUid:[YTHsharedManger startManger].Uid WithSystem:@"1" WithLoginUid:[[NSUserDefaults standardUserDefaults] objectForKey:@"LoginUid"] withBlock:^(BOOL isSuccessful, id result, NSString *error) {
|
@strongify(self)
|
if (isSuccessful) {
|
//首先判断数据是否获成功
|
NSString *err = [NSString stringWithFormat:@"%@",[result objectForKey:@"Error"]];
|
if (err.length == 0) {
|
[[NSUserDefaults standardUserDefaults] setObject:[result objectForKey:@"Data"] forKey:@"buUserInfo"];
|
[[NSUserDefaults standardUserDefaults] synchronize];
|
|
[self.webView removeFromSuperview];
|
self.webView.DSUIDelegate = nil;
|
self.webView.navigationDelegate = nil;
|
self.webView = nil;
|
[self.view addSubview:self.webView];
|
[self loadWebView:self.content];
|
}
|
}
|
}
|
];
|
}
|
|
- (void)loadWebView:(NSString *)url {
|
[self.webView addJavascriptObject:[[JSAPI alloc] init] namespace:@"yestv"];
|
// open debug mode, Release mode should disable this.
|
[self.webView setDebugMode:true];
|
[self.webView customJavascriptDialogLabelTitles:@{@"alertTitle":@"Notification",@"alertBtn":@"OK"}];
|
|
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
|
[self.webView loadRequest:request];
|
}
|
|
-(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 {
|
[_viewPPTVPop removeFromSuperview];
|
[_viewPPTVVip removeFromSuperview];
|
[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:@"复制成功"];
|
|
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"复制成功" preferredStyle:UIAlertControllerStyleAlert];
|
[self presentViewController:alert animated:YES completion:^{
|
//延时执行
|
dispatch_queue_t q = dispatch_get_main_queue();
|
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.2 * NSEC_PER_SEC), q, ^{
|
[alert dismissViewControllerAnimated:YES completion:nil];
|
});
|
}];
|
}
|
|
- (void)login {
|
LoggingViewController *loginVC=[[LoggingViewController alloc] init];
|
loginVC.backNotRoot = YES;
|
[self.navigationController pushViewController:loginVC animated:YES];
|
}
|
|
- (void)tryPlayFinish {
|
[SVProgressHUD show];
|
|
NSString *vid = [self getParamByName:@"vid" URLString:_content];
|
NSString *cid = [self getParamByName:@"cid" URLString:_content];
|
[[YTHNetInterface startInterface] fetchPptvBuyResult:[YTHsharedManger startManger].Uid WithSystem:@"1" WithLoginUid:[[NSUserDefaults standardUserDefaults] objectForKey:@"LoginUid"] url:_content vid:vid cid:cid withBlock:^(BOOL isSuccessful, id result, NSString *error) {
|
|
if (isSuccessful) {
|
self.viewPPTVVip.data = result[@"Data"];
|
BuyVipController *vc = [[BuyVipController alloc] init];
|
vc.url =[[NSUserDefaults standardUserDefaults] objectForKey:VIPLINK];
|
vc.refrshData = ^{
|
[self refrshData];
|
};
|
[self.navigationController pushViewController:vc animated:YES];
|
|
//self.viewPPTVVip.hidden = NO;
|
}
|
[SVProgressHUD dismiss];
|
}];
|
|
//self.viewPPTVVip.hidden = NO;
|
}
|
|
- (void)loginSuccess {
|
[_webView removeFromSuperview];
|
_webView.DSUIDelegate = nil;
|
_webView.navigationDelegate = nil;
|
_webView = nil;
|
[self.view addSubview:self.webView];
|
[self loadWebView:_content];
|
}
|
|
// 页面开始加载时调用
|
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
|
[self jsAdds];
|
}
|
|
// 页面加载失败时调用
|
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
|
|
}
|
|
// 当内容开始返回时调用
|
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
|
[self jsAdds];
|
[self performSelector:@selector(afterDelay) withObject:nil afterDelay:0.5];
|
|
NSString *vid = [self getParamByName:@"vid" URLString:_content];
|
NSString *cid = [self getParamByName:@"cid" URLString:_content];
|
|
[[YTHNetInterface startInterface] fetchPPTVVideoPlayStateWith:[YTHsharedManger startManger].Uid WithSystem:@"1" WithLoginUid:[[NSUserDefaults standardUserDefaults] objectForKey:@"LoginUid"] url:_content vid:vid cid:cid withBlock:^(BOOL isSuccessful, id result, NSString *error) {
|
if (isSuccessful) {
|
if (result[@"Data"][@"title"]) {
|
self.viewPPTVNav.name = result[@"Data"][@"title"];
|
}
|
}
|
}];
|
}
|
|
- (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"]) {
|
if (![_content isEqualToString:strRequest] && _content) {
|
decisionHandler(WKNavigationActionPolicyCancel);
|
self.content = strRequest;
|
[self loginSuccess];
|
return;
|
}
|
self.content = strRequest;
|
self.viewPPTVNav.content = strRequest;
|
[self jsAdds];
|
}
|
|
decisionHandler(WKNavigationActionPolicyAllow);//允许跳转
|
}
|
|
- (void)afterDelay {
|
[self.webView evaluateJavaScript:@"javascript:adPlayFinish()" completionHandler:^(id _Nullable object, NSError * _Nullable error) {}];
|
}
|
|
- (void)jsAdds {
|
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) {}];
|
}
|
|
- (DWKWebView *)webView {
|
if (!_webView) {
|
// 0.网页配置对象
|
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
|
// 1.原生与JS交互管理
|
WKUserContentController *userContentController = [[WKUserContentController alloc] init];
|
|
// 添加
|
config.userContentController = userContentController;
|
|
// 3.WKWebview设置
|
WKPreferences *prefer = [[WKPreferences alloc] init];
|
//设置是否支持javaScript 默认是支持的
|
prefer.javaScriptEnabled = YES;
|
// // 在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.DSUIDelegate = 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;
|
@weakify(self)
|
_viewPPTVVip.buyVip = ^{
|
@strongify(self)
|
BuyVipController *vc = [[BuyVipController alloc] init];
|
vc.url = [[NSUserDefaults standardUserDefaults] objectForKey:VIPLINK];
|
@weakify(self)
|
vc.refrshData = ^{
|
@strongify(self)
|
[self refrshData];
|
};
|
[self.navigationController pushViewController:vc animated:YES];
|
};
|
// _viewPPTVVip.delegate = self;
|
}
|
return _viewPPTVVip;
|
}
|
|
- (NSString *)getParamByName:(NSString *)name URLString:(NSString *)url {
|
|
NSError *error;
|
NSString *regTags=[[NSString alloc] initWithFormat:@"(^|&|\\?)+%@=+([^&]*)(&|$)", name];
|
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regTags
|
options:NSRegularExpressionCaseInsensitive
|
error:&error];
|
|
// 执行匹配的过程
|
NSArray *matches = [regex matchesInString:url
|
options:0
|
range:NSMakeRange(0, [url length])];
|
for (NSTextCheckingResult *match in matches) {
|
NSString *tagValue = [url substringWithRange:[match rangeAtIndex:2]]; // 分组2所对应的串
|
return tagValue;
|
}
|
return @"";
|
}
|
@end
|