123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- //
- // FqBUDExpressNativeAd.m
- // XenonSDK
- //
- // Created by fq on 2021/4/23.
- // Copyright © 2021 SAGESSE. All rights reserved.
- //
- #import "FqBUDExpressNativeAd.h"
- #import "GMTools.h"
- #import "XSNetwork.h"
- @interface FqBUDExpressNativeAd()<BUNativeExpressAdViewDelegate>
- @property (strong, nonatomic) NSMutableArray<__kindof BUNativeExpressAdView *> *expressAdViews;
- @property (strong, nonatomic) BUNativeExpressAdManager *nativeExpressAdManager;
- @property (strong, nonatomic) NSTimer *timer;
- @property(nonatomic,assign)CGRect rect1;
- @property(nonatomic,assign)NSInteger viewSize;
- @property(nonatomic,assign)BOOL isResourceReadly;
- @property(nonatomic,strong)void(^nativeCallback)(BOOL);
- @end
- @implementation FqBUDExpressNativeAd
- -(instancetype)init{
- if (self = [super init]) {
- self.agentName = @"ChuanSANJIA"; //平台名称
- self.adUnitId = @"native";
- self.type = @"native"; //广告类型
- NSDictionary *infos = [[NSBundle mainBundle] infoDictionary];
- self.adUnitId = [infos objectForKey:@"csj-native-unitId"]; //广告组ID
- self.isResourceReadly = NO;
-
-
- //[self loadNativeAdWithSlotID:self.adUnitId];
- }
- return self;
- }
- -(BOOL)isAdReady {
-
- return self.isResourceReadly;
- }
- //打开广告
- -(void)openNativeAd:(NSString *)adName WidthCGRect:(CGRect)rect BUSize:(NSInteger)size callback:(void (^)(BOOL))callback {
- self.adId = adName;
- self.nativeCallback = callback;
-
- self.rect1 = rect;
- self.viewSize = size;
-
- //加载广告
- [self loadNativeAdWithSlotID:self.adUnitId CGRect:rect BUSize:size];
-
- // dispatch_async(dispatch_get_main_queue(), ^{
- // NSInteger count = 2;
- // [self.nativeExpressAdManager loadAdDataWithCount:count];
- // });
- }
- //加载广告
- - (void)loadNativeAdWithSlotID:(NSString *)slotId CGRect:(CGRect)rect BUSize:(NSInteger)size{
- if (slotId.length == 0) {
- NSLog(@"【穿山甲广告】,error=原生广告广告位id为空!!!");
- return;
- }
-
- if (!self.expressAdViews) {
- self.expressAdViews = [NSMutableArray arrayWithCapacity:20];
- }
- BUAdSlot *slot1 = [[BUAdSlot alloc] init];
-
- slot1.ID = slotId;
- slot1.AdType = BUAdSlotAdTypeFeed;
- slot1.supportRenderControl = YES;
- //BUProposalSize_Interstitial600_400 这里需要跟穿山甲后台设置保持一致.
- BUSize *imgSize = [BUSize sizeBy:size];
- slot1.imgSize = imgSize;
- slot1.position = BUAdSlotPositionFeed;
-
- // self.nativeExpressAdManager可以重用
- if (!self.nativeExpressAdManager) {
- //CGSizeMake(600, 400)
- self.nativeExpressAdManager = [[BUNativeExpressAdManager alloc] initWithSlot:slot1 adSize:CGSizeMake(rect.size.width, rect.size.height)];
- }
- // 不支持中途更改代理,中途更改代理会导致接收不到广告相关回调,如若存在中途更改代理场景,需自行处理相关逻辑,确保广告相关回调正常执行。
- self.nativeExpressAdManager.delegate = self;
-
-
- NSInteger count = 2;
- [self.nativeExpressAdManager loadAdDataWithCount:count];
-
- //上报数据
- [XSNetwork adRecord:@"quest" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
- }
- #pragma mark - BUNativeExpressAdViewDelegate
- - (void)nativeExpressAdSuccessToLoad:(BUNativeExpressAdManager *)nativeExpressAd views:(NSArray<__kindof BUNativeExpressAdView *> *)views {
- [self.expressAdViews removeAllObjects];//【重要】不能保存太多view,需要在合适的时机手动释放不用的,否则内存会过大
- if (views.count) {
-
- [self.expressAdViews addObjectsFromArray:views];
- UIWindow *keyWindow = [GMTools getKeyWindow];
- [views enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
- BUNativeExpressAdView *expressView = (BUNativeExpressAdView *)obj;
- expressView.rootViewController = keyWindow.rootViewController;
- // important: 此处会进行WKWebview的渲染,建议一次最多预加载三个广告,如果超过3个会很大概率导致WKWebview渲染失败。
- [expressView render];
- // *stop = YES;
- }];
- }
-
- UIViewController *v1 = [GMTools getViewControl];
- BUNativeExpressAdView *childView = (BUNativeExpressAdView *)[self.expressAdViews firstObject];
- //childView.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/2);
- childView.center = CGPointMake(self.rect1.origin.x, self.rect1.origin.y);
-
- [v1.view addSubview:childView];
- [self pbud_logWithSEL:_cmd msg:@""];
- }
- - (void)nativeExpressAdFailToLoad:(BUNativeExpressAdManager *)nativeExpressAd error:(NSError *)error {
- [self pbud_logWithSEL:_cmd msg:[NSString stringWithFormat:@"error:%@", error]];
-
- self.errorMsg = [NSString stringWithFormat:@"code=%ld,domain=%@,describe=%@",(long)error.code,error.domain,error.userInfo];
-
- //广告加载失败回调
- self.isResourceReadly = NO;
-
- }
- - (void)nativeExpressAdViewRenderSuccess:(BUNativeExpressAdView *)nativeExpressAdView {
- UIViewController *v1 = [GMTools getViewControl];
- BUNativeExpressAdView *childView = (BUNativeExpressAdView *)[self.expressAdViews firstObject];
- childView.center = CGPointMake(self.rect1.origin.x, self.rect1.origin.y);
- [v1.view addSubview:childView];
-
- //回调成功
- self.isResourceReadly = YES;
- if (self.nativeCallback) {
- self.nativeCallback(self.isResourceReadly);
- }
-
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateCurrentPlayedTime) userInfo:nil repeats:YES];
- [self.timer fire];
- });
- [self pbud_logWithSEL:_cmd msg:[NSString stringWithFormat:@"nativeExpressAdView.videoDuration:%ld", (long)nativeExpressAdView.videoDuration]];
- }
- - (void)updateCurrentPlayedTime {
- for (BUNativeExpressAdView *nativeExpressAdView in self.expressAdViews) {
- [self pbud_logWithSEL:_cmd msg:[NSString stringWithFormat:@"nativeExpressAdView.currentPlayedTime:%.4f", nativeExpressAdView.currentPlayedTime]];
- }
- }
- - (void)nativeExpressAdView:(BUNativeExpressAdView *)nativeExpressAdView stateDidChanged:(BUPlayerPlayState)playerState {
- [self pbud_logWithSEL:_cmd msg:[NSString stringWithFormat:@"playerState:%ld", (long)playerState]];
- }
- - (void)nativeExpressAdViewRenderFail:(BUNativeExpressAdView *)nativeExpressAdView error:(NSError *)error {
- [self pbud_logWithSEL:_cmd msg:[NSString stringWithFormat:@"error:%@ 此广告将会从列表中移除", error]];
- [self.expressAdViews removeObject:nativeExpressAdView];
- UIViewController *v1 = [GMTools getViewControl];
- if (self.expressAdViews.count > 0) {
- [v1.view addSubview:[self.expressAdViews firstObject]];
- }
-
- self.errorMsg = [NSString stringWithFormat:@"code=%ld,domain=%@,describe=%@",(long)error.code,error.domain,error.userInfo];
-
- //回调失败
- self.isResourceReadly = NO;
- if (self.nativeCallback) {
- self.nativeCallback(self.isResourceReadly);
- }
-
- }
- - (void)nativeExpressAdViewWillShow:(BUNativeExpressAdView *)nativeExpressAdView {
- [self pbud_logWithSEL:_cmd msg:@""];
-
- //上报数据
- [XSNetwork adRecord:@"show" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
-
- }
- - (void)nativeExpressAdViewDidClick:(BUNativeExpressAdView *)nativeExpressAdView {
- [self pbud_logWithSEL:_cmd msg:@""];
-
- //上报数据
- [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)nativeExpressAdViewPlayerDidPlayFinish:(BUNativeExpressAdView *)nativeExpressAdView error:(NSError *)error {
- [self pbud_logWithSEL:_cmd msg:@""];
- }
- - (void)nativeExpressAdView:(BUNativeExpressAdView *)nativeExpressAdView dislikeWithReason:(NSArray<BUDislikeWords *> *)filterWords {//【重要】需要在点击叉以后 在这个回调中移除视图,否则,会出现用户点击叉无效的情况
- [self.expressAdViews removeObject:nativeExpressAdView];
- [nativeExpressAdView removeFromSuperview];
- [self pbud_logWithSEL:_cmd msg:@""];
-
- self.nativeExpressAdManager = nil;
-
- }
- - (void)nativeExpressAdViewDidClosed:(BUNativeExpressAdView *)nativeExpressAdView {
- [self pbud_logWithSEL:_cmd msg:@""];
-
-
- self.nativeExpressAdManager = nil;
- //上报数据
- [XSNetwork adRecord:@"close" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
-
- }
- - (void)nativeExpressAdViewWillPresentScreen:(BUNativeExpressAdView *)nativeExpressAdView {
- [self pbud_logWithSEL:_cmd msg:@""];
- }
- - (void)nativeExpressAdViewDidCloseOtherController:(BUNativeExpressAdView *)nativeExpressAdView interactionType:(BUInteractionType)interactionType {
- NSString *str;
- if (interactionType == BUInteractionTypePage) {
- str = @"ladingpage";
- } else if (interactionType == BUInteractionTypeVideoAdDetail) {
- str = @"videoDetail";
- } else {
- str = @"appstoreInApp";
- }
- [self pbud_logWithSEL:_cmd msg:str];
- }
- - (void)pbud_logWithSEL:(SEL)sel msg:(NSString *)msg {
- NSLog(@"SDKDemoDelegate BUNativeExpressFeedAdView In VC (%@) extraMsg:%@", NSStringFromSelector(sel), msg);
- }
- @end
|