‹ InMobi Doc Home

InMobi 集成指南

For the C++ 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.plistLSApplicationQueriesSchemes 下添加名单。

关于 Creator 工程

Creator 导出工程会修改工程的配置, 这可能会影响 SDKBox 对工程的配置修改. 所以可以有以下做法(任选其一):

  1. 保存导出工程中的修改, 对于 SDKBox 来说可能有这些文件(mk, gradle, gradle.properties, AppDelegate.cpp, ...), 导出后再恢复对应该文件
  2. 在每一次 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, 不需要可以删除
}

使用

初始化 InMobi

#include "PluginInMobi/PluginInMobi.h"
AppDelegate::applicationDidFinishLaunching()
{
     sdkbox::PluginInMobi::init();
}

显示插屏广告

// 手动加载广告
sdkbox::PluginInMobi::loadInterstitial();

if (sdkbox::PluginInMobi::isInterstitialReady()) {
    CCLOG("Plugin InMobi interstitial ad is ready");
    sdkbox::PluginInMobi::showInterstitial();
} else {
    CCLOG("Plugin 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("900");
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("en-us");
sdkbox::PluginInMobi::setLocation("cd", "sc", "usa");
sdkbox::PluginInMobi::setLocation(102, 348);
sdkbox::PluginInMobi::setNationality("nationality");
sdkbox::PluginInMobi::setPostalCode("618000");

接收 InMobi 事件 (可选)

#include "PluginInMobi/PluginInMobi.h"
class MyClass : public sdkbox::InMobiListener {
public:
    void bannerDidFinishLoading() {
        CCLOG("bannerDidFinishLoading");
    };
    void bannerDidFailToLoadWithError(sdkbox::PluginInMobi::SBIMStatusCode code, const std::string& description) {
        CCLOG("bannerDidFailToLoadWithError status:%d, desc:%s", code, description.c_str());
    };

    void bannerDidInteractWithParams(const std::map<std::string, std::string>& params) {
        CCLOG("bannerDidInteractWithParams");
    };

    void userWillLeaveApplicationFromBanner() {
        CCLOG("userWillLeaveApplicationFromBanner");
    };

    void bannerWillPresentScreen() {
        CCLOG("bannerWillPresentScreen");
    };

    void bannerDidPresentScreen() {
        CCLOG("bannerDidPresentScreen");
    };

    void bannerWillDismissScreen() {
        CCLOG("bannerWillDismissScreen");
    };

    void bannerDidDismissScreen() {
        CCLOG("bannerDidDismissScreen");
    };

    void bannerRewardActionCompletedWithRewards(const std::map<std::string, std::string>& rewards) {
        CCLOG("bannerRewardActionCompletedWithRewards");
    };

    void interstitialDidFinishLoading() {
        CCLOG("interstitialDidFinishLoading");
    };

    void interstitialDidFailToLoadWithError(sdkbox::PluginInMobi::SBIMStatusCode code, const std::string& description) {
        CCLOG("interstitialDidFailToLoadWithError status:%d, desc:%s", code, description.c_str());
    };

    void interstitialWillPresent() {
        CCLOG("interstitialWillPresent");
    };

    void interstitialDidPresent() {
        CCLOG("interstitialDidPresent");
    };

    void interstitialDidFailToPresentWithError(sdkbox::PluginInMobi::SBIMStatusCode code, const std::string& description) {
        CCLOG("interstitialDidFailToPresentWithError");
    };

    void interstitialWillDismiss() {
        CCLOG("interstitialWillDismiss");
    };

    void interstitialDidDismiss() {
        CCLOG("interstitialDidDismiss");
    };

    void interstitialDidInteractWithParams(const std::map<std::string, std::string>& params) {
        CCLOG("interstitialDidInteractWithParams");
    };

    void interstitialRewardActionCompletedWithRewards(const std::map<std::string, std::string>& rewards) {
        CCLOG("interstitialRewardActionCompletedWithRewards");
    };

    void userWillLeaveApplicationFromInterstitial() {
        CCLOG("userWillLeaveApplicationFromInterstitial");
    };
};
sdkbox::PluginInMobi::setListener(this);

API Reference

Methods

static bool init ( ) ;

initialize the plugin instance.

static void setListener ( InMobiListener * listener ) ;

Set listener to listen for inmobi events

static InMobiListener * getListener ( ) ;

Get the listener

static void removeListener ( ) ;

Remove the listener, and can't listen to events anymore

static std::string getVersion ( ) ;

Use this to get the version of the SDK. @return The version of the SDK.

static void setLogLevel ( SBIMSDKLogLevel desiredLogLevel ) ;

Set the log level for SDK's logs @param desiredLogLevel The desired level of logs.

static void addIdForType ( const std::string & identifier ,
                           SBIMSDKIdType type ) ;

Register a user specific id with the SDK @param identifier The user Id. @param type The user Id type.

static void removeIdType ( SBIMSDKIdType type ) ;

Deregister a particular set of Ids @param type The user Id type.

static void setAge ( int age ) ;

Provide the user's age to the SDK for targetting purposes. @param age The user's age.

static void setAreaCode ( const std::string & areaCode ) ;

Provide the user's area code to the SDK for targetting purposes. @param areaCode The user's area code.

static void setAgeGroup ( SBIMSDKAgeGroup ageGroup ) ;

Provide the user's age group to the SDK for targetting purposes. @param ageGroup The user's age group.

static void setYearOfBirth ( int yearOfBirth ) ;

Provide a user's date of birth to the SDK for targetting purposes. @param dateOfBirth The user's date of birth.

static void setEducation ( SBIMSDKEducation education ) ;

Provide the user's education status to the SDK for targetting purposes. @param education The user's education status.

static void setEthnicity ( SBIMSDKEthnicity ethnicity ) ;

Provide the user's ethnicity to the SDK for targetting purposes. @param ethnicity The user's ethnicity.

static void setGender ( SBIMSDKGender gender ) ;

Provide the user's gender to the SDK for targetting purposes. @param gender The user's gender.

static void setHouseholdIncome ( SBIMSDKHouseholdIncome income ) ;

Provide the user's household income to the SDK for targetting purposes. @param income The user's household income.

static void setIncome ( unsigned int income ) ;

Provide the user's income to the SDK for targetting purposes. @param income The user's income.

static void setInterests ( const std::string & interests ) ;

Provide the user's interests to the SDK for targetting purposes. @param interests The user's interests.

static void setLanguage ( const std::string & language ) ;

Provide the user's preferred language to the SDK for targetting purposes. @param language The user's language.

static void setLocation ( const std::string & city ,
                          const std::string & state ,
                          const std::string & 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.

static void setLocation ( double latitude , double longitude ) ;

Provide the user's location to the SDK for targetting purposes. @param location: The location of the user

static void setNationality ( const std::string & nationality ) ;

Provide the user's nationality to the SDK for targetting purposes. @param nationality The user's nationality.

static void setPostalCode ( const std::string & postalcode ) ;

Provide the user's postal code to the SDK for targetting purposes. @param postalcode The user's postalcode.

static void shouldAutoRefresh ( bool refresh ) ;

Control if the banner should auto-refresh ad content.

static void setRefreshInterval ( int interval ) ;

Specify the refresh interval for the banner ad.

static void loadBanner ( ) ;

Submit a request to load banner ad content.

static void disableHardwareAccelerationForBanner ( ) ;

Turn off hardware acceleration on the underlying views. vaild on android

static void setBannerAnimationType ( SBIMBannerAnimationType animationType ) ;

Set the animation preference on the banner views during ad refresh.

static void setBannerExtras ( const std::map <std::string ,
                              std::string> & extras ) ;

Set any additional custom parameters that will be sent in the ad request.

static void setBannerKeywords ( const std::string & keywords ) ;

Set comma delimited keywords for targeting purpose

static void loadInterstitial ( ) ;

Submit a request to load interstitial ad content.

static bool isInterstitialReady ( ) ;

Returns true if the interstitial was loaded successfully and in ready to be shown.

static void showInterstitial ( ) ;

Displays the interstitial on the screen

static void showInterstitial ( SBIMInterstitialAnimationType type ) ;

Displays the interstitial on the screen valid on ios

static void showInterstitial ( int enterAnimationResourcedId ,
                               int exitAnimationResourceId ) ;

Displays the interstitial on the screen valid on android

static void disableHardwareAccelerationForInterstitial ( ) ;

Disable hardware acceleration on the underlying views. valid on android

static void setInterstitialExtras ( const std::map <std::string ,
                                    std::string> & extras ) ;

Set any additional custom parameters that will be sent in the ad request.

static void setInterstitialKeywords ( const std::string & keywords ) ;

Set comma delimited keywords for targeting purpose

Listeners

void bannerDidFinishLoading ( ) {

Notifies the delegate that the banner has finished loading

void bannerDidFailToLoadWithError ( PluginInMobi::SBIMStatusCode code ,
                                    const std::string & description ) {

Notifies the delegate that the banner has failed to load with some error.

void bannerDidInteractWithParams ( const std::map <std::string ,
                                   std::string> & params ) {

Notifies the delegate that the banner was interacted with.

void userWillLeaveApplicationFromBanner ( ) {

Notifies the delegate that the user would be taken out of the application context.

void bannerWillPresentScreen ( ) {

Notifies the delegate that the banner would be presenting a full screen content.

void bannerDidPresentScreen ( ) {

Notifies the delegate that the banner has finished presenting screen.

void bannerWillDismissScreen ( ) {

Notifies the delegate that the banner will start dismissing the presented screen.

void bannerDidDismissScreen ( ) {

Notifies the delegate that the banner has dismissed the presented screen.

void bannerRewardActionCompletedWithRewards ( const std::map <std::string ,
                                              std::string> & rewards );

Notifies the delegate that the user has completed the action to be incentivised with.

void interstitialDidFinishLoading ( ) {

Notifies the delegate that the interstitial has finished loading

void interstitialDidFailToLoadWithError ( PluginInMobi::SBIMStatusCode code ,
                                          const std::string & description ) {

Notifies the delegate that the interstitial has failed to load with some error.

void interstitialWillPresent ( ) {

Notifies the delegate that the interstitial would be presented.

void interstitialDidPresent ( ) {

Notifies the delegate that the interstitial has been presented.

void interstitialDidFailToPresentWithError ( PluginInMobi::SBIMStatusCode code ,
                                             const std::string & description ) {

Notifies the delegate that the interstitial has failed to present with some error.

void interstitialWillDismiss ( ) {

Notifies the delegate that the interstitial will be dismissed.

void interstitialDidDismiss ( ) {

Notifies the delegate that the interstitial has been dismissed.

void interstitialDidInteractWithParams ( const std::map <std::string ,
                                         std::string> & params ) {

Notifies the delegate that the interstitial has been interacted with.

void interstitialRewardActionCompletedWithRewards ( const std::map <std::string ,
                                                    std::string> & rewards );

Notifies the delegate that the user has performed the action to be incentivised with.

void 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

Android 手动集成

SDKBox 支持三种 Android 工程, command-line, eclipseAndroid Studio.

Android 平台手动集成

拷贝文件

从插件安装包中的 plugin/android/libs 目录拷贝下列 jar 文件到您的工程的 proj.android/libs 目录。

InMobiLib.jar

PluginInMobi.jar

sdkbox.jar

拷贝 jni 库

plugin/android/jni/ 拷贝并覆盖 <your_project_root>/jni/ 目录.

编辑 AndroidManifest.xml

<!--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"/>
<!--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

修改 AppActivity.java

插件版本 >= 2.4.0.3

  1. 找到 AppActivity.java 文件
find . -name "AppActivity.java"
  1. 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

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
import android.content.Intent;
import com.sdkbox.plugin.SDKBox;
onLoadNativeLibraries();
SDKBox.init(this);
    @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)

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 模式下,不会调到混淆规则.