123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- //
- // ISAdBanner.m
- // XenonSDK
- //
- // Created by fq on 2020/12/25.
- // Copyright © 2020 SAGESSE. All rights reserved.
- //
- #import "ISAdBanner.h"
- #import <IronSource/IronSource.h>
- #import "XSNetwork.h"
- @interface ISAdBanner()<ISBannerDelegate>
- //是否加载好
- @property (nonatomic, assign)BOOL ready;
- //是否需要显示
- @property (nonatomic, assign)BOOL isShow;
- @property (nonatomic, strong)ISBannerView *bannerView;
- @end
- @implementation ISAdBanner
- - (instancetype)init
- {
- self = [super init];
- if (self) {
-
- self.isShow = NO;
- self.ready = NO;
- self.adId = @""; //广告位名称
- self.agentName = @"ironsource"; //平台名称
- self.medium = @""; //中介
- self.adUnitId = @"Banner"; //原平台单元id
- self.type = @"Banner"; //广告类型
- self.unitAdId = @""; //广告组ID
- self.errorMsg = @""; //错误信息
-
- //init
- [IronSource setBannerDelegate:self];
-
- }
- return self;
- }
- -(BOOL)isReady {
- return self.ready;
- }
- //显示
- - (void)play:(NSString *)adId callback:(void (^)(int))back {
- if (self.isShow) {
- return;
- }
-
- self.adId = adId;
- self.isShow = YES;
-
- if (self.bannerView != nil) {
- UIWindow *window = [[UIApplication sharedApplication].delegate window];
- if (window == [self currentWindow]) {
- [window addSubview:self.bannerView];
- }
- }else{
- UIWindow *window = [self currentWindow];
- //
- [IronSource loadBannerWithViewController:window.rootViewController size:ISBannerSize_BANNER];
- [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)close {
- self.isShow = NO;
-
- if (self.bannerView != nil) {
- [self.bannerView removeFromSuperview];
- [self destroyBanner];
- }
- }
- //销毁
- - (void)destroyBanner {
- dispatch_async(dispatch_get_main_queue(), ^{
- if (self.bannerView) {
- [IronSource destroyBanner:self.bannerView];
- self.bannerView = nil;
- }
- });
- }
- #pragma mark - ISBannerDelegate
- /**
- Called after a banner ad has been successfully loaded
- */
- - (void)bannerDidLoad:(ISBannerView *)bannerView {
- NSLog(@"%s",__PRETTY_FUNCTION__);
- dispatch_async(dispatch_get_main_queue(), ^{
- self.bannerView = bannerView;
- self.ready = YES;
-
- UIWindow *window = [self currentWindow];
- if (window) {
- UIViewController *viewController = window.rootViewController;
- if (@available(iOS 11.0, *)) {
- [self.bannerView setCenter:CGPointMake(viewController.view.center.x, viewController.view.frame.size.height -(self.bannerView.frame.size.height/2.0) - viewController.view.safeAreaInsets.bottom)];
- }else{
- [self.bannerView setCenter:CGPointMake(viewController.view.center.x, viewController.view.frame.size.height -(self.bannerView.frame.size.height/2.0))];
- }
-
- if (self.isShow) {
- [window addSubview:self.bannerView];
- }
- [XSNetwork adRecord:@"show" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
- }
- });
- }
- /**
- Called after a banner has attempted to load an ad but failed.
- @param error The reason for the error
- */
- - (void)bannerDidFailToLoadWithError:(NSError *)error {
- NSLog(@"%s",__PRETTY_FUNCTION__);
- self.ready = NO;
- //收集错误日志上报服务器.
- self.errorMsg = [NSString stringWithFormat:@"code=%ld,domain=%@,describe=%@",(long)error.code,error.domain,error.userInfo];
- }
- /**
- Called after a banner has been clicked.
- */
- - (void)didClickBanner {
- NSLog(@"%s",__PRETTY_FUNCTION__);
-
- [XSNetwork adRecord:@"click" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
-
- }
- /**
- Called when a banner is about to present a full screen content.
- */
- - (void)bannerWillPresentScreen {
- NSLog(@"%s",__PRETTY_FUNCTION__);
- }
- /**
- Called after a full screen content has been dismissed.
- */
- - (void)bannerDidDismissScreen {
- NSLog(@"%s",__PRETTY_FUNCTION__);
- [XSNetwork adRecord:@"close" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
- }
- /**
- Called when a user would be taken out of the application context.
- */
- - (void)bannerWillLeaveApplication {
- NSLog(@"%s",__PRETTY_FUNCTION__);
- }
- @end
|