cocos穿山甲广告iOS接入

1.去穿山甲官网下载sdk

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

2.把sdk导入到Xcode项目里的Framework里面


1

3.然后根据穿山甲官网配置各种东西

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

下面的内容可以根据

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

4.就是如此简单,然后写代码,每一步都有哦

AppController.h脚本里

2

AppController.mm脚本里

还有需要在第一个函数里初始化一下:

需要导入的包为

#import "CsjAdReward.h"

#import 

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

// 初始化Csj SDK

[self initCsjAdSDK];

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

3

CsjAdReward.h脚本

4

CsjAdReward.m脚本

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

#import

#import "CsjAdReward.h"

#import 

#import 

//#import

//#import "AppController.h"

#import 

//

#import "CallJS.h"

//#import "RootViewController.h"

@interface CsjAdReward ()

@property (nonatomic,strong) BUNativeExpressRewardedVideoAd *rewardedVideoAd;

@end


static CsjAdReward *instance;    //这里声明一下静态实例,方便javascript层调用


@implementation CsjAdReward


//获取实例的方法,方便javascript层调用

+(CsjAdReward *) getAdInstance{

    return instance;

}


//初始化,根着SDK文档照做就好了

- (void) viewDidLoad {

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

    instance=self;


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

    model.userId=@"账号ID";

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

    self.rewardedVideoAd.delegate=self;

    [self.rewardedVideoAd loadAdData];

}


//打开激励视频的方法

-(void) OpenAd{

   if(self.rewardedVideoAd){

       [self.rewardedVideoAd showAdFromRootViewController:self];

    }

}


/**

This method is called when video ad material loaded successfully.

*/

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

    NSLog(@"----------此方法在成功加载视频广告素材时调用。");

}


/**

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(@"----------当视频广告材料加载失败时调用此方法。error.code = %l",error.code);

}


/**

This method is called when cached successfully.

*/

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

     NSLog(@"----------成功缓存时调用此方法。");

}


/**

This method is called when rendering a nativeExpressAdView successed.

*/

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

    NSLog(@"----------在成功呈现nativeExpressAdView时调用此方法。");

}

/**

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(@"----------当nativeExpressAdView无法呈现时调用此方法。error.code = %l",error.code);

}

/**

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

*/

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

    NSLog(@"----------此方法在显示视频广告槽时调用。");

}

/**

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

*/

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

     NSLog(@"----------此方法在显示视频广告槽时调用。");

}

/**

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

*/

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

    NSLog(@"----------此方法在视频广告即将关闭时调用。");

}

/**

This method is called when video ad is closed.

*/

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

    NSLog(@"----------此方法在视频广告关闭时调用。");

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


    // 调用 js/ts 脚本中的函数: AppMain 节点下 IOSCtrl 脚本中的 GetAward 的函数。

    [CallJS callJsCCNodeFunc_1:@"视屏广告看完了,发奖励罗!"];


    //视频结束后,再加载一次广告数据,保证广告的不重复

    [self.rewardedVideoAd loadAdData];

}

- (void)testCallJs{

    // 调用 js/ts 脚本中的函数: AppMain 节点下 IOSCtrl 脚本中的 GetAward 的函数。

    [CallJS callJsCCNodeFunc_1:@"第1个参数!"];

    // 调用 js/ts 脚本中的函数: AppMain 节点下 IOSCtrl 脚本中的 GetAward2 的函数。

    [CallJS callJsCCNodeFunc_3:@"GetAward2" withCmd:@"第1个参数!" withContent:@"第2个参数!"];

    // 调用 js/ts 的全局函数:GetBigAward

    [CallJS callJsGlobalFunc_2:@"GetBigAward" withCmd:@"第1个参数!"];

}

/**

This method is called when video ad is clicked.

*/

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

    NSLog(@"----------此方法在单击视频广告时调用。");

}

/**

This method is called when the user clicked skip button.

*/

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

    NSLog(@"----------此方法在单击视频广告时调用。");

}

/**

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(@"----------此方法在视频广告播放完成或发生错误时调用。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(@"----------异步请求的服务器验证成功。");

}

/**

Server verification which is requested asynchronously is failed.

Return value is not 2000.

*/

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

    NSLog(@"----------异步请求的服务器验证失败。");

}

@end

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

CallJS.h脚本

#import

@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

#import "cocos2d.h"

#import "CallJS.h"

// 这个必须导入,否则报错。

#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()

    // 可以理解为:调用 cc.find() 函数在场景中查找 AppMain 节点,在利用 getComponent() 函数获取该节点下 名为 IOSCtrl.ts 的脚本。最后调用 脚本中的成员函数 GetAward(),此函数有一个参数。

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调用object-c了,

jsb.reflection.callStaticMethod("AppController", "csjAdRewardOpen:", '参数');

7

接入完成,最后忘了说回调了,回调的话在CallJs.mm文件里有写


8

cocos中记得监听消息


9

ok,够详细了!

以下为踩坑步骤:

1. SDK的framework的那几个文件只能要放在framework下面

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

你可能感兴趣的:(cocos穿山甲广告iOS接入)