cocos穿山甲廣告iOS接入

1.去穿山甲官網(wǎng)下載sdk

https://www.pangle.cn/union/media/union/download

2.把sdk導(dǎo)入到Xcode項(xiàng)目里的Framework里面


1

3.然后根據(jù)穿山甲官網(wǎng)配置各種東西

https://www.pangle.cn/union/media/union/download/detail?id=16&docId=5de8d570b1afac00129330c5&osType=ios

下面的內(nèi)容可以根據(jù)

https://blog.csdn.net/ZFSR05255134/article/details/105595256

4.就是如此簡(jiǎn)單,然后寫(xiě)代碼,每一步都有哦

AppController.h腳本里

2

AppController.mm腳本里

還有需要在第一個(gè)函數(shù)里初始化一下:

需要導(dǎo)入的包為

#import "CsjAdReward.h"

#import?<BUAdSDK/BUAdSDKManager.h>

-------------------------------------------------

// 初始化Csj SDK

[self initCsjAdSDK];

----------------------------------------------------

3

CsjAdReward.h腳本

4

CsjAdReward.m腳本

-------------------------------------------------------------------

#import <Foundation/Foundation.h>

#import "CsjAdReward.h"

#import?<BUAdSDK/BURewardedVideoModel.h>

#import?<BUAdSDK/BUNativeExpressRewardedVideoAd.h>

//#import <BUAdSDK/BURewardedVideoAd.h>

//#import "AppController.h"

#import?<BUAdSDK/BUAdSDKManager.h>

//

#import "CallJS.h"

//#import "RootViewController.h"

@interface CsjAdReward ()<BUNativeExpressRewardedVideoAdDelegate>

@property (nonatomic,strong) BUNativeExpressRewardedVideoAd *rewardedVideoAd;

@end


static CsjAdReward *instance;? ? //這里聲明一下靜態(tài)實(shí)例棚品,方便javascript層調(diào)用


@implementation CsjAdReward


//獲取實(shí)例的方法傅寡,方便javascript層調(diào)用

+(CsjAdReward *) getAdInstance{

? ? return instance;

}


//初始化,根著SDK文檔照做就好了

- (void) viewDidLoad {

? ? NSLog(@"----------viewDidLoad");

? ? instance=self;


? ? BURewardedVideoModel *model=[[BURewardedVideoModel alloc]init];

? ? model.userId=@"賬號(hào)ID";

? ? self.rewardedVideoAd=[[BUNativeExpressRewardedVideoAd alloc] initWithSlotID:@"廣告位ID" rewardedVideoModel:model];

? ? self.rewardedVideoAd.delegate=self;

? ? [self.rewardedVideoAd loadAdData];

}


//打開(kāi)激勵(lì)視頻的方法

-(void) OpenAd{

?? if(self.rewardedVideoAd){

?? ? ? [self.rewardedVideoAd showAdFromRootViewController:self];

? ? }

}


/**

This method is called when video ad material loaded successfully.

*/

- (void)nativeExpressRewardedVideoAdDidLoad:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{

? ? NSLog(@"----------此方法在成功加載視頻廣告素材時(shí)調(diào)用湾盒。");

}


/**

This method is called when video ad materia failed to load.

@param error: the reason of error

*/

- (void)nativeExpressRewardedVideoAd:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd didFailWithError:(NSError *_Nullable)error{

? ? NSLog(@"----------當(dāng)視頻廣告材料加載失敗時(shí)調(diào)用此方法湿右。error.code = %l",error.code);

}


/**

This method is called when cached successfully.

*/

- (void)nativeExpressRewardedVideoAdDidDownLoadVideo:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{

?? ? NSLog(@"----------成功緩存時(shí)調(diào)用此方法。");

}


/**

This method is called when rendering a nativeExpressAdView successed.

*/

- (void)nativeExpressRewardedVideoAdViewRenderSuccess:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{

? ? NSLog(@"----------在成功呈現(xiàn)nativeExpressAdView時(shí)調(diào)用此方法罚勾。");

}

/**

This method is called when a nativeExpressAdView failed to render.

@param error: the reason of error

*/

- (void)nativeExpressRewardedVideoAdViewRenderFail:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd error:(NSError *_Nullable)error{

? ? NSLog(@"----------當(dāng)nativeExpressAdView無(wú)法呈現(xiàn)時(shí)調(diào)用此方法毅人。error.code = %l",error.code);

}

/**

This method is called when video ad slot will be showing.

*/

- (void)nativeExpressRewardedVideoAdWillVisible:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{

? ? NSLog(@"----------此方法在顯示視頻廣告槽時(shí)調(diào)用。");

}

/**

This method is called when video ad slot has been shown.

*/

- (void)nativeExpressRewardedVideoAdDidVisible:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{

?? ? NSLog(@"----------此方法在顯示視頻廣告槽時(shí)調(diào)用尖殃。");

}

/**

This method is called when video ad is about to close.

*/

- (void)nativeExpressRewardedVideoAdWillClose:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{

? ? NSLog(@"----------此方法在視頻廣告即將關(guān)閉時(shí)調(diào)用帚屉。");

}

/**

This method is called when video ad is closed.

*/

- (void)nativeExpressRewardedVideoAdDidClose:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{

? ? NSLog(@"----------此方法在視頻廣告關(guān)閉時(shí)調(diào)用眶根。");

//? ? NSLog(@"-----------nativeExpressRewardedVideoAdDidClose");


? ? // 調(diào)用 js/ts 腳本中的函數(shù): AppMain 節(jié)點(diǎn)下 IOSCtrl 腳本中的 GetAward 的函數(shù)。

? ? [CallJS callJsCCNodeFunc_1:@"視屏廣告看完了,發(fā)獎(jiǎng)勵(lì)羅锋谐!"];


? ? //視頻結(jié)束后,再加載一次廣告數(shù)據(jù)昆烁,保證廣告的不重復(fù)

? ? [self.rewardedVideoAd loadAdData];

}

- (void)testCallJs{

? ? // 調(diào)用 js/ts 腳本中的函數(shù): AppMain 節(jié)點(diǎn)下 IOSCtrl 腳本中的 GetAward 的函數(shù)多矮。

? ? [CallJS callJsCCNodeFunc_1:@"第1個(gè)參數(shù)!"];

? ? // 調(diào)用 js/ts 腳本中的函數(shù): AppMain 節(jié)點(diǎn)下 IOSCtrl 腳本中的 GetAward2 的函數(shù)登失。

? ? [CallJS callJsCCNodeFunc_3:@"GetAward2" withCmd:@"第1個(gè)參數(shù)遏佣!" withContent:@"第2個(gè)參數(shù)!"];

? ? // 調(diào)用 js/ts 的全局函數(shù):GetBigAward

? ? [CallJS callJsGlobalFunc_2:@"GetBigAward" withCmd:@"第1個(gè)參數(shù)壁畸!"];

}

/**

This method is called when video ad is clicked.

*/

- (void)nativeExpressRewardedVideoAdDidClick:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{

? ? NSLog(@"----------此方法在單擊視頻廣告時(shí)調(diào)用贼急。");

}

/**

This method is called when the user clicked skip button.

*/

- (void)nativeExpressRewardedVideoAdDidClickSkip:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{

? ? NSLog(@"----------此方法在單擊視頻廣告時(shí)調(diào)用。");

}

/**

This method is called when video ad play completed or an error occurred.

@param error: the reason of error

*/

- (void)nativeExpressRewardedVideoAdDidPlayFinish:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd didFailWithError:(NSError *_Nullable)error{

? ? NSLog(@"----------此方法在視頻廣告播放完成或發(fā)生錯(cuò)誤時(shí)調(diào)用捏萍。error.code = %l",error.code);

}

/**

Server verification which is requested asynchronously is succeeded.

@param verify:return YES when return value is 2000.

*/

- (void)nativeExpressRewardedVideoAdServerRewardDidSucceed:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd verify:(BOOL)verify{

? ? NSLog(@"----------異步請(qǐng)求的服務(wù)器驗(yàn)證成功太抓。");

}

/**

Server verification which is requested asynchronously is failed.

Return value is not 2000.

*/

- (void)nativeExpressRewardedVideoAdServerRewardDidFail:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{

? ? NSLog(@"----------異步請(qǐng)求的服務(wù)器驗(yàn)證失敗。");

}

@end

-----------------------------------------------------------------------------------

CallJS.h腳本

#import <Foundation/Foundation.h>

@interface CallJS? :UIViewController{

//? ? int countNum;

}

+(void)callJsCCNodeFunc_1:(NSString*) cmdStr;

+(void)callJsCCNodeFunc_3:(NSString*) funcNameStrwithCmd:(NSString*) cmdStrwithContent:(NSString*) contentStr;

+(void)callJsGlobalFunc_1:(NSString*) funcNameStr;

+(void)callJsGlobalFunc_2:(NSString*) funcNameStrwithCmd:(NSString*) cmdStr;

+(void)callJsGlobalFunc_3:(NSString*) funcNameStrwithCmd:(NSString*) cmdStrwithContent:(NSString*) contentStr;

@end


5

CallJS.mm腳本

#import <Foundation/Foundation.h>

#import "cocos2d.h"

#import "CallJS.h"

// 這個(gè)必須導(dǎo)入令杈,否則報(bào)錯(cuò)走敌。

#include "cocos/scripting/js-bindings/jswrapper/SeApi.h"

//using namespace std;


@implementation CallJS


+(void)callJsCCNodeFunc_1:(NSString*) cmdStr{

? ? NSLog(@"-------callJsCCNodeFunc_1...");

? ? std::string param001 = [cmdStr UTF8String];

? ? // "cc.find('AppMain').getComponent('IOSCtrl').GetAward(\"%s\");", param001.c_str()

? ? // 可以理解為:調(diào)用 cc.find() 函數(shù)在場(chǎng)景中查找 AppMain 節(jié)點(diǎn),在利用 getComponent() 函數(shù)獲取該節(jié)點(diǎn)下 名為 IOSCtrl.ts 的腳本逗噩。最后調(diào)用 腳本中的成員函數(shù) GetAward(),此函數(shù)有一個(gè)參數(shù)掉丽。

std::string jsCallStr = cocos2d::StringUtils::format("cc.game.emit(\"VideoAdCallBack\", \"%@\");", param001.c_str());

? ? NSLog(@"--------- jsCallStr = %s", jsCallStr.c_str());

? ? se::ScriptEngine::getInstance()->evalString(jsCallStr.c_str());

}


+(void)callJsCCNodeFunc_3:(NSString*) funcNameStrwithCmd:(NSString*) cmdStrwithContent:(NSString*) contentStr{


? ? NSLog(@"-------callJsCCNodeFunc_3...");


? ? std::string funcName = [funcNameStr UTF8String];

? ? std::string param001 = [cmdStr UTF8String];

? ? std::string param002 = [contentStr UTF8String];


? ? std::string jsCallStr = cocos2d::StringUtils::format("cc.find('AppMain').getComponent('IOSCtrl').%s(\"%s\",\"%s\");", funcName.c_str(),param001.c_str(),param002.c_str());


? ? NSLog(@"--------- jsCallStr = %s", jsCallStr.c_str());

? ? se::ScriptEngine::getInstance()->evalString(jsCallStr.c_str());

}

+(void)callJsGlobalFunc_1:(NSString*) funcNameStr{


? ? NSLog(@"-------callJsGlobalFunc_1...");


? ? std::string funcName = [funcNameStr UTF8String];

? ? std::string jsCallStr = cocos2d::StringUtils::format("%s();",funcName.c_str());

? ? NSLog(@"--------- jsCallStr = %s", jsCallStr.c_str());

? ? se::ScriptEngine::getInstance()->evalString(jsCallStr.c_str());

}


+(void)callJsGlobalFunc_2:(NSString*) funcNameStrwithCmd:(NSString*) cmdStr{


? ? NSLog(@"-------callJsGlobalFunc_2...");


? ? std::string funcName = [funcNameStr UTF8String];

? ? std::string param001 = [cmdStr UTF8String];

? ? std::string jsCallStr = cocos2d::StringUtils::format("%s(\"%s\");",funcName.c_str(), param001.c_str());

? ? NSLog(@"--------- jsCallStr = %s", jsCallStr.c_str());

? ? se::ScriptEngine::getInstance()->evalString(jsCallStr.c_str());

}



+(void)callJsGlobalFunc_3:(NSString*) funcNameStrwithCmd:(NSString*) cmdStrwithContent:(NSString*) contentStr{

? ? NSLog(@"-------callJsGlobalFunc_3...");


? ? std::string funcName = [funcNameStr UTF8String];

? ? std::string param001 = [cmdStr UTF8String];

? ? std::string param002 = [contentStr UTF8String];

? ? std::string jsCallStr = cocos2d::StringUtils::format("%s(\"%s\",\"%s\");",funcName.c_str(), param001.c_str(),param002.c_str());

? ? NSLog(@"--------- jsCallStr = %s", jsCallStr.c_str());

//? ? ScriptingCore::getInstance()->evalString(jsCallStr.c_str());

? ? se::ScriptEngine::getInstance()->evalString(jsCallStr.c_str());

}

@end

6

最后就是cocos中用ts調(diào)用object-c了跌榔,

jsb.reflection.callStaticMethod("AppController", "csjAdRewardOpen:", '參數(shù)');

7

接入完成,最后忘了說(shuō)回調(diào)了捶障,回調(diào)的話(huà)在CallJs.mm文件里有寫(xiě)


8

cocos中記得監(jiān)聽(tīng)消息


9

ok僧须,夠詳細(xì)了!

以下為踩坑步驟:

1. SDK的framework的那幾個(gè)文件只能要放在framework下面

2. 在Other Linker Flags中 不要添加?-all_load

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末项炼,一起剝皮案震驚了整個(gè)濱河市担平,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌锭部,老刑警劉巖暂论,帶你破解...
    沈念sama閱讀 218,941評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異拌禾,居然都是意外死亡取胎,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)湃窍,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)闻蛀,“玉大人,你說(shuō)我怎么就攤上這事坝咐⊙埽” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,345評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵墨坚,是天一觀(guān)的道長(zhǎng)秧饮。 經(jīng)常有香客問(wèn)我,道長(zhǎng)泽篮,這世上最難降的妖魔是什么盗尸? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,851評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮帽撑,結(jié)果婚禮上泼各,老公的妹妹穿的比我還像新娘。我一直安慰自己亏拉,他們只是感情好扣蜻,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,868評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著及塘,像睡著了一般莽使。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上笙僚,一...
    開(kāi)封第一講書(shū)人閱讀 51,688評(píng)論 1 305
  • 那天芳肌,我揣著相機(jī)與錄音,去河邊找鬼。 笑死亿笤,一個(gè)胖子當(dāng)著我的面吹牛翎迁,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播净薛,決...
    沈念sama閱讀 40,414評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼汪榔,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了罕拂?” 一聲冷哼從身側(cè)響起揍异,我...
    開(kāi)封第一講書(shū)人閱讀 39,319評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤全陨,失蹤者是張志新(化名)和其女友劉穎爆班,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體辱姨,經(jīng)...
    沈念sama閱讀 45,775評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡柿菩,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了雨涛。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片枢舶。...
    茶點(diǎn)故事閱讀 40,096評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖替久,靈堂內(nèi)的尸體忽然破棺而出凉泄,到底是詐尸還是另有隱情,我是刑警寧澤蚯根,帶...
    沈念sama閱讀 35,789評(píng)論 5 346
  • 正文 年R本政府宣布后众,位于F島的核電站,受9級(jí)特大地震影響颅拦,放射性物質(zhì)發(fā)生泄漏蒂誉。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,437評(píng)論 3 331
  • 文/蒙蒙 一距帅、第九天 我趴在偏房一處隱蔽的房頂上張望右锨。 院中可真熱鬧,春花似錦碌秸、人聲如沸绍移。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,993評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)蹂窖。三九已至,卻和暖如春允趟,著一層夾襖步出監(jiān)牢的瞬間恼策,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,107評(píng)論 1 271
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留涣楷,地道東北人分唾。 一個(gè)月前我還...
    沈念sama閱讀 48,308評(píng)論 3 372
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像狮斗,于是被迫代替她去往敵國(guó)和親绽乔。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,037評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容