Cocos处理 保证 jsToOc 正确,并且确保前段回调接口正确性(能正常回调),然后 Build
window.OctoJs = function OctoJs() {
jsToOc.currentJS.ocCallBack('110');
};
window.onGameCallBack = function onGameCallBack(type1) {
jsToOc.currentJS.ocCallBack(type1);
}
window.OctoJs2 = function OctoJs2() {
TipManager.getInstance().ShowTip('暂无广告,请检查网络连接是否正常!');
jsToOc.currentJS.ocCallBack('112');
};
let jsToOc = {
currentJS: null,
mymimiBuy: function (type1,currentJS) {
jsToOc.currentJS = currentJS;
if (cc.sys.OS_ANDROID == cc.sys.os) {
console.log("current platform is: cc.sys.OS_ANDROID");
jsb.reflection.callStaticMethod("org/cocos2dx/mili/mymimi", "buy", "(Ljava/lang/String;)V", type1);
return true;
}
},
sendJSToOc(currentJS) {
jsToOc.currentJS = currentJS;
jsb.reflection.callStaticMethod("AppController", "CsjAdRewardOpen");
},
sendJSToOc2() {
jsb.reflection.callStaticMethod("AppController", "CsjAdRewardOpen2");
},
sendJSToOc3() {
jsb.reflection.callStaticMethod("AppController", "CsjAdRewardOpen3");
},
sendJSToOc4() {
jsb.reflection.callStaticMethod("AppController", "CsjAdRewardOpen4");
},
};
module.exports = jsToOc;
打开 Build 目录,在其中找到/build/jsb-link/frameworks/runtime-src/proj.ios_mac/ 项目名.xcodeproj 双击打开
XCODE 操作
尝试直接打包,保证打出来的 IPA 能正常运行.
配置证书 在 Cocos 图标的 Target 中选中 BuildSetting 栏,并输入 Sign 修改证书为开发证书 然后打开项目中的 Ios文件夹中的 AppController.mm 脚本 添加 Cocos 调用的接口 添加的代码:
+ (void)CsjAdRewardOpen {
NSLog(@"打开激励视频");
}
+ (void)CsjAdRewardOpen2 {
NSLog(@"打开全屏视频");
}
+ (void)CsjAdRewardOpen3 {
NSLog(@"打开插屏视频");
}
+ (void)CsjAdRewardOpen4 {
NSLog(@"打开banner视频");
}
然后构建IPA,看是否正常运行。
注意:如果这里构建出的 IPA 都有问题,那就先解决存在的问题,确保无问题后,再进行下一步。
接广告
1-下载穿山甲 SDK; 2-导入穿山甲 SDK; 这里如果没文字描述的话,请参照穿 穿山甲官方文档
穿山甲配置
1-将穿山甲的 SKAdNetwork ID 添加到 info.plist 中,以保证SKAdNetwork的正确运行 添加以下代码
<key>SKAdNetworkItems</key>
<array>
<dict>
<key>SKAdNetworkIdentifier</key>
<string>238da6jt44.skadnetwork</string>
</dict>
<dict>
<key>SKAdNetworkIdentifier</key>
<string>x2jnk7ly8j.skadnetwork</string>
</dict>
<dict>
<key>SKAdNetworkIdentifier</key>
<string>22mmun2rn5.skadnetwork</string>
</dict>
</array>
<key>NSUserTrackingUsageDescription</key>
<string>该标识符将用于向您投放个性化广告</string>
Build Settings中Other Linker Flags 增加参数-ObjC、-l"c++"、 -l"c++abi" 、-l"sqlite3"、-l"z",SDK同时支持-all_load具体操作如图 参数列表 -ObjC -l"c++" -l"c++abi" -l"sqlite3" -l"z" -all_load ? 工程需要在TARGETS -> Build Phases中找到Link Binary With Libraries,点击“+”,依次添加下列依赖库
添加依赖库
o Accelerate.framework o AdSupport.framework o AudioToolbox.framework o AVFoundation.framework o CoreGraphics.framework o CoreImage.framework o CoreLocation.framework o CoreMedia.framework o CoreMotion.framework o CoreTelephony.framework o CoreText.framework o ImageIO.framework o JavaScriptCore.framework o MapKit.framework o MediaPlayer.framework o MobileCoreServices.framework o QuartzCore.framework o Security.framework o StoreKit.framework o SystemConfiguration.framework o UIKit.framework o WebKit.framework o libbz2.tbd o libc++.tbd o libiconv.tbd o libresolv.9.tbd o libsqlite3.tbd o libxml2.tbd o libz.tbd o libc++abi.tbd o AppTrackingTransparency.framework iOS本地化\国际化配置 注意 : 开发者必须在这里设置所支持的语言,否则会有语言显示的问题.例如 : 支持中文 添加 Chinese 加入穿山甲代码 1-从其他项目中,拿到 CsjAdReward.h 和 CsjAdReward.mm 2 个脚本 拖进当前项目中(或者直接创建也可以),具体代码如下:
@interface CsjAdReward : UIViewController
+(CsjAdReward *) getAdInstance;
-(void) OpenAd;
-(void) OpenAd2;
-(void) OpenAd3;
-(void) OpenAd4;
@end
#import "CsjAdReward.h"
#import "cocos2d.h"
#import <BUAdSDK/BURewardedVideoModel.h>
#import <BUAdSDK/BUNativeExpressRewardedVideoAd.h>
#import <BUAdSDK/BUNativeExpressFullscreenVideoAd.h>
#import <BUAdSDK/BUNativeExpressInterstitialAd.h>
#import <BUAdSDK/BUNativeExpressBannerView.h>
#import <BUAdSDK/BUNativeAd.h>
#import <BUAdSDK/BUAdSlot.h>
#import <BUAdSDK/BUMaterialMeta.h>
#import <BUAdSDK/BUNativeAdRelatedView.h>
#import <BUAdSDK/BUMaterialMeta.h>
#import "AppController.h"
#import <BUAdSDK/BUAdSDKManager.h>
#import "RootViewController.h"
#include "platform/CCApplication.h"
#include "platform/ios/CCEAGLView-ios.h"
#include "cocos/scripting/js-bindings/jswrapper/SeApi.h"
@interface CsjAdReward ()<BUNativeExpressRewardedVideoAdDelegate>
@property (nonatomic,strong) BUNativeExpressRewardedVideoAd *rewardedVideoAd;
@end
@interface CsjAdReward ()<BUNativeExpressFullscreenVideoAdDelegate>
@property (nonatomic,strong) BUNativeExpressFullscreenVideoAd *fullscreenAd;
@end
@interface CsjAdReward ()<BUNativeExpresInterstitialAdDelegate>
@property (nonatomic,strong) BUNativeExpressInterstitialAd *interstitialAd;
@end
@interface CsjAdReward ()<BUNativeExpressBannerViewDelegate>
@property (nonatomic,strong) BUNativeExpressBannerView *bannerView;
@end
@interface CsjAdReward ()<BUNativeAdDelegate>
@property (nonatomic,strong) BUNativeAd *nativeAd;
@end
static CsjAdReward *instance;
@implementation CsjAdReward
BOOL isVideoLoaded = NO;
BOOL isFullscreenLoaded = NO;
BOOL IsLandscape = NO;
int adCOUNT = 0;
+(CsjAdReward *) getAdInstance{
return instance;
}
- (void)nativeExpressFullscreenVideoAd:(BUNativeExpressFullscreenVideoAd *)fullscreenAd didFailWithError:(NSError *_Nullable)error{
NSLog(@"aaa--error");
NSLog(@"%@", error);
}
- (void) viewDidLoad {
NSLog(@"viewDidLoad");
instance=self;
BURewardedVideoModel *model=[[BURewardedVideoModel alloc]init];
model.userId=@"123";
self.rewardedVideoAd=[[BUNativeExpressRewardedVideoAd alloc] initWithSlotID:@"******" rewardedVideoModel:model];
self.rewardedVideoAd.delegate=self;
[self.rewardedVideoAd loadAdData];
self.fullscreenAd = [[BUNativeExpressFullscreenVideoAd alloc] initWithSlotID:@"*******"];
self.fullscreenAd.delegate = self;
[self.fullscreenAd loadAdData];
}
-(void) OpenAd{
if (!isVideoLoaded||!self.rewardedVideoAd.isAdValid) {
[self.rewardedVideoAd loadAdData];
return;
}
NSLog(@"1111 value: %@" ,self.rewardedVideoAd.isAdValid?@"YES":@"NO");
[self.rewardedVideoAd showAdFromRootViewController:self];
}
- (void)nativeExpressRewardedVideoAdDidLoad:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{
isVideoLoaded = YES;
}
- (void)nativeExpressRewardedVideoAd:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd didFailWithError:(NSError *_Nullable)error{
std::string strRet = [@"" UTF8String];
std::string jsCallStr = cocos2d::StringUtils::format("OctoJs2();", strRet.c_str());
se::Value *ret = new se::Value();
se::ScriptEngine::getInstance()->evalString(jsCallStr.c_str(), -1, ret);
}
- (void)nativeExpressRewardedVideoAdDidClose:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{
NSLog(@"rewardedVideoAdDidClose");
std::string strRet = [@"" UTF8String];
std::string jsCallStr = cocos2d::StringUtils::format("OctoJs();", strRet.c_str());
se::Value *ret = new se::Value();
se::ScriptEngine::getInstance()->evalString(jsCallStr.c_str(), -1, ret);
[CsjAdReward SetOrientation];
[self.rewardedVideoAd loadAdData];
}
-(void) OpenAd2{
NSLog(@"2222 value: %@" ,self.fullscreenAd.isAdValid?@"YES":@"NO");
if (!isFullscreenLoaded||!self.fullscreenAd.isAdValid) {
[self.fullscreenAd loadAdData];
return;
}
[self.fullscreenAd showAdFromRootViewController:self];
}
- (void)nativeExpressFullscreenVideoAdDidLoad:(BUNativeExpressFullscreenVideoAd *)fullscreenAd{
isFullscreenLoaded = YES;
}
-(void) OpenAd3{
}
-(void) OpenAd4{
UIWindow *window = [[UIApplication sharedApplication].delegate window];
for (UIView *view in window.subviews) {
if ([view isKindOfClass:[UIImageView class]]) {
[view removeFromSuperview];
}
}
}
- (void)nativeExpressFullscreenVideoAdDidClose:(BUNativeExpressFullscreenVideoAd *)fullscreenAd{
NSLog(@"fullscreenVideoAdDidClose");
[CsjAdReward SetOrientation];
[self.fullscreenAd loadAdData];
}
+(void)SetOrientation{
if(IsLandscape==NO)return;
NSNumber *orientationTarget = [NSNumber numberWithInt:UIDeviceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}
@end
2-修改全屏和激励视频的参数 3-在 CsjReward.mm 中确认游戏横竖屏 4-接入开屏等穿山甲代码 导入穿山甲库
#import <BUAdSDK/BUAdSDKManager.h>
#import <BUAdSDK/BUSplashAdView.h>
#import "CsjAdReward.h"
#include "cocos/scripting/js-bindings/jswrapper/SeApi.h"
#import <StoreKit/StoreKit.h>
引用穿山甲开屏相关代码
@interface AppController () <BUSplashAdDelegate>
@end
BUSplashAdView *splashView;
在 didFinishLaunchingWithOptions 方法中加入以下代码,并修改参数
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[BUAdSDKManager setAppID:@"*******"];
[BUAdSDKManager setIsPaidApp:NO];
[BUAdSDKManager setLoglevel:BUAdSDKLogLevelDebug];
CGRect frame = [UIScreen mainScreen].bounds;
splashView = [[BUSplashAdView alloc] initWithSlotID:@"******" frame:frame];
splashView.delegate = self;
[splashView loadAdData];
UIViewController *_csjAdReward= [[CsjAdReward alloc] init];
[_viewController.view addSubview:_csjAdReward.view];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(statusBarOrientationChanged:)
name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
app->start();
return YES;
再添加并覆盖以下接口
- (void)splashAdDidLoad:(BUSplashAdView *)splashAd{
UIWindow *keyWindow = [UIApplication sharedApplication].windows.firstObject;
[keyWindow.rootViewController.view addSubview:splashView];
splashView.rootViewController = keyWindow.rootViewController;
}
- (void)splashAdDidClose:(BUSplashAdView *)splashAd {
[splashAd removeFromSuperview];
}
- (void)splashAd:(BUSplashAdView *)splashAd didFailWithError:(NSError *)error{
[splashAd removeFromSuperview];
app->start();
}
+ (void)CsjAdRewardOpen {
NSLog(@"打开激励视频");
[[CsjAdReward getAdInstance] OpenAd];
}
+ (void)CsjAdRewardOpen2 {
NSLog(@"打开全屏视频");
[[CsjAdReward getAdInstance] OpenAd2];
}
+ (void)CsjAdRewardOpen3 {
NSLog(@"打开插屏视频");
[[CsjAdReward getAdInstance] OpenAd3];
}
+ (void)CsjAdRewardOpen4 {
NSLog(@"打开banner视频");
[[CsjAdReward getAdInstance] OpenAd4];
}
此时可以尝试构建 IPA,正常来说这里就能正常显示一切广告了。
|