123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- //
- // FqGDTRewardedVideoAd.m
- // XenonSDK
- //
- // Created by fq on 2021/4/30.
- // Copyright © 2021 SAGESSE. All rights reserved.
- //
- #import "FqGDTRewardedVideoAd.h"
- #import "GDTRewardVideoAd.h"
- #import "GDTSDKConfig.h"
- #import <AVFoundation/AVFoundation.h>
- #import "XSNetwork.h"
- #import "GMTools.h"
- @interface FqGDTRewardedVideoAd()<GDTRewardedVideoAdDelegate>
- @property (nonatomic, strong) GDTRewardVideoAd *rewardVideoAd;
- @property(nonatomic,assign)BOOL isAdReadly; //广告是否准备好
- @property(nonatomic,assign)BOOL isReward; //是否发放奖励
- @property(nonatomic,strong)void(^rewardVideoAdCallback)(BOOL,BOOL);
- @property (nonatomic, assign)BOOL videoMuted;
- @property (nonatomic, assign)BOOL audioSession;
- @end
-
- @implementation FqGDTRewardedVideoAd
- -(instancetype)init{
- if (self = [super init]) {
- self.agentName = @"YLH"; //平台名称
- self.adUnitId = @"Rewarded";
- self.type = @"Rewarded"; //广告类型
- self.isAdReadly = NO;
- self.isReward = NO;
- self.videoMuted = YES;
- self.audioSession = YES;
-
- //
- NSDictionary *infos = [[NSBundle mainBundle] infoDictionary];
- if ([infos objectForKey:@"ylh-rewarded-unitId"]) {
- self.adUnitId = [infos objectForKey:@"ylh-rewarded-unitId"]; //广告组ID
- }
- if (self.adUnitId.length == 0) {
- NSLog(@"【优量汇广告】,error=模板2视频激励广告位id为空!!!");
- }
-
- //预加载广告
- [self loadRewardedVideoAdWithPlacementId:self.adUnitId];
-
- }
- return self;
- }
- //打开广告
- -(void)openExpressRewardedVideoAd:(NSString *)adName callback:(void (^)(BOOL, BOOL))callback {
-
- self.adId = adName;
- self.rewardVideoAdCallback = callback;
-
- if (self.rewardVideoAd) {
- [self.rewardVideoAd loadAd];
- }
- }
- //拉取视频激励广告
- - (void)loadRewardedVideoAdWithPlacementId:(NSString *)placementId {
- if (self.rewardVideoAd) {
- self.rewardVideoAd.delegate = nil;
- }
-
- self.rewardVideoAd = [[GDTRewardVideoAd alloc] initWithPlacementId:placementId];
- self.rewardVideoAd.videoMuted = YES;
- self.rewardVideoAd.delegate = self;
-
- // [self.rewardVideoAd loadAd];
- //数据上报
- [XSNetwork adRecord:@"quest" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
-
-
- }
- //播放视频广告
- -(void)playVideoAd {
- if(self.rewardVideoAd.expiredTimestamp <= [[NSDate date] timeIntervalSince1970]) {
- NSLog(@"视频激励广告已过期,请重新拉取");
- return;
- }
-
- if (!self.rewardVideoAd.isAdValid) {
- NSLog(@"视频激励广告失效,请重新拉取");
- return;
- }
-
- [GDTSDKConfig enableDefaultAudioSessionSetting:self.audioSession];
- UIWindow *fK = [GMTools getKeyWindow];
- [self.rewardVideoAd showAdFromRootViewController:fK.rootViewController];
-
- if (self.audioSession) {
- [[AVAudioSession sharedInstance] setActive:NO error:nil];
- [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
- [[AVAudioSession sharedInstance] setActive:YES error:nil];
-
- }
- }
- #pragma mark - GDTRewardVideoAdDelegate
- - (void)gdt_rewardVideoAdDidLoad:(GDTRewardVideoAd *)rewardedVideoAd
- {
- NSLog(@"%s",__FUNCTION__);
- NSLog(@"%@",[NSString stringWithFormat:@"%@ 广告数据加载成功", rewardedVideoAd.adNetworkName]);
- NSLog(@"eCPM:%ld eCPMLevel:%@", [rewardedVideoAd eCPM], [rewardedVideoAd eCPMLevel]);
- NSLog(@"videoDuration :%lf rewardAdType:%ld", rewardedVideoAd.videoDuration, rewardedVideoAd.rewardAdType);
-
- self.isAdReadly = YES;
- if (self.rewardVideoAdCallback) {
- self.rewardVideoAdCallback(self.isAdReadly, self.isReward);
- }
-
- [self playVideoAd];
-
- }
- - (void)gdt_rewardVideoAdVideoDidLoad:(GDTRewardVideoAd *)rewardedVideoAd
- {
- NSLog(@"%s",__FUNCTION__);
- NSLog(@"%@",[NSString stringWithFormat:@"%@ 视频文件加载成功", rewardedVideoAd.adNetworkName]);
- }
- - (void)gdt_rewardVideoAdWillVisible:(GDTRewardVideoAd *)rewardedVideoAd
- {
- NSLog(@"%s",__FUNCTION__);
- NSLog(@"视频播放页即将打开");
- }
- - (void)gdt_rewardVideoAdDidExposed:(GDTRewardVideoAd *)rewardedVideoAd
- {
- NSLog(@"%s",__FUNCTION__);
- NSLog(@"%@",[NSString stringWithFormat:@"%@ 广告已曝光", rewardedVideoAd.adNetworkName]);
- NSLog(@"广告已曝光");
- }
- - (void)gdt_rewardVideoAdDidClose:(GDTRewardVideoAd *)rewardedVideoAd
- {
- NSLog(@"%s",__FUNCTION__);
- NSLog(@"%@", [NSString stringWithFormat:@"%@ 广告已关闭", rewardedVideoAd.adNetworkName]);
- NSLog(@"广告已关闭");
-
- //数据上报
- [XSNetwork adRecord:@"close" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
-
- self.rewardVideoAd = nil;
-
- }
- - (void)gdt_rewardVideoAdDidClicked:(GDTRewardVideoAd *)rewardedVideoAd
- {
- NSLog(@"%s",__FUNCTION__);
- NSLog(@"%@",[NSString stringWithFormat:@"%@ 广告已点击", rewardedVideoAd.adNetworkName]);
- NSLog(@"广告已点击");
-
- //数据上报
- [XSNetwork adRecord:@"click" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
-
- }
- - (void)gdt_rewardVideoAd:(GDTRewardVideoAd *)rewardedVideoAd didFailWithError:(NSError *)error
- {
- NSLog(@"%s",__FUNCTION__);
- if (error.code == 4014) {
- NSLog(@"请拉取到广告后再调用展示接口");
- } else if (error.code == 4016) {
- NSLog(@"应用方向与广告位支持方向不一致");
- } else if (error.code == 5012) {
- NSLog(@"广告已过期");
- } else if (error.code == 4015) {
- NSLog(@"广告已经播放过,请重新拉取");
- } else if (error.code == 5002) {
- NSLog(@"视频下载失败");
- } else if (error.code == 5003) {
- NSLog(@"视频播放失败");
- } else if (error.code == 5004) {
- NSLog(@"没有合适的广告");
- } else if (error.code == 5013) {
- NSLog(@"请求太频繁,请稍后再试");
- } else if (error.code == 3002) {
- NSLog(@"网络连接超时");
- } else if (error.code == 5027){
- NSLog(@"页面加载失败");
- }
- NSLog(@"ERROR: %@", error);
-
- self.errorMsg = [NSString stringWithFormat:@"code=%ld,domain=%@,describe=%@",(long)error.code,error.domain,error.userInfo];
-
- self.isAdReadly = NO;
- self.isReward = NO;
- if (self.rewardVideoAdCallback) {
- self.rewardVideoAdCallback(self.isAdReadly, self.isReward);
- }
-
- }
- - (void)gdt_rewardVideoAdDidRewardEffective:(GDTRewardVideoAd *)rewardedVideoAd info:(NSDictionary *)info {
- NSLog(@"%s",__FUNCTION__);
- NSLog(@"播放达到激励条件 transid:%@", [info objectForKey:@"GDT_TRANS_ID"]);
-
- self.isAdReadly = YES;
- self.isReward = YES;
- if (self.rewardVideoAdCallback) {
- self.rewardVideoAdCallback(self.isAdReadly, self.isReward);
- }
-
- }
- - (void)gdt_rewardVideoAdDidPlayFinish:(GDTRewardVideoAd *)rewardedVideoAd
- {
- NSLog(@"%s",__FUNCTION__);
- NSLog(@"视频播放结束");
-
- if (self.audioSession) {
- [[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
- }
- }
- @end
|