重庆迈尖科技有限公司
2019-05-08 8c116459f2308e9f8564ea8f55b2ca25849b8336
BuWanVideo2.0/recommendView.m
@@ -28,14 +28,17 @@
#import "IndividualStarController.h"//明星详情页
#import "LXAlertView.h"//弹窗视图
#import "GDTNativeAd.h"//广点通原生广告
//#import "GDTNativeAd.h"//广点通原生广告
#import "YTHNetdata.h"
#import "UIImage+GIF.h"
#import "AdCollectionViewCell.h"
#import "GDTNativeExpressAd.h"
#import "GDTNativeExpressAdView.h"
#define collectTag 270
@import GoogleMobileAds;
@interface recommendView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,BSKImagesPageViewViewDelegate,GADNativeAppInstallAdLoaderDelegate, GADNativeContentAdLoaderDelegate,GDTNativeAdDelegate,UIScrollViewDelegate>{
@interface recommendView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,BSKImagesPageViewViewDelegate,GADNativeAppInstallAdLoaderDelegate, GADNativeContentAdLoaderDelegate,UIScrollViewDelegate,GDTNativeExpressAdDelegete>{
    
    //    UICollectionView *_recommentCollectionView;//覆盖整个View的瀑布流
    BSKImagesPageView *_cycleScrollView;//头部的推荐轮播图
@@ -44,8 +47,8 @@
    BOOL googleNoAdToShow;//当这个变量为YES时,说明谷歌广告没有资源
    
    //广点通原生广告
    GDTNativeAd *_nativeAd;     //原生广告实例
    NSArray *nativeArray;//存储请求下来的原生广告信息
//    GDTNativeAd *_nativeAd;     //原生广告实例
//    NSArray *nativeArray;//存储请求下来的原生广告信息
    NSString *_type_id;
    NSString *_type_title;
    NSInteger maintypeNumber;//种类的位置
@@ -82,8 +85,16 @@
@property (nonatomic , strong) NoNetworkView *noNetworkView;
@property (nonatomic, strong) UIViewController *adViewController;
@property (nonatomic, strong) NSArray *expressAdViews;
@property (nonatomic, strong) GDTNativeExpressAd *nativeExpressAd;
@property (nonatomic, strong) NSArray *secExpressAdViews;
@property (nonatomic, strong) GDTNativeExpressAd *secNativeExpressAd;
@property (nonatomic, strong) NSArray *thiExpressAdViews;
@property (nonatomic, strong) GDTNativeExpressAd *thiNativeExpressAd;
@property (nonatomic, strong) NSArray *clSecExpressAdViews;
@property (nonatomic, strong) GDTNativeExpressAd *clSecNativeExpressAd;
@property (nonatomic, strong) NSArray *clThiExpressAdViews;
@property (nonatomic, strong) GDTNativeExpressAd *clThiNativeExpressAd;
@end
@implementation recommendView
@@ -618,13 +629,22 @@
 *  加载广点通原生广告
 */
-(void)loadAd{
//    _nativeAd = [[GDTNativeAd alloc] initWithAppkey:GDTADkey placementId:GDTYSADkey2];
    _nativeAd = [[GDTNativeAd alloc]initWithAppId: GDTADkey placementId:GDTYSADkey2];
    _nativeAd.controller = [self viewController];
    _nativeAd.delegate = self;
    [_nativeAd loadAd:3];
}
    self.nativeExpressAd = [self loadAdWithId:GDTYSADkey2 adSize:CGSizeMake((KScreenW-30)/2, (KScreenW-30)*10/32+55)];
    self.secNativeExpressAd = [self loadAdWithId:GDTYSADkey8 adSize:CGSizeMake(KScreenW, (KScreenW-20)/16*9 + 10)];
    self.thiNativeExpressAd = [self loadAdWithId:GDTYSADkey9 adSize:CGSizeMake(KScreenW, (KScreenW-20)/16*9 + 10)];
    self.clSecNativeExpressAd = [self loadAdWithId:GDTYSADkey10 adSize:CGSizeMake((KScreenW-30)/2, (KScreenW-30)*10/32+55)];
    self.clThiNativeExpressAd = [self loadAdWithId:GDTYSADkey11 adSize:CGSizeMake(KScreenW, (KScreenW-20)/16*9 + 10)];
}
- (GDTNativeExpressAd *)loadAdWithId:(NSString *)placementId adSize:(CGSize)adSize{
    GDTNativeExpressAd * expressAd = [[GDTNativeExpressAd alloc] initWithAppId:GDTADkey placementId:placementId adSize:adSize];
    expressAd.delegate = self;
    // 配置视频播放属性
    expressAd.videoAutoPlayOnWWAN = YES;
    expressAd.videoMuted = NO;
    [expressAd loadAd:1];
    return expressAd;
}
/**
 *  加载谷歌广告
@@ -651,19 +671,19 @@
/**
 *  点击芒果广告的方法
 */
-(void)getMoreInformation:(UIButton *)sender{
    GDTNativeAdData * info;
    if (sender.tag-456==1) {
        info=nativeArray[0];
    }else if(sender.tag-456==_dataHomeType.count){
        info=nativeArray[1];
    }else{
        info=nativeArray[2];
    }
    [_nativeAd clickAd:info];
}
//-(void)getMoreInformation:(UIButton *)sender{
//    GDTNativeAdData * info;
//    if (sender.tag-456==1) {
//        info=nativeArray[0];
//    }else if(sender.tag-456==_dataHomeType.count){
//        info=nativeArray[1];
//    }else{
//        info=nativeArray[2];
//    }
//
//    [_nativeAd clickAd:info];
//
//}
//在主线程刷新collectview,非主线程不能刷新
- (void)collectLoadNewData{
    dispatch_async(dispatch_get_main_queue(), ^{
@@ -754,24 +774,17 @@
                return nil;
            }
            if (indexPath.row == 0 && indexPath.section == 1) {
                AdCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"AdCollectionViewCell" forIndexPath:indexPath];
                GDTNativeAdData *info;
                if(nativeArray.count>2){
                    info=nativeArray[2];
                GDTNativeExpressAdView *adView ;
                if ([_type_id integerValue] == 309) {
                    adView = (GDTNativeExpressAdView *)self.expressAdViews[0];
                }else{
                    adView = (GDTNativeExpressAdView *)self.clSecExpressAdViews[0];
                }
                cell.ADTitle.text=[info.properties objectForKey:GDTNativeAdDataKeyTitle];
                NSLog(@"%@",info.properties);
                cell.ADTitle.backgroundColor=[UIColor clearColor];
                [cell.ADImage setYthImageWithURL:[info.properties objectForKey:GDTNativeAdDataKeyImgUrl] placeholderImage:nil];
                //把button设为透明
                cell.ADClickButton.backgroundColor=[UIColor clearColor];
                [cell.ADClickButton setTag:479];
                [cell.ADClickButton addTarget:self action:@selector(getMoreInformation:) forControlEvents:UIControlEventTouchUpInside];
                [_nativeAd attachAd:info toView:cell];
                NSInteger number = arc4random()%1000;
                cell.bofangNumber.text = [NSString stringWithFormat:@"%ld",(long)number];
                cell.pinglunNumber.text = [NSString stringWithFormat:@"%u",arc4random()%number];
                adView.frame = cell.bounds;
                [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
                [cell.contentView addSubview: adView];
                //添加阴影
                cell.layer.masksToBounds = NO;
@@ -823,14 +836,19 @@
            NSString *TagStr1=[VideoInformation objectForKey:@"Tag"];
            NSString *TagStr2=[VideoDic objectForKey:@"Tag"];
            NSString *Score=[VideoDic objectForKey:@"Score"];
            cell.recommentReplyLabel.hidden = NO;
            if(TagStr1.length>1){
                cell.recommentReplyLabel.text=TagStr1;
            }else if(TagStr2.length>1){
                cell.recommentReplyLabel.text=TagStr2;
            }else if([Score intValue]>0){
                cell.recommentReplyLabel.hidden = YES;
                cell.recommentReplyLabel.text=[NSString stringWithFormat:@"评分:%@",Score];
            }else{
                cell.recommentReplyLabel.text=[NSString stringWithFormat:@"评分:无"];
                cell.recommentReplyLabel.hidden = YES;
            }
            cell.recommentReplyLabel.backgroundColor=[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.7];
            //标题
@@ -857,7 +875,8 @@
                commentCountStr=[NSString stringWithFormat:@"%0.1f万",[[VideoDic objectForKey:@"CommentCount"] floatValue]/10000.0];
            }
            cell.recommentCommentNub.text=[NSString stringWithFormat:@"%@",commentCountStr];
            cell.recommentCommentNub.hidden = YES;
            cell.pinglunPic.hidden = YES;
            //添加阴影
            cell.layer.masksToBounds = NO;
            cell.layer.contentsScale = [UIScreen mainScreen].scale;
@@ -883,21 +902,11 @@
        if (indexPath.row < _dataHomeType.count) {
            if (indexPath.row == 0 && indexPath.section == 1) {
                AdCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"AdCollectionViewCell" forIndexPath:indexPath];
                GDTNativeAdData *info;
                if (nativeArray.count > 2) {
                    info=nativeArray[2];
                }
                cell.ADTitle.text=[info.properties objectForKey:GDTNativeAdDataKeyTitle];
                cell.ADTitle.backgroundColor=[UIColor clearColor];
                [cell.ADImage setYthImageWithURL:[info.properties objectForKey:GDTNativeAdDataKeyImgUrl] placeholderImage:nil];
                //把button设为透明
                cell.ADClickButton.backgroundColor=[UIColor clearColor];
                [cell.ADClickButton setTag:479];
                [cell.ADClickButton addTarget:self action:@selector(getMoreInformation:) forControlEvents:UIControlEventTouchUpInside];
                [_nativeAd attachAd:info toView:cell];
                NSInteger number = arc4random()%1000;
                cell.bofangNumber.text = [NSString stringWithFormat:@"%ld",(long)number];
                cell.pinglunNumber.text = [NSString stringWithFormat:@"%u",arc4random()%number];
                GDTNativeExpressAdView *adView = (GDTNativeExpressAdView *)self.clSecExpressAdViews[0];
                adView.frame = cell.bounds;
                [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
                [cell.contentView addSubview: adView];
                //添加阴影
                cell.layer.masksToBounds = NO;
                cell.layer.contentsScale = [UIScreen mainScreen].scale;
@@ -989,7 +998,8 @@
                commentCountStr=[NSString stringWithFormat:@"%0.1f万",[[VideoDic objectForKey:@"CommentCount"] floatValue]/10000.0];
            }
            cell.recommentCommentNub.text=[NSString stringWithFormat:@"%@",commentCountStr];
            cell.recommentCommentNub.hidden = YES;
            cell.pinglunPic.hidden = YES;
            //添加阴影
            cell.layer.masksToBounds = NO;
            cell.layer.contentsScale = [UIScreen mainScreen].scale;
@@ -1089,34 +1099,20 @@
            if (indexPath.section==1) {
                //返回广告视图
                ADCollectionReusableView *groupfootSection = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"ADCollectionReusableView" forIndexPath:indexPath];
                GDTNativeAdData *info;
                if(indexPath.section==1){
                    info=nativeArray[0];
                GDTNativeExpressAdView *adView;
                if ([_type_id integerValue] == 309) {
                    adView = (GDTNativeExpressAdView *)self.secExpressAdViews[0];
                }else{
                    adView = (GDTNativeExpressAdView *)self.clThiExpressAdViews[0];
                }
                groupfootSection.ADTitle.text=[info.properties objectForKey:GDTNativeAdDataKeyTitle];
                groupfootSection.ADTitle.backgroundColor=[UIColor clearColor];
                [groupfootSection.ADImage setYthImageWithURL:[info.properties objectForKey:GDTNativeAdDataKeyImgUrl] placeholderImage:nil];
                //把button设为透明
                groupfootSection.ADClickButton.backgroundColor=[UIColor clearColor];
                [groupfootSection.ADClickButton setTag:456+indexPath.section];
                [groupfootSection.ADClickButton addTarget:self action:@selector(getMoreInformation:) forControlEvents:UIControlEventTouchUpInside];
                [_nativeAd attachAd:info toView:groupfootSection];
                [groupfootSection addSubview:adView];
                return groupfootSection;
            }else if (indexPath.section==_dataHomeType.count){
                if(googleNoAdToShow){//加载芒果广告
                    ADCollectionReusableView *groupfootSection = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"ADCollectionReusableView" forIndexPath:indexPath];
                    GDTNativeAdData *info;
                    if(indexPath.section==_dataHomeType.count){
                        info=nativeArray[1];
                    }
                    groupfootSection.ADTitle.text=[info.properties objectForKey:GDTNativeAdDataKeyTitle];
                    groupfootSection.ADTitle.backgroundColor=[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.7];
                    [groupfootSection.ADImage setYthImageWithURL:[info.properties objectForKey:GDTNativeAdDataKeyImgUrl] placeholderImage:nil];
                    //把button设为透明
                    groupfootSection.ADClickButton.backgroundColor=[UIColor clearColor];
                    [groupfootSection.ADClickButton setTag:456+indexPath.section];
                    [groupfootSection.ADClickButton addTarget:self action:@selector(getMoreInformation:) forControlEvents:UIControlEventTouchUpInside];
                    [_nativeAd attachAd:info toView:groupfootSection];
                    GDTNativeExpressAdView *adView = (GDTNativeExpressAdView *)self.thiExpressAdViews[0];
                    [groupfootSection addSubview:adView];
                    return groupfootSection;
                }else{
                    if (self.nativeAdView!=nil) {
@@ -1157,17 +1153,20 @@
        return CGSizeMake(0, 0);
        //        }
    }else if(section==1){
        if (nativeArray==nil) {
        if (self.expressAdViews==nil) {
            return CGSizeMake(0, 0);
        }else{
            return CGSizeMake(0, (KScreenW-20)/16*9);
            GDTNativeExpressAdView *adView = (GDTNativeExpressAdView *)self.secExpressAdViews[0];
            return CGSizeMake(0, adView.bounds.size.height);
        }
    }else if(section==_dataHomeType.count){
        if(googleNoAdToShow){
            if (nativeArray==nil) {
            if (self.expressAdViews==nil) {
                return CGSizeMake(0, 0);
            }else{
                return CGSizeMake(0, (KScreenW-20)/16*9);
                GDTNativeExpressAdView *adView = (GDTNativeExpressAdView *)self.thiExpressAdViews[0];
                return CGSizeMake(0, adView.bounds.size.height);
            }
        }else{
            if (self.nativeAdView==nil) {
@@ -1399,18 +1398,18 @@
/**
 *  原生广告加载广告数据成功回调,返回为GDTNativeAdData对象的数组
 */
-(void)nativeAdSuccessToLoad:(NSArray *)nativeAdDataArray{
//-(void)nativeAdSuccessToLoad:(NSArray *)nativeAdDataArray{
//    if (!nativeArray) {
//        nativeArray =[[NSMutableArray alloc]initWithArray:nativeAdDataArray];
//    }else{
//        nativeArray = nativeAdDataArray.mutableCopy;
//    }
    nativeArray = nativeAdDataArray;
    [self collectLoadNewData];
//    nativeArray = nativeAdDataArray;
//    [self collectLoadNewData];
    //    [collectview reloadData];
    //    [_recommentCollectionView reloadData];
}
//}
/**
 *  原生广告加载广告数据失败回调
@@ -1418,6 +1417,75 @@
-(void)nativeAdFailToLoad:(NSError *)error{
    NSLog(@"XYR_%@",error);
}
- (void)nativeExpressAdSuccessToLoad:(GDTNativeExpressAd *)nativeExpressAd views:(NSArray<__kindof GDTNativeExpressAdView *> *)views
{
    if (nativeExpressAd == self.nativeExpressAd) {
        self.expressAdViews = [NSArray arrayWithArray:views];
        if (self.expressAdViews.count) {
            [self.expressAdViews enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                GDTNativeExpressAdView *expressView = (GDTNativeExpressAdView *)obj;
                expressView.controller = [self XC_GetViewControllerWithView:self];
                [expressView render];
            }];
        }
    }
    if (nativeExpressAd == self.secNativeExpressAd) {
        self.secExpressAdViews = [NSArray arrayWithArray:views];
        if (self.secExpressAdViews.count) {
            [self.secExpressAdViews enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                GDTNativeExpressAdView *expressView = (GDTNativeExpressAdView *)obj;
                expressView.controller = [self XC_GetViewControllerWithView:self];
                [expressView render];
            }];
        }
    }
    if (nativeExpressAd == self.thiNativeExpressAd) {
        self.thiExpressAdViews = [NSArray arrayWithArray:views];
        if (self.thiExpressAdViews.count) {
            [self.thiExpressAdViews enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                GDTNativeExpressAdView *expressView = (GDTNativeExpressAdView *)obj;
                expressView.controller = [self XC_GetViewControllerWithView:self];
                [expressView render];
            }];
        }
    }
    if (nativeExpressAd == self.clSecNativeExpressAd) {
        self.clSecExpressAdViews = [NSArray arrayWithArray:views];
        if (self.clSecExpressAdViews.count) {
            [self.clSecExpressAdViews enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                GDTNativeExpressAdView *expressView = (GDTNativeExpressAdView *)obj;
                expressView.controller = [self XC_GetViewControllerWithView:self];
                [expressView render];
            }];
        }
    }
    if (nativeExpressAd == self.clThiNativeExpressAd) {
        self.clThiExpressAdViews = [NSArray arrayWithArray:views];
        if (self.clThiExpressAdViews.count) {
            [self.clThiExpressAdViews enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                GDTNativeExpressAdView *expressView = (GDTNativeExpressAdView *)obj;
                expressView.controller = [self XC_GetViewControllerWithView:self];
                [expressView render];
            }];
        }
    }
    // 广告位 render 后刷新 tableView
    [self collectLoadNewData];
}
- (UIViewController *)XC_GetViewControllerWithView:(UIView *)view
{
    for (UIView* next = [view superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}
- (void)nativeExpressAdFailToLoad:(GDTNativeExpressAd *)nativeExpressAd error:(NSError *)error{
    NSLog(@"%@",error);
}
#pragma mark -GADNativeContentAdLoaderDelegate
- (void)adLoader:(GADAdLoader *)adLoader didReceiveNativeContentAd:(GADNativeContentAd *)nativeContentAd{