InMobi 集成指南
For the Javascript version of cocos2d-x v3.x - (all other versions)
集成
用如下命令来集成 SDKBOX InMobi 插件,请确保您可以正常执行的 SDKBOX 安装器.
$ sdkbox import inmobi
重点注意事项
如果您升级到了 Xcode7, 则需要以下额外步骤来确保插件工作正常:
禁用应用程序安全传输策略
添加以下项到 plist:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
添加后的文件内容看起来就像这样:
禁止 Bitcode 支持
您必须禁止 Bitcode 的支持,否则将会编译失败。
游戏全屏配置
如果您的游戏不同时支持横竖屏,则必须在 Xcode 中选中 Requires full screen
,否则将不会通过 Apple 的审核。
canOpenURL 白名单
取决于您使用哪些插件。需要在 info.plist
的 LSApplicationQueriesSchemes
下添加名单。
关于 Creator 工程
Creator 导出工程会修改工程的配置, 这可能会影响 SDKBox 对工程的配置修改. 所以可以有以下做法(任选其一):
- 保存导出工程中的修改, 对于 SDKBox 来说可能有这些文件(mk, gradle, gradle.properties, AppDelegate.cpp, ...), 导出后再恢复对应该文件
- 在每一次 Creator 导出工程后, 重新 import 插件.
推荐第一种, 但是第一种的麻烦点在于, 对于第一次(或很久没接触的人)可能会有露掉某个文件 第二种, 很方便, 它的麻烦点在于, 如果你对工程有自己的修改, SDKBox 的重新 import 不能帮你恢复.
JSON 配置
SDKBOX 安装器会为您自动生成一个 sdkbox_config.json
配置文件, 请先修改再使用.
下面给出一个例子, 您必须替换掉 <account_id>
, 这个账号从 InMobi
获取.
"InMobi":{
"interstitial_placement_id": "1449919424310", // 插屏广告的 id
"account_id": "922cc696d9fa475097651b5cad78567d", // 从官网获取
"banner_h": 50, // 横幅广告得高度
"banner_placement_id": "1447081423897" // 横幅广告的 id, 不需要可以删除
}
使用
注册 Javascript 函数
- 修改
./frameworks/runtime-src/Classes/AppDelegate.cpp
包含如下头文件:
#include "PluginInMobiJS.hpp"
#include "PluginInMobiJSHelper.h"
- 修改
./frameworks/runtime-src/Classes/AppDelegate.cpp
确保有如下调用:
sc->addRegisterCallback(register_all_PluginInMobiJS);
sc->addRegisterCallback(register_all_PluginInMobiJS_helper);
初始化 InMobi
- 在您的代码中合适的地方调用
init()
, 我们建议在app.js
初始化,比如:
sdkbox.PluginInMobi.init();
显示插屏广告
// 手动加载广告
sdkbox.PluginInMobi.loadInterstitial();
// 显示插屏广告
if (sdkbox.PluginInMobi.isInterstitialReady()) {
console.log('inmobi interstitial ad is ready');
sdkbox.PluginInMobi.showInterstitial();
} else {
console.log('inmobi interstitial ad is not ready');
}
设置日志等级
sdkbox.PluginInMobi.setLogLevel(sdkbox.PluginInMobi.SBIMSDKLogLevel.kIMSDKLogLevelDebug);
设置用户数据
sdkbox.PluginInMobi.addIdForType("test", sdkbox.PluginInMobi.SBIMSDKIdType.kIMSDKIdTypeLogin);
sdkbox.PluginInMobi.removeIdType(sdkbox.PluginInMobi.SBIMSDKIdType.kIMSDKIdTypeLogin);
sdkbox.PluginInMobi.setAge(18);
sdkbox.PluginInMobi.setAreaCode("area code");
sdkbox.PluginInMobi.setAgeGroup(sdkbox.PluginInMobi.SBIMSDKAgeGroup.kIMSDKAgeGroupBetween18And20);
sdkbox.PluginInMobi.setYearOfBirth(1989);
sdkbox.PluginInMobi.setEducation(sdkbox.PluginInMobi.SBIMSDKEducation.kIMSDKEducationHighSchoolOrLess);
sdkbox.PluginInMobi.setEthnicity(sdkbox.PluginInMobi.SBIMSDKEthnicity.kIMSDKEthnicityHispanic);
sdkbox.PluginInMobi.setGender(sdkbox.PluginInMobi.SBIMSDKGender.kIMSDKGenderMale);
sdkbox.PluginInMobi.setHouseholdIncome(sdkbox.PluginInMobi.SBIMSDKHouseholdIncome.kIMSDKHouseholdIncomeBelow5kUSD);
sdkbox.PluginInMobi.setIncome(4500);
sdkbox.PluginInMobi.setInterests("game");
sdkbox.PluginInMobi.setLanguage("zh-cn");
sdkbox.PluginInMobi.setLocation("cd", "sc", "usa");
sdkbox.PluginInMobi.setLocation(102, 348);
sdkbox.PluginInMobi.setNationality("nationality");
sdkbox.PluginInMobi.setPostalCode("618000");
接收 InMobi 事件 (可选)
var plugin = sdkbox.PluginInMobi
plugin.setListener({
bannerDidFinishLoading: function() { console.log('bannerDidFinishLoading'); },
bannerDidFailToLoadWithError: function(code, description) { console.log('bannerDidFailToLoadWithError code:' + code + ' desc:' + description); },
bannerDidInteractWithParams: function(params) { console.log('bannerDidInteractWithParams'); },
userWillLeaveApplicationFromBanner: function() { console.log('userWillLeaveApplicationFromBanner'); },
bannerWillPresentScreen: function() { console.log('bannerWillPresentScreen'); },
bannerDidPresentScreen: function() { console.log('bannerDidPresentScreen'); },
bannerWillDismissScreen: function() { console.log('bannerWillDismissScreen'); },
bannerDidDismissScreen: function() { console.log('bannerDidDismissScreen'); },
bannerRewardActionCompletedWithRewards: function(rewards) { console.log('bannerRewardActionCompletedWithRewards'); },
interstitialDidFinishLoading: function() { console.log('interstitialDidFinishLoading'); },
interstitialDidFailToLoadWithError: function(code, description) { console.log('interstitialDidFailToLoadWithError code:' + code + ' desc:' + description); },
interstitialWillPresent: function() { console.log('interstitialWillPresent'); },
interstitialDidPresent: function() { console.log('interstitialDidPresent'); },
interstitialDidFailToPresentWithError: function(code, description) { console.log('interstitialDidFailToPresentWithError code:' + code + ' desc:' + description); },
interstitialWillDismiss: function() { console.log('interstitialWillDismiss'); },
interstitialDidDismiss: function() { console.log('interstitialDidDismiss'); },
interstitialDidInteractWithParams: function(params) { console.log('interstitialDidInteractWithParams'); },
interstitialRewardActionCompletedWithRewards: function(rewards) { console.log('interstitialRewardActionCompletedWithRewards'); },
userWillLeaveApplicationFromInterstitial: function() { console.log('userWillLeaveApplicationFromInterstitial'); }
})
plugin.init();
API Reference
Methods
sdkbox.PluginInMobi.init();
initialize the plugin instance.
sdkbox.PluginInMobi.setListener(listener);
Set listener to listen for inmobi events
sdkbox.PluginInMobi.getVersion();
Use this to get the version of the SDK. @return The version of the SDK.
sdkbox.PluginInMobi.setLogLevel(desiredLogLevel);
Set the log level for SDK's logs @param desiredLogLevel The desired level of logs.
sdkbox.PluginInMobi.addIdForType(identifier, type);
Register a user specific id with the SDK @param identifier The user Id. @param type The user Id type.
sdkbox.PluginInMobi.removeIdType(type);
Deregister a particular set of Ids @param type The user Id type.
sdkbox.PluginInMobi.setAge(age);
Provide the user's age to the SDK for targetting purposes. @param age The user's age.
sdkbox.PluginInMobi.setAreaCode(areaCode);
Provide the user's area code to the SDK for targetting purposes. @param areaCode The user's area code.
sdkbox.PluginInMobi.setAgeGroup(ageGroup);
Provide the user's age group to the SDK for targetting purposes. @param ageGroup The user's age group.
sdkbox.PluginInMobi.setYearOfBirth(yearOfBirth);
Provide a user's date of birth to the SDK for targetting purposes. @param dateOfBirth The user's date of birth.
sdkbox.PluginInMobi.setEducation(education);
Provide the user's education status to the SDK for targetting purposes. @param education The user's education status.
sdkbox.PluginInMobi.setEthnicity(ethnicity);
Provide the user's ethnicity to the SDK for targetting purposes. @param ethnicity The user's ethnicity.
sdkbox.PluginInMobi.setGender(gender);
Provide the user's gender to the SDK for targetting purposes. @param gender The user's gender.
sdkbox.PluginInMobi.setHouseholdIncome(income);
Provide the user's household income to the SDK for targetting purposes. @param income The user's household income.
sdkbox.PluginInMobi.setIncome(income);
Provide the user's income to the SDK for targetting purposes. @param income The user's income.
sdkbox.PluginInMobi.setInterests(interests);
Provide the user's interests to the SDK for targetting purposes. @param interests The user's interests.
sdkbox.PluginInMobi.setLanguage(language);
Provide the user's preferred language to the SDK for targetting purposes. @param language The user's language.
sdkbox.PluginInMobi.setLocation(city, state, country);
Provide the user's location to the SDK for targetting purposes. @param city The user's city. @param state The user's state. @param country The user's country.
sdkbox.PluginInMobi.setLocation(latitude, longitude);
Provide the user's location to the SDK for targetting purposes. @param location: The location of the user
sdkbox.PluginInMobi.setNationality(nationality);
Provide the user's nationality to the SDK for targetting purposes. @param nationality The user's nationality.
sdkbox.PluginInMobi.setPostalCode(postalcode);
Provide the user's postal code to the SDK for targetting purposes. @param postalcode The user's postalcode.
sdkbox.PluginInMobi.shouldAutoRefresh(refresh);
Control if the banner should auto-refresh ad content.
sdkbox.PluginInMobi.setRefreshInterval(interval);
Specify the refresh interval for the banner ad.
sdkbox.PluginInMobi.loadBanner();
Submit a request to load banner ad content.
sdkbox.PluginInMobi.disableHardwareAccelerationForBanner();
Turn off hardware acceleration on the underlying views. vaild on android
sdkbox.PluginInMobi.setBannerAnimationType(animationType);
Set the animation preference on the banner views during ad refresh.
sdkbox.PluginInMobi.setBannerKeywords(keywords);
Set comma delimited keywords for targeting purpose
sdkbox.PluginInMobi.loadInterstitial();
Submit a request to load interstitial ad content.
sdkbox.PluginInMobi.isInterstitialReady();
Returns true if the interstitial was loaded successfully and in ready to be shown.
sdkbox.PluginInMobi.showInterstitial();
Displays the interstitial on the screen
sdkbox.PluginInMobi.showInterstitial(type);
Displays the interstitial on the screen valid on ios
sdkbox.PluginInMobi.showInterstitial(enterAnimationResourcedId,
exitAnimationResourceId);
Displays the interstitial on the screen valid on android
sdkbox.PluginInMobi.disableHardwareAccelerationForInterstitial();
Disable hardware acceleration on the underlying views. valid on android
sdkbox.PluginInMobi.setInterstitialKeywords(keywords);
Set comma delimited keywords for targeting purpose
Listeners
bannerDidFinishLoading();
Notifies the delegate that the banner has finished loading
bannerDidFailToLoadWithError(code, description);
Notifies the delegate that the banner has failed to load with some error.
bannerDidInteractWithParams(params);
Notifies the delegate that the banner was interacted with.
userWillLeaveApplicationFromBanner();
Notifies the delegate that the user would be taken out of the application context.
bannerWillPresentScreen();
Notifies the delegate that the banner would be presenting a full screen content.
bannerDidPresentScreen();
Notifies the delegate that the banner has finished presenting screen.
bannerWillDismissScreen();
Notifies the delegate that the banner will start dismissing the presented screen.
bannerDidDismissScreen();
Notifies the delegate that the banner has dismissed the presented screen.
bannerRewardActionCompletedWithRewards(rewards);
Notifies the delegate that the user has completed the action to be incentivised with.
interstitialDidFinishLoading();
Notifies the delegate that the interstitial has finished loading
interstitialDidFailToLoadWithError(code, description);
Notifies the delegate that the interstitial has failed to load with some error.
interstitialWillPresent();
Notifies the delegate that the interstitial would be presented.
interstitialDidPresent();
Notifies the delegate that the interstitial has been presented.
interstitialDidFailToPresentWithError(code, description);
Notifies the delegate that the interstitial has failed to present with some error.
interstitialWillDismiss();
Notifies the delegate that the interstitial will be dismissed.
interstitialDidDismiss();
Notifies the delegate that the interstitial has been dismissed.
interstitialDidInteractWithParams(params);
Notifies the delegate that the interstitial has been interacted with.
interstitialRewardActionCompletedWithRewards(rewards);
Notifies the delegate that the user has performed the action to be incentivised with.
userWillLeaveApplicationFromInterstitial();
Notifies the delegate that the user will leave application context.
手动集成
如果 SDKBOX 安装器 安装插件失败了,那么需要手动集成插件.如果安装器安装插件成功了,那么不需要,也没必要,按文档再手动集成一次.
下面列出的的步骤一般很少用到.如果你按下面的步骤完成了集成,请在完成集成后,再按步骤检查一次.
iOS 平台手动集成
拖拽下列 framework 从 InMobi
插件包的 plugins/ios 目录到您的 Xcode 工程中,在添加 frameworks 的时候,请勾选 Copy items if needed
:
sdkbox.framework
PluginInMobi.framework
上面的 frameworks 依赖于其他 frameworks。如果您没有添加它们,您也需要添加下列这些 frameworks:
AdSupport.framework
AudioToolbox.framework
AVFoundation.framework
CoreLocation.framework
CoreTelephony.framework
EventKit.framework
EventKitUI.framework
MediaPlayer.framework
MessageUI.framework
Security.framework
Social.framework
StoreKit.framework
SystemConfiguration.framework
UIKit.framework
SafariServices.framework
GameController.framework
libsqlite3.0.tbd
libc++.tbd
把 plugin/jsbindings
文件夹中所有的头文件和源文件都拷贝到你的工程的 Classes
文件夹中.
把刚刚拷贝的文件拖动到 Xcode 中或使用 File -> Add files to... 来添加.
Android 手动集成
SDKBox 支持三种 Android 工程, command-line, eclipse 和 Android Studio.
- command-line 和 eclipse 类型的项目,
<project_root>
指代proj.android
. - Android Studio 类型的项目,
<project_root>
指代proj.android-studio
.
Android 平台手动集成
拷贝文件
从插件安装包中的 plugin/android/libs
目录拷贝下列 jar 文件到您的工程的 proj.android/libs 目录。
InMobiLib.jar
PluginInMobi.jar
sdkbox.jar
-
如果你使用 cocos2d-x 源码,拷贝 jar 文件到:
Android command-line:
cocos2d/cocos/platform/android/java/libs
Android Studio:
cocos2d/cocos/platform/android/libcocos2dx/libs
-
如果你使用 cocos2d-js 或者 lua ,拷贝 jar 文件到:
Android command-line:
frameworks/cocos2d-x/cocos/platform/android/java/libs
Android Studio:
frameworks/cocos2d-x/cocos/platform/android/libcocos2dx/libs
-
如果你使用 cocos2d-x 预编译包,拷贝 jar 文件到:
Android command-line:
proj.android/libs
拷贝 jni 库
从 plugin/android/jni/
拷贝并覆盖 <your_project_root>/jni/
目录.
编辑 AndroidManifest.xml
- 在标签 application tag 上添加下列权限:
<!--Mandatory permissions to receive ads-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!--Recommended permissions to receive brand‐centric ads with interactive functionality for better eCPMs-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/>
<uses-permission android:name="android.permission.READ_CALENDAR"/>
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>
<uses-permission android:name="android.permission.GET_TASKS"/>
- 在标签 application tag 上添加元数据:
<!--Required Activity for rendering ads in the embedded browser-->
<activity android:name="com.inmobi.rendering.InMobiAdActivity"
android:configChanges="keyboardHidden|orientation|keyboard|smallestScreenSize|screenSize"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:hardwareAccelerated="true" />
<!--Required Receiver for enhanced targeting for better ads.-->
<receiver android:name="com.inmobi.commons.core.utilities.uid.ImIdShareBroadCastReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="com.inmobi.share.id" />
</intent-filter>
</receiver>
<service android:enabled="true" android:name="com.inmobi.signals.activityrecognition.ActivityRecognitionManager" />
<!--Required for Google Play Services-->
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
编辑 Android.mk
编辑 <project_root>/jni/Android.mk
:
为 LOCAL_STATIC_LIBRARIES 添加额外的库:
LOCAL_WHOLE_STATIC_LIBRARIES += PluginInMobi
LOCAL_WHOLE_STATIC_LIBRARIES += sdkbox
在所有 import-module 语句之前添加一条 call 语句:
$(call import-add-path,$(LOCAL_PATH))
在最后添加额外的 import-module 语句:
$(call import-module, ./sdkbox)
$(call import-module, ./plugininmobi)
这意味着您的语句顺序看起来像是这样:
$(call import-add-path,$(LOCAL_PATH))
$(call import-module, ./sdkbox)
$(call import-module, ./plugininmobi)
Note: 如果您使用的是 cocos2d-x 预编译库,那么保证这些语句在已有的 $(call import-module,./prebuilt-mk)
语句之上非常重要。
编辑 Application.mk
(只限 Cocos2d-x v3.0 到 v3.2 版本)
编辑 proj.android/jni/Application.mk
保证 APP_STL 的定义正确。如果 Application.mk
包含了 APP_STL := c++_static
语句,那么这条语句应该被改为:
APP_STL := gnustl_static
把 plugin/jsbindings
文件夹中所有的头文件和源文件都拷贝到你的工程的 Classes
文件夹中.
把你刚刚拷贝的 .cpp
文件添加到 Android.mk
文件的的 LOCAL_SRC_FILES 项.比如
LOCAL_SRC_FILES := hellocpp/main.cpp \
../../Classes/AppDelegate.cpp \
../../Classes/HelloWorldScene.cpp \
../../Classes/NewSourceFile.cpp
修改 AppActivity.java
插件版本 >= 2.4.0.3
- 找到 AppActivity.java 文件
find . -name "AppActivity.java"
- 把
extends Cocos2dxActivity
替换为extends com.sdkbox.plugin.SDKBoxActivity
以下是 AppActivity.java 不同版本的引擎所在的目录:
cpp
- proj.android/src/org/cocos2dx/cpp/AppActivity.java
- proj.android-studio/app/src/org/cocos2dx/cpp/AppActivity.java
- proj.android/app/src/org/cocos2dx/cpp/AppActivity.java ( from cocos2d-x 3.17)
lua
- frameworks/runtime-src/proj.android/src/org/cocos2dx/lua/AppActivity.java
- frameworks/runtime-src/proj.android-studio/app/src/org/cocos2dx/lua/AppActivity.java
- frameworks/runtime-src/proj.android/app/src/org/cocos2dx/lua/AppActivity.java (from cocos2d-x 3.17)
js
- frameworks/runtime-src/proj.android/src/org/cocos2dx/javascript/AppActivity.java
- frameworks/runtime-src/proj.android/app/src/org/cocos2dx/javascript/AppActivity.java ( from cocos2d-x 3.17)
插件版本 < 2.4.0.3
-
如果您使用 cocos2d-x 源代码,假设您在
proj.android
目录下,那么您可以在如下位置找到Cocos2dxActivity.java
文件:../../cocos2d-x/cocos/platform/android/java/src/org/cocos2dx/ lib/Cocos2dxActivity.java
-
如果您使用 cocos2dx-x 预编译库, 假设您在
proj.android
目录下,那么您可以在如下位置找到Cocos2dxActivity.java
文件:./src/org/cocos2dx/lib/Cocos2dxActivity.java
Note: 当你使用 cocos2d-x 源代码时,不同的版本中 Cocos2dxActivity.java
文件的位置也不同。一个确定该文件位置的方法是查看 proj.android/project.properties
。比如:
android.library.reference.1=../../cocos2d-x/cocos/platform/android/java
在这个例子中, Cocos2dxActivity.java
文件应该在如下位置:
../../cocos2d-x/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java
- 修改
Cocos2dxActivity.java
文件,导入如下包:
import android.content.Intent;
import com.sdkbox.plugin.SDKBox;
- 然后,修改
Cocos2dxActivity
类的onCreate(final Bundle savedInstanceState)
函数,添加一个调用语句SDKBox.init(this);
。添加的位置非常重要,必须在调用onLoadNativeLibraries();
之后。如下:
onLoadNativeLibraries();
SDKBox.init(this);
-
最后, 我需要提供合适的 overrides 方法的代码。这里有一些约定如下。
-
如果这个被列出的方法没有在
SDKBox
中定义,那么__定义它__。 -
如果这个被列出的方法已经被定义在
SDKBox
中,那么请调用这个在SDKBox
中的__同名方法__。
-
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(!SDKBox.onActivityResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
}
@Override
protected void onStart() {
super.onStart();
SDKBox.onStart();
}
@Override
protected void onStop() {
super.onStop();
SDKBox.onStop();
}
@Override
protected void onResume() {
super.onResume();
SDKBox.onResume();
}
@Override
protected void onPause() {
super.onPause();
SDKBox.onPause();
}
@Override
public void onBackPressed() {
if(!SDKBox.onBackPressed()) {
super.onBackPressed();
}
}
Proguard (release, optional)
- 修改
project.properties
加入您的Proguard
配置文件. 比如:
proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
- 修改这个您的配置文件,并在其中加下以下代码:
# inmobi
-keep class com.inmobi.** { *; }
-dontwarn com.inmobi.**
# cocos2d-x
-keep public class org.cocos2dx.** { *; }
-dontwarn org.cocos2dx.**
-keep public class com.chukong.** { *; }
-dontwarn com.chukong.**
# google play service
-keep public class com.google.android.gms.** { public *; }
-dontwarn com.google.android.gms.**
-keep class * extends java.util.ListResourceBundle {
protected Object[][] getContents();
}
-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
public static final *** NULL;
}
-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
@com.google.android.gms.common.annotation.KeepName *;
}
-keepnames class * implements android.os.Parcelable {
public static final ** CREATOR;
}
#sdkbox
-keep public class com.sdkbox.** { *; }
-dontwarn com.sdkbox.**
注意: 混淆只在 Release 模式下有效 (比如 cocos run -m release
), 在 debug 模式下,不会调到混淆规则.