Creator iOS接入穿山甲SDK

首先介绍一下

.m文件与.mm文件有什么区别
.m文件 支持C ,OC
.mm文件 支持C,C++,OC

OC 与 TS 互调,例子有很多,比如官方的例子里面就有
TS 调用OC的时候有个点 初学者可能有点懵

//错误示例
//oc中代码  微信分享图片 
+(void) wxShareImage:(NSString*) filepath : (NSString*) title : (const int) target
{

}
//上述写法在OC中并不报错 但是在TS中我们调用OC代码时就会出现参数错误的问题
//TS代码调用
jsb.reflection.callStaticMethod("AppController","wxShareImage:",filePath,title,target);


//正确示例
+(void) wxShareImage:(NSString*) filepath titleStr : (NSString*) title targetInter : (const int) target
{
    
}
//你可能会想什么是titleStr 字符串 targetInter 整型,其实它的可选字符串可以帮助我们阅读和理解方法,尤其是当方法被调用的时候。
//这两个字段就是代表可选字段,用来帮助我们理解的,OC代码记得写上,并与JS代码配合好就可以了
//TS代码调用
jsb.reflection.callStaticMethod("AppController","wxShareImage:titleStr:targetInter:",filePath,title,target);
//无参示范
jsb.reflection.callStaticMethod("AppController","wxShareImage");
//一个参数示范
jsb.reflection.callStaticMethod("AppController","wxShareImage:","参数1");

下面开始接入穿山甲SDK
然后先根据官方文档配置环境
假设出现库引入了,但是代码不出提示,或者报错,可以点击Finder --> 前往 -->按住options 点击出现的隐藏文件资源库 --> Developer -->xcode
清除掉里面的缓存文件夹,关闭xcode,再次打开,如果还是报错,自行检查了

穿山甲SDK初始化

    //在AppController.mm文件中的didFinishLaunchingWithOptions方法中调用
    //穿山甲SDK初始化
    [BUAdSDKManager setAppID:@"应用ID"];
    [BUAdSDKManager setLoglevel:BUAdSDKLogLevelDebug];
    [BUAdSDKManager setIsPaidApp:NO];

激励视频广告

//TTRewardVideoAd.h头文件
@interface TTRewardVideoAd : UIViewController
{
    
}

+(TTRewardVideoAd*) getInstance;
-(void) ShowRewardVideoAd:(NSString *) adId;


@end

//
//  TTRewardVideoAd.mm
//
//
//  Created by Jaymz on 2020/7/18.
//
#import "TTRewardVideoAd.h"
#import 
#import 
#import 
#import 
#import "CallJS.h" 
//CallJS.h 这个是我自己封装的调用JS的类

@interface TTRewardVideoAd()
@property (nonatomic, strong) BUNativeExpressRewardedVideoAd *rewardedVideoAd;
@end

static TTRewardVideoAd* m_instance;
@implementation TTRewardVideoAd

    
-(void)viewDidLoad
{
    [super viewDidLoad];
    m_instance = self;
    NSLog(@"viewDidLoad");
}

+(TTRewardVideoAd*) getInstance
{
    if (m_instance == NULL)
    {
        NSLog(@"获取m_instance失败");
    }
    return m_instance;
}

//拉起视频广告
-(void) ShowRewardVideoAd:(NSString *)slotID
{
    slotID = @"xxxxxx";  //广告ID
    NSLog(@"ShowRewardVideoAd Call !!!");
    BURewardedVideoModel *model = [[BURewardedVideoModel alloc] init];
    model.userId = @"jaymz";
    self.rewardedVideoAd = [[BUNativeExpressRewardedVideoAd alloc] initWithSlotID:slotID rewardedVideoModel:model];
    self.rewardedVideoAd.delegate = self;
    [self.rewardedVideoAd loadAdData];
    NSLog(@"ShowRewardVideoAd over!!");
}

//需要获取到显示在最上面的viewController
- (UIViewController *)theTopviewControler{
    //获取根控制器
    UIViewController *rootVC = [[UIApplication sharedApplication].delegate window].rootViewController;
    
    UIViewController *parent = rootVC;
    //遍历 如果是presentViewController
    while ((parent = rootVC.presentedViewController) != nil ) {
        rootVC = parent;
    }
   
    while ([rootVC isKindOfClass:[UINavigationController class]]) {
        rootVC = [(UINavigationController *)rootVC topViewController];
    }
    return rootVC;
}

-(void) showRewardVideoAd
{
    NSLog(@"展示奖励视频广告");
    if(self.rewardedVideoAd.isAdValid)
    {
       [self.rewardedVideoAd showAdFromRootViewController:[self theTopviewControler]];
    }
}

#pragma mark - BUNativeExpressRewardedVideoAdDelegate
//加载广告的时候,delegate 传的是self,广告事件发生后会自动回调回来,我们只需要重新实现这些方法即可
- (void)nativeExpressRewardedVideoAdDidLoad:
(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
    NSLog(@"%s",__func__);
    [self showRewardVideoAd]; //记载完毕的回调,在这里做显示
}

- (void)nativeExpressRewardedVideoAd:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd didFailWithError:(NSError *_Nullable)error {
    NSLog(@"%s",__func__);
    NSLog(@"error code : %ld , error message : %@",(long)error.code,error.description);
}

- (void)nativeExpressRewardedVideoAdCallback:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd withType:(BUNativeExpressRewardedVideoAdType)nativeExpressVideoType{
    NSLog(@"%s",__func__);
}

- (void)nativeExpressRewardedVideoAdDidDownLoadVideo:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
    NSLog(@"%s",__func__);
}

- (void)nativeExpressRewardedVideoAdViewRenderSuccess:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
    NSLog(@"%s",__func__);
}

- (void)nativeExpressRewardedVideoAdViewRenderFail:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd error:(NSError *_Nullable)error {
    NSLog(@"%s",__func__);
    NSLog(@"视频广告渲染错误");
    [CallJS callJsEngineCallBack:@"window.AdMrg.VideoAdFail":@"广告播放错误"];
}

- (void)nativeExpressRewardedVideoAdWillVisible:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
    NSLog(@"%s",__func__);
}

- (void)nativeExpressRewardedVideoAdDidVisible:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
    NSLog(@"%s",__func__);
}

- (void)nativeExpressRewardedVideoAdWillClose:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
    NSLog(@"%s",__func__);
}

- (void)nativeExpressRewardedVideoAdDidClose:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
    NSLog(@"%s",__func__);
    NSLog(@"看完广告返回 发放奖励");
    [CallJS callJsEngineCallBack:@"window.AdMrg.VideoAdFinish":@""];
}

- (void)nativeExpressRewardedVideoAdDidClick:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
    NSLog(@"%s",__func__);
    NSLog(@"加载激励视频错误");
    [CallJS callJsEngineCallBack:@"window.AdMrg.VideoAdFail":@"广告播放错误"];
}

- (void)nativeExpressRewardedVideoAdDidClickSkip:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
    NSLog(@"%s",__func__);
}

- (void)nativeExpressRewardedVideoAdDidPlayFinish:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd didFailWithError:(NSError *_Nullable)error {
    NSLog(@"%s",__func__);
}

- (void)nativeExpressRewardedVideoAdServerRewardDidSucceed:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd verify:(BOOL)verify {
    NSLog(@"%s",__func__);
}

- (void)nativeExpressRewardedVideoAdServerRewardDidFail:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
    NSLog(@"%s",__func__);
}

- (void)nativeExpressRewardedVideoAdDidCloseOtherController:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd interactionType:(BUInteractionType)interactionType {
    NSString *str = nil;
    if (interactionType == BUInteractionTypePage) {
        str = @"ladingpage";
    } else if (interactionType == BUInteractionTypeVideoAdDetail) {
        str = @"videoDetail";
    } else {
        str = @"appstoreInApp";
    }
    NSLog(@"%s __ %@",__func__,str);
}

@end

添加上述代码后 还需要在AppController.mm的didFinishLaunchingWithOptions方法中添加
(后续所有广告都要这样操作,这里只写这个典型了)

    //记得在初始化SDK之后,_viewController初始化之后
    UIViewController *rewardVideoAd = [[TTRewardVideoAd alloc] init];
    [_viewController.view addSubview:rewardVideoAd.view];

2.插屏广告

//插屏广告(非信息流形式,采用模版渲染)
//
//  TTExpressInsertAd.h
//  
//
//  Created by Jaymz on 2020/7/18.
//

@interface TTExpressInsertAd : UIViewController
{
    
}

+(TTExpressInsertAd*) getInstance;
-(void) ShowExpressInsertAd:(NSString *) adId;
@end

//
//  TTExpressInsertAd.mm
//
//  Created by Jaymz on 2020/7/23.
//

#import 
#import "TTExpressInsertAd.h"
#import 


@interface TTExpressInsertAd()
@property (nonatomic, strong) BUNativeExpressInterstitialAd *interstitialAd;
@end

static TTExpressInsertAd* m_instance;

@implementation TTExpressInsertAd

-(void)viewDidLoad
{
    [super viewDidLoad];
    m_instance = self;
    NSLog(@"viewDidLoad");
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
}

+(TTExpressInsertAd*) getInstance
{
    return m_instance;
}

-(void) ShowExpressInsertAd:(NSString *) slotID
{
    slotID = @"945341872";//945341872
    NSLog(@"ShowExpressInsertAd!!");
    //NSValue *sizeValue = [self.sizeDict objectForKey:slotID];
    CGSize size = CGSizeMake(300, 450);
    CGFloat width = CGRectGetWidth([UIScreen mainScreen].bounds)-40;
    CGFloat height = width/size.width*size.height;
    NSLog(@"width : %f  height : %f",width,height);
    //#warning 升级的用户请注意,初始化方法去掉了imgSize参数
    self.interstitialAd = [[BUNativeExpressInterstitialAd alloc] initWithSlotID:slotID adSize:CGSizeMake(width, height)];
    self.interstitialAd.delegate = self;
    [self.interstitialAd loadAdData];
}

- (void)showInterstitial
{
    NSLog(@"showIntersitial call %d",self.interstitialAd.adValid);
    if (self.interstitialAd)
    {
        NSLog(@"展示插屏广告 showInterstitial");
        [self.interstitialAd showAdFromRootViewController:[self theTopviewControler]];
    }
}

- (UIViewController *)theTopviewControler{
    //获取根控制器
    UIViewController *rootVC = [[UIApplication sharedApplication].delegate window].rootViewController;
    
    UIViewController *parent = rootVC;
    //遍历 如果是presentViewController
    while ((parent = rootVC.presentedViewController) != nil ) {
        rootVC = parent;
    }
   
    while ([rootVC isKindOfClass:[UINavigationController class]]) {
        rootVC = [(UINavigationController *)rootVC topViewController];
    }
    return rootVC;
}

#pragma ---BUNativeExpresInterstitialAdDelegate
- (void)nativeExpresInterstitialAdDidLoad:(BUNativeExpressInterstitialAd *)interstitialAd {
    NSLog(@"%s",__func__);
}

- (void)nativeExpresInterstitialAd:(BUNativeExpressInterstitialAd *)interstitialAd didFailWithError:(NSError *)error {
    NSLog(@"%s",__func__);
}

- (void)nativeExpresInterstitialAdRenderSuccess:(BUNativeExpressInterstitialAd *)interstitialAd {
    NSLog(@"%s",__func__);
    [self showInterstitial];
    //这个插屏广告不能放在加载完毕中,加载完毕广告组件adValid = 0,问了官方才知道要在这里加,文档上跟demo里都是在加载回调,坑
}

- (void)nativeExpresInterstitialAdRenderFail:(BUNativeExpressInterstitialAd *)interstitialAd error:(NSError *)error {
    NSLog(@"%s",__func__);
    NSLog(@"error code : %ld , error message : %@",(long)error.code,error.description);
}

- (void)nativeExpresInterstitialAdWillVisible:(BUNativeExpressInterstitialAd *)interstitialAd {
    NSLog(@"%s",__func__);
}

- (void)nativeExpresInterstitialAdDidClick:(BUNativeExpressInterstitialAd *)interstitialAd {
    NSLog(@"%s",__func__);
}

- (void)nativeExpresInterstitialAdWillClose:(BUNativeExpressInterstitialAd *)interstitialAd {
    NSLog(@"%s",__func__);
}

- (void)nativeExpresInterstitialAdDidClose:(BUNativeExpressInterstitialAd *)interstitialAd {
    NSLog(@"%s",__func__);
}

- (void)nativeExpresInterstitialAdDidCloseOtherController:(BUNativeExpressInterstitialAd *)interstitialAd interactionType:(BUInteractionType)interactionType {
    NSString *str = nil;
    if (interactionType == BUInteractionTypePage) {
        str = @"ladingpage";
    } else if (interactionType == BUInteractionTypeVideoAdDetail) {
        str = @"videoDetail";
    } else {
        str = @"appstoreInApp";
    }
    NSLog(@"%s __ %@",__func__,str);
}

@end

3.Banner广告

//
//  TTBannerAd.h
//
//  Created by Jaymz on 2020/7/24.


@interface TTBannerAd : UIViewController
{
    
}

+(TTBannerAd*) getInstance;
-(void) ShowBannerAd:(NSString *) adId;
-(void) HideBannerAd;

@end

//
//  TTBannerAd.mm
//
//  Created by Jaymz on 2020/7/24.
//

#import 
#import "TTBannerAd.h"
#import 

@interface TTBannerAd()
@property (nonatomic, strong) BUNativeExpressBannerView *bannerView;
@end

static TTBannerAd* m_instance;

@implementation TTBannerAd

+(TTBannerAd*) getInstance
{
    return m_instance;
}

-(void) viewDidLoad
{
    [super viewDidLoad];
    m_instance = self;
    NSLog(@"BannerViewDidLoad");
}

-(void) ShowBannerAd:(NSString *)slotID
{
    NSLog(@"展示bannerAd");
    slotID = @"945344475";
    [self.bannerView removeFromSuperview];
    CGSize size = CGSizeMake(600,150);
    CGFloat screenWidth = CGRectGetWidth([UIScreen mainScreen].bounds);
    CGFloat bannerHeigh = screenWidth/size.width*size.height;//banner按缩放比例变化
    BOOL isOpenRotate = NO; //是否轮转 如果开启后30s后会自动再次加载banner,不做限制的话,会出现bannerAd已经被关闭,但是又被加载出来
    CGFloat screenHeight = CGRectGetHeight([UIScreen mainScreen].bounds);//获取到屏幕高度
    #warning 升级的用户请注意,初始化方法去掉了imgSize参数
    if (isOpenRotate)
    {
        self.bannerView = [[BUNativeExpressBannerView alloc] initWithSlotID:slotID rootViewController:self adSize:CGSizeMake(screenWidth, bannerHeigh) IsSupportDeepLink:YES interval:30]; //设置间隔30s?
    } else {
        self.bannerView = [[BUNativeExpressBannerView alloc] initWithSlotID:slotID rootViewController:self adSize:CGSizeMake(screenWidth, bannerHeigh) IsSupportDeepLink:YES];
    }
    self.bannerView.frame = CGRectMake(0, screenHeight - bannerHeigh - 10, screenWidth, bannerHeigh);
    //self.bannerView.frame = CGRectMake(0, 10, screenWidth, bannerHeigh);
    self.bannerView.delegate = self;
    [self.bannerView loadAdData];
}

-(void) showBanner
{
    NSLog(@"加载banner完成,开始展示");
    [self.view addSubview:self.bannerView];
}

-(void) HideBannerAd
{
    NSLog(@"关闭BannerAd");
    [self.bannerView removeFromSuperview]; //字面意思像删除操作
}

- (UIViewController *)theTopviewControler{
    //获取根控制器
    UIViewController *rootVC = [[UIApplication sharedApplication].delegate window].rootViewController;
    
    UIViewController *parent = rootVC;
    //遍历 如果是presentViewController
    while ((parent = rootVC.presentedViewController) != nil ) {
        rootVC = parent;
    }
   
    while ([rootVC isKindOfClass:[UINavigationController class]]) {
        rootVC = [(UINavigationController *)rootVC topViewController];
    }
    return rootVC;
}

#pragma BUNativeExpressBannerViewDelegate
- (void)nativeExpressBannerAdViewDidLoad:(BUNativeExpressBannerView *)bannerAdView {
    NSLog(@"%s",__func__);
    [self showBanner];
}

- (void)nativeExpressBannerAdView:(BUNativeExpressBannerView *)bannerAdView didLoadFailWithError:(NSError *)error {
    NSLog(@"%s",__func__);
    NSLog(@"error code : %ld , error message : %@",(long)error.code,error.description);
}

- (void)nativeExpressBannerAdViewRenderSuccess:(BUNativeExpressBannerView *)bannerAdView {
    NSLog(@"%s",__func__);
}

- (void)nativeExpressBannerAdViewRenderFail:(BUNativeExpressBannerView *)bannerAdView error:(NSError *)error {
    NSLog(@"%s",__func__);
}

- (void)nativeExpressBannerAdViewWillBecomVisible:(BUNativeExpressBannerView *)bannerAdView {
    NSLog(@"%s",__func__);
}

- (void)nativeExpressBannerAdViewDidClick:(BUNativeExpressBannerView *)bannerAdView {
    NSLog(@"%s",__func__);
}

- (void)nativeExpressBannerAdView:(BUNativeExpressBannerView *)bannerAdView dislikeWithReason:(NSArray *)filterwords {
    NSLog(@"%s",__func__);
    [UIView animateWithDuration:0.25 animations:^{
        bannerAdView.alpha = 0;
    } completion:^(BOOL finished) {
        [bannerAdView removeFromSuperview];
        self.bannerView = nil;
    }];
}

- (void)nativeExpressBannerAdViewDidCloseOtherController:(BUNativeExpressBannerView *)bannerAdView interactionType:(BUInteractionType)interactionType {
    NSString *str = nil;
    if (interactionType == BUInteractionTypePage) {
        str = @"ladingpage";
    } else if (interactionType == BUInteractionTypeVideoAdDetail) {
        str = @"videoDetail";
    } else {
        str = @"appstoreInApp";
    }
    NSLog(@"%s __ %@",__func__,str);
}

@end

4.开屏广告

//
//  TTSplashAd.h
//
//  Created by Jaymz on 2020/7/24.
//

@interface TTSplashAd : UIViewController
{
    
}

+(TTSplashAd*) getInstance;
-(void) ShowSplashAd:(NSString *) adId;
@end

//
//  TTSplashAd.mm
//
//  Created by Jaymz on 2020/7/24.
//

#import 
#import "TTSplashAd.h"
#import 

@interface TTSplashAd()
@property (nonatomic, strong) BUSplashAdView *splashView;
@end

static TTSplashAd* m_instance;

@implementation TTSplashAd

+(TTSplashAd*) getInstance
{
    return m_instance;
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    m_instance = self;
    NSLog(@"viewDidLoad");
}

-(void) ShowSplashAd:(NSString *)adId
{
    NSLog(@"调用开屏广告展示");
    CGRect frame=[UIScreen mainScreen].bounds;
    BUSplashAdView *splashView = [[BUSplashAdView alloc] initWithSlotID:adId frame:frame];
    //yyyyyyyyyy是你在穿山甲后台对应的广告位Id
    splashView.delegate = self;
    UIWindow *keyWindow =[UIApplication sharedApplication].windows.firstObject;
    [splashView loadAdData];
    [keyWindow.rootViewController.view addSubview:splashView];
    splashView.rootViewController = keyWindow.rootViewController;
}

#pragma mark --Splash Delegate
- (void)splashAdDidLoad:(BUSplashAdView *)splashAd {
    NSLog(@"splashAdDidLoad");
    NSLog(@"mediaExt-%@",splashAd.mediaExt);
}

- (void)splashAdDidClose:(BUSplashAdView *)splashAd {
    NSLog(@"splashAdView AdDidClose");
    [splashAd removeFromSuperview];
}

- (void)splashAd:(BUSplashAdView *)splashAd didFailWithError:(NSError *)error {
    NSLog(@"splashAdView load data fail");
    [splashAd removeFromSuperview];
    NSLog(@"error code : %ld , error message : %@",(long)error.code,error.description);
}

- (void)splashAdDidCloseOtherController:(BUSplashAdView *)splashAd interactionType:(BUInteractionType)interactionType {
    NSString *str = @"";
    if (interactionType == BUInteractionTypePage) {
        str = @"ladingpage";
    } else if (interactionType == BUInteractionTypeVideoAdDetail) {
        str = @"videoDetail";
    } else {
        str = @"appstoreInApp";
    }
    //弹窗提示
//    #pragma clang diagnostic push
//    #pragma clang diagnostic ignored"-Wdeprecated-declarations"
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:str message:[NSString stringWithFormat:@"%s",__func__] delegate:self cancelButtonTitle:nil otherButtonTitles:@"ok", nil];
//        [alert show];
//    #pragma clang diagnostic pop
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

开屏广告需要在AppController.mm的didFinishLaunchingWithOptions函数中,app->start(); 之前调用

    UIViewController *splashAd = [[TTSplashAd alloc] init];
    [_viewController.view addSubview:splashAd.view];
    [[TTSplashAd getInstance] ShowSplashAd:@"开屏广告ID"]; //展示开屏广告

你可能感兴趣的:(Creator iOS接入穿山甲SDK)