CocosCreator接入穿山甲廣告(IOS)

說明

  • 接入穿山甲廣告SDK

  • 將Cocos Creator工程打包,在手機上運行,看能否正常運行遊戲,保證遊戲沒有問題的前提下再引入SDK所需要的各種包

  • 根據文檔引入各種包,文檔地址:https://partner.oceanengine.com/union/media/union/download/detail?id=1&osType=ios

  • 按前面步驟引入包後,再打包,測試遊戲能否正常運行,能正常運行,說明引入包沒有問題

開屏廣告

  • 開屏廣告是穿山甲中最簡單的廣告,在打開遊戲App後即顯示的廣告
  • 引入頭文件
#import <BUAdSDK/BUAdSDKManager.h>
#import <BUAdSDK/BUSplashAdView.h>
  • 繼承
*@interface xxxxxxxxx () <BUSplashAdDelegate>
//xxxxxxxxx 是你自己的類名
@end
  • 初始化(SDK文檔中有,照做就OK)
[BUAdSDKManager setAppID:@"xxxxxx"]; //xxxxxx是你在穿山甲後臺對應的appid
[BUAdSDKManager setIsPaidApp:NO];
[BUAdSDKManager setLoglevel:BUAdSDKLogLevelDebug];

float scale = [[UIScreen mainScreen] scale];
CGRect frame=[UIScreen mainScreen].bounds;
BUSplashAdView *splashView = [[BUSplashAdView alloc] initWithSlotID:@"yyyyyyyyyy" frame:frame];
//yyyyyyyyyy是你在穿山甲後臺對應的廣告位Id
splashView.delegate = self;
UIWindow *keyWindow =[UIApplication sharedApplication].windows.firstObject;
[splashView loadAdData];
[keyWindow.rootViewController.view addSubview:splashView];
splashView.rootViewController = keyWindow.rootViewController;
  • 當開屏廣告被關閉時
- (void) splashAdDidClose:(BUSplashAdView *)splashAd{
    [splashAd removeFromSuperview];		
    [AppController SetOrientation:@""];	//強制豎屏轉橫屏
}

開屏廣告的問題

  • cocos creator遊戲是橫屏的,而穿山甲開屏廣告只有豎屏的
  • AppController 中加入【強制橫屏轉豎屏】及【強制豎屏轉橫屏】的方法
  • 方法如下:
UIInterfaceOrientationMask oMask = UIInterfaceOrientationMaskLandscape;

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    return oMask;
}

+(void)SetOrientation:(NSString*)dir{
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationUnknown] forKey:@"orientation"];
    
    if([dir isEqualToString:@"V"]){
        oMask = UIInterfaceOrientationMaskPortrait;
        [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];
    }
    else{
        oMask = UIInterfaceOrientationMaskLandscape;
        [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeLeft] forKey:@"orientation"];
    }
}

  • 在打開穿山甲開屏廣告前,調用【強制橫屏轉豎屏】方法:[AppController SetOrientation:@"V"];
  • 在穿山甲開屏廣告結束的回調中,【強制豎屏轉橫屏】調用:[AppController SetOrientation:@""];

激勵廣告

  • 根着SDK文檔照做(略)
  • 注: 以下示例我將類命名爲:CsjAdReward
  • CsjAdReward.h 部分代碼如下
@interface CsjAdReward  : UIViewController
+(CsjAdReward *) getAdInstance;			//用來獲取實例的方法
-(void) OpenAd;
@end
  • CsjAdReward.m 部分代碼如下
#import "CsjAdReward.h"
#import <BUAdSDK/BURewardedVideoModel.h>
#import <BUAdSDK/BUNativeExpressRewardedVideoAd.h>
#import "AppController.h"
#import <BUAdSDK/BUAdSDKManager.h>
#import "RootViewController.h"
@interface CsjAdReward ()<BUNativeExpressRewardedVideoAdDelegate>
@property (nonatomic,strong) BUNativeExpressRewardedVideoAd *rewardedVideoAd;
@end

static CsjAdReward *instance;	//這裏聲明一下靜態實例,方便javascript層調用
@implementation CsjAdReward

//獲取實例的方法,方便javascript層調用
+(CsjAdReward *) getAdInstance{
    return instance;
}


//初始化,根着SDK文檔照做就好了
- (void) viewDidLoad {
    NSLog(@"viewDidLoad");
    
    instance=self;
    
    [BUAdSDKManager setAppID:@"5043574"];
    [BUAdSDKManager setIsPaidApp:NO];
    [BUAdSDKManager setLoglevel:BUAdSDKLogLevelDebug];
    
    BURewardedVideoModel *model=[[BURewardedVideoModel alloc]init];
    model.userId=@"ming123";
    self.rewardedVideoAd=[[BUNativeExpressRewardedVideoAd alloc] initWithSlotID:@"945009897" rewardedVideoModel:model];
    self.rewardedVideoAd.delegate=self;
    [self.rewardedVideoAd loadAdData];
}

//打開激勵視頻的方法
-(void) OpenAd{
   if(self.rewardedVideoAd.isAdValid){
       [self.rewardedVideoAd showAdFromRootViewController:self];
    }
}

//視頻結束的回調
- (void)nativeExpressRewardedVideoAdDidClose:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{
    NSLog(@"rewardedVideoAdDidClose");
    [self.rewardedVideoAd loadAdData];		//視頻結束後,再加載一次廣告數據,保證廣告的不重複
}

  • AppController中初始化
  • didFinishLaunchingWithOptions方法中,在viewController初始化後,調用CsjAdReward的初始化,代碼如下:
UIViewController *_csjAdReward= [[CsjAdReward alloc] init];
[_viewController.view addSubview:_csjAdReward.view];		
  • Appcontroller中的靜態方法,用於javascript中調用並打開激勵視頻廣告
+ (void)CsjAdRewardOpen {
   NSLog(@"打開激勵視頻");
   [[CsjAdReward getAdInstance] OpenAd];
}
  • Cocos CreatorjavaScript中調用方法
public static CsjAdRewardOpen()
{
	if ('jsb' in window) {
		if (cc.sys.os == cc.sys.OS_IOS) {
			jsb.reflection.callStaticMethod("AppController", 'CsjAdRewardOpen');
			return;
		}
	}
}
	
發佈了14 篇原創文章 · 獲贊 1 · 訪問量 6227
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章