1.去穿山甲官網(wǎng)下載sdk
https://www.pangle.cn/union/media/union/download
2.把sdk導(dǎo)入到Xcode項(xiàng)目里的Framework里面
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腳本里
AppController.mm腳本里
還有需要在第一個(gè)函數(shù)里初始化一下:
需要導(dǎo)入的包為
#import "CsjAdReward.h"
#import?<BUAdSDK/BUAdSDKManager.h>
-------------------------------------------------
// 初始化Csj SDK
[self initCsjAdSDK];
----------------------------------------------------
CsjAdReward.h腳本
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
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
最后就是cocos中用ts調(diào)用object-c了跌榔,
jsb.reflection.callStaticMethod("AppController", "csjAdRewardOpen:", '參數(shù)');
接入完成,最后忘了說(shuō)回調(diào)了捶障,回調(diào)的話(huà)在CallJs.mm文件里有寫(xiě)
cocos中記得監(jiān)聽(tīng)消息
ok僧须,夠詳細(xì)了!
以下為踩坑步驟:
1. SDK的framework的那幾個(gè)文件只能要放在framework下面
2. 在Other Linker Flags中 不要添加?-all_load