cocos2dx之友盟统计(android/ios)

一、玩家登录,在lua代码中获取玩家的帐号信息

---------账号登录统计--------------- 
if device.platform == "android" then
    local args = { 1,tostring(PlayerId),1 }
    local luaj = require "cocos.cocos2d.luaj"
    local sigs = "(ILjava/lang/String;I)I"
    local className = "com/cocos2dx/sample/LuaJavaBridge"
    local ok,ret  = luaj.callStaticMethod(className,"sendLuaToJavaProfileSignIn",args,sigs)
    if not ok then
        print("luaj error:",ret)
    else
        print("The ret is:",ret)
    end

elseif device.platform == "ios" then
    iosProfileSignInWithPUID(tostring(PlayerId))
    -- iosProfileSignInWithPUID("playerID_str_param")
end
---------账号登录统计--------------- 

lua中获取信息后,通过不同平台来调用不同平台的方法。
android中,lua(–>c/c++)–>LuaJavaBridge–>java(android)
ios中,lua(–>c/c++)–>oc(iOS)

android情况:项目文件下,src–>com.cocos2dx.sample–>LuaJavaBridge.java

// 账号登录统计
public static int sendLuaToJavaProfileSignIn(int type,String UserID,final int luaFuncID){
    //System.out.printf("%n","sendLuaToJavaProfileSignIn **********************************");

    //Log.d("tag","正sendLuaToJavaProfileSignIn播放中");
    MobclickAgent.onProfileSignIn(UserID);   //友盟账号登录统计的方法

    return 1;
}

调用了友盟统计登录的方法即可。
还需要的一些代码编写以及配置情况:在src–>org.cocos2dx.lua–>AppActivity.java

@Override
protected void onResume() {
    super.onResume(); 

    //添加友盟的部分
    MobclickAgent.onResume(this);
}
@Override
public void onPause(){
    super.onPause();

    //添加友盟的部分
    MobclickAgent.onPause(this);
}

在项目文件下,找到如下,在其中添加

<application>

    <!-- umeng Sdk start -->
    <meta-data android:value="584e516aa3251114e10012a3" android:name="UMENG_APPKEY"/>
    <meta-data android:value="umeng" android:name="UMENG_CHANNEL"/>
    <!-- umeng Sdk end -->
</application>

“584e516aa3251114e10012a3”:在友盟后台申请的应用Appkey(根据自己申请的来填写)。

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

添加权限。

iOS情况:AppDelegate.h中,声明函数

//友盟统计登录
static int iosProfileSignInWithPUID(lua_State *L);

AppDelegate.cpp中,applicationDidFinishLaunching里面,lua注册函数

lua_register(L,"iosProfileSignInWithPUID",iosProfileSignInWithPUID);

以及实现函数

// lua 调用 c 友盟账号统计
int AppDelegate::iosProfileSignInWithPUID(lua_State *L)
{
    if(CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    {
        //从lua处得到账号信息,lua里面会调用已经注册的该函数iosProfileSignInWithPUID
        std::string playerId = lua_tostring(L,1);
        printf("玩家的playerID: %s\n",playerId.c_str());
        //新建一个接口,这个接口能被c/c++函数iosProfileSignInWithPUID调用,接口中又可以调用oc代码,lua代码又可以调用该函数iosProfileSignInWithPUID
        UMProfile::profileSignInWithPUID(playerId.c_str());
    }
    return 1;
}

创建该接口
新建一个类UMProfile,有UMProfile.h文件和UMProfile.mm文件.h文件中声明函profileSignInWithPUID,
.mm文件实现函数,(.mm文件里面既可以写oc代码也可以写c/c++代码)

// 实现文件 UMProfile.cpp
#include "UMProfile.h"
//导入友盟sdk
#import "UMMobClick/MobClick.h"

//--------------------------------------------------
void UMProfile::profileSignInWithPUID(const char* userID)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    //获取用户账号后转换string类型
    NSString *strNSString = [[NSString alloc] initWithUTF8String:userID];
    //调用友盟iOS统计函数即可。 
    [MobClick profileSignInWithPUID:strNSString];
#endif
}

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...