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 <Foundation/Foundation.h>
#import <BUAdSDK/BUAdSDKManager.h>
#import <BUAdSDK/BUNativeExpressRewardedVideoAd.h>
#import <BUAdSDK/BURewardedVideoModel.h>
#import "CallJS.h" 
//CallJS.h 這個是我自己封裝的調用JS的類

@interface TTRewardVideoAd()<BUNativeExpressRewardedVideoAdDelegate>
@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 <Foundation/Foundation.h>
#import "TTExpressInsertAd.h"
#import <BUAdSDK/BUNativeExpressInterstitialAd.h>


@interface TTExpressInsertAd()<BUNativeExpresInterstitialAdDelegate>
@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 <Foundation/Foundation.h>
#import "TTBannerAd.h"
#import <BUAdSDK/BUNativeExpressBannerView.h>

@interface TTBannerAd()<BUNativeExpressBannerViewDelegate>
@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<BUDislikeWords *> *)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 <Foundation/Foundation.h>
#import "TTSplashAd.h"
#import <BUAdSDK/BUSplashAdView.h>

@interface TTSplashAd()<BUSplashAdDelegate>
@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"]; //展示開屏廣告
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。