穿山甲廣告
:官方網(wǎng)站文檔
穿山甲及廣告位ID
:創(chuàng)建穿山甲應用和廣告位
我公司之前使用的是廣點通
廣告骤宣,但是在點擊變現(xiàn)時,穿山甲
變現(xiàn)的更多序愚,所以公司要求在我們項目中接入穿山甲
替換掉原來的廣點通
被环,
下面就是我在網(wǎng)上找到的iOS APP接入穿山甲
文檔:
Podfile文件:
#(穿山甲.廣告)
#SDK版本 >=3.4.0.0
pod 'Ads-CN' # (4.0.0.5)
#SDK版本 <3.4.0.0
pod 'Bytedance-UnionAD' # (3.3.6.2)
相關鏈接:
CocosCreator接入穿山甲SDK(IOS)--激勵視屏
Creator iOS接入穿山甲SDK
首先介紹一下
.m文件與.mm文件有什么區(qū)別
.m文件 支持C ,OC
.mm文件 支持C,C++,OC
OC 與 TS 互調(diào)贩耐,例子有很多,比如官方的例子里面就有
TS 調(diào)用OC的時候有個點 初學者可能有點懵
//錯誤示例
//oc中代碼 微信分享圖片
+(void) wxShareImage:(NSString*) filepath : (NSString*) title : (const int) target
{
}
//上述寫法在OC中并不報錯 但是在TS中我們調(diào)用OC代碼時就會出現(xiàn)參數(shù)錯誤的問題
//TS代碼調(diào)用
jsb.reflection.callStaticMethod("AppController","wxShareImage:",filePath,title,target);
//正確示例
+(void) wxShareImage:(NSString*) filepath titleStr : (NSString*) title targetInter : (const int) target
{
}
//你可能會想什么是titleStr 字符串 targetInter 整型,其實它的可選字符串可以幫助我們閱讀和理解方法聪蘸,尤其是當方法被調(diào)用的時候菇晃。
//這兩個字段就是代表可選字段,用來幫助我們理解的仔粥,OC代碼記得寫上示姿,并與JS代碼配合好就可以了
//TS代碼調(diào)用
jsb.reflection.callStaticMethod("AppController","wxShareImage:titleStr:targetInter:",filePath,title,target);
//無參示范
jsb.reflection.callStaticMethod("AppController","wxShareImage");
//一個參數(shù)示范
jsb.reflection.callStaticMethod("AppController","wxShareImage:","參數(shù)1");
下面開始接入穿山甲SDK
然后先根據(jù)官方文檔配置環(huán)境
假設出現(xiàn)庫引入了,但是代碼不出提示桐早,或者報錯癣缅,可以點擊Finder --> 前往 -->按住options 點擊出現(xiàn)的隱藏文件資源庫 --> Developer -->xcode
清除掉里面的緩存文件夾,關閉xcode,再次打開勘畔,如果還是報錯所灸,自行檢查了
注意:在使用cocoapods導入SDK時,`Bytedance-UnionAD
我導入了好幾次炫七,其中幾次都是因為從
github`下載文件太大爬立,而導致錯誤,好在最后導入進去了万哪。遇到同樣問題的童鞋~可以多試幾次侠驯。
穿山甲SDK初始化
//在AppController.mm文件中的didFinishLaunchingWithOptions方法中調(diào)用
//穿山甲SDK初始化
[BUAdSDKManager setAppID:@"應用ID"];
[BUAdSDKManager setLoglevel:BUAdSDKLogLevelDebug];
[BUAdSDKManager setIsPaidApp:NO];
1.激勵視頻廣告
//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 這個是我自己封裝的調(diào)用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奕巍,廣告事件發(fā)生后會自動回調(diào)回來吟策,我們只需要重新實現(xiàn)這些方法即可
- (void)nativeExpressRewardedVideoAdDidLoad:
(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
NSLog(@"%s",__func__);
[self showRewardVideoAd]; //記載完畢的回調(diào),在這里做顯示
}
- (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(@"看完廣告返回 發(fā)放獎勵");
[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方法中添加
(后續(xù)所有廣告都要這樣操作的止,這里只寫這個典型了)
//記得在初始化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參數(shù)
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里都是在加載回調(diào)氓润,坑
}
- (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; //是否輪轉(zhuǎn) 如果開啟后30s后會自動再次加載banner,不做限制的話赂乐,會出現(xiàn)bannerAd已經(jīng)被關閉,但是又被加載出來
CGFloat screenHeight = CGRectGetHeight([UIScreen mainScreen].bounds);//獲取到屏幕高度
#warning 升級的用戶請注意咖气,初始化方法去掉了imgSize參數(shù)
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(@"調(diào)用開屏廣告展示");
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函數(shù)中挖滤,app->start(); 之前調(diào)用
UIViewController *splashAd = [[TTSplashAd alloc] init];
[_viewController.view addSubview:splashAd.view];
[[TTSplashAd getInstance] ShowSplashAd:@"開屏廣告ID"]; //展示開屏廣告