#ifndef __Charge_uc_h__
#define __Charge_uc_h__
typedef void* (*charge_cb)(void*);
struct product_t{
int _id;
const char* _name;
int _roleid;
const char* _rolename;
};
class cppChargeManager_uc{
static void* _rootView;
cppChargeManager_uc();
~cppChargeManager_uc();
cppChargeManager_uc(const cppChargeManager_uc&);
cppChargeManager_uc& operator=(const cppChargeManager_uc&);
public:
static cppChargeManager_uc* getInstance(void);
//注册ID和Key
void init(void* addr);
//登录
void login(charge_cb suc, charge_cb fail, charge_cb cancel =NULL);
//购买
void buy(product_t* info, charge_cb suc, charge_cb fail, charge_cb cancel =NULL);
//设置链接回调函数
void setConnectFunc(charge_cb sd, charge_cb rec);
//获得用户姓名和密码
const char* getUserName(void);
const char* getUserPasswd(void);
const char* getSid(void);
};
#endif
#import "Charge_uc.h"
#import
#import
#import
#import "UCGameSdk/UCGameSdk.h"
#import "UCGameSdk/UCGameSdkConst.h"
#import "ChargeManager.h"
#include
using namespace std;
static charge_cb call_backs[10] = {NULL};
static const char* game_user =NULL;
static const char* game_passwd = NULL;
//游戏帐号登录需要用到的变量
static pthread_cond_t thread_cond = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t thread_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_t connect_to_our_server =0;
static string ucsid;
void* wait_for_cour_server_replay(void*){
ucsid.clear();
while (true){
void* value = NULL;
if(NULL != call_backs[Connect_receive])
{
value = call_backs[Connect_receive](NULL);
call_backs[Connect_receive] = NULL;
}
if(NULL != value){
const char* text =static_cast<constchar*>(value);
ucsid = text;
pthread_cond_signal(&thread_cond); //唤醒阻塞的线程
break;
}
}
return NULL;
}
@interface UCViewController : UIViewController
}
//加载
- (void)viewDidLoad;
- (void)viewDidUnload;
//登陆部分
- (void) onSdkInitFin: (NSNotification *) notification;
- (void) onLoginFin: (NSNotification *) notification;
- (void)onUnloginExit;
//支付部分
- (void)onPayFin: (NSNotification *) notification;
- (void)payExit: (NSNotification *) notification;
- (void)buy:(product_t*)info;
@end
@implementation UCViewController
- (void)viewDidUnload{
}
-(void)viewDidLoad{
}
//登陆部分
// sdk.cpId = 0;
// sdk.gameId = 0;
// sdk.serverId = 0;
// sdk.isDebug = NO;
-(id)init{
self = [super init];
if(NULL != self){
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onSdkInitFin:) name:UCG_SDK_MSG_SDK_INIT_FIN object:nil];
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
NSString* hostUrl = @"http://sdk.test4.g.uc.cn/cs/data/host/";
NSString* siUrl = @"http://sdk.test4.g.uc.cn/cs/data/si/";
[userDefault setValue:hostUrl forKey:@"UCG_SDK_URL_HOST_DEBUG"];
[userDefault setValue:siUrl forKey:@"UCG_SDK_URL_SI_DEBUG"];
UCGameSdk *sdk = [UCGameSdk defaultSDK];
sdk.cpId = 4;
sdk.gameId = 4;
sdk.serverId = 11;
sdk.isDebug = YES;
sdk.logLevel = UCLOG_LEVEL_DEBUG;
sdk.allowGameUserLogin = YES;
sdk.gameUserName = @"官方帐号";
ExInfo *exInfo = [[[ExInfo alloc] init] autorelease];
exInfo.cpServiceContact = @"客服电话:021-110110110";//self.feedbackCPTxtTextView.text;
sdk.exInfo = exInfo;
[sdk initSDK];
}
return self;
}
- (void)onUnloginExit //登陆失败
{
if(NULL != call_backs[Login_fail] )
{
call_backs[Login_fail](NULL);
call_backs[Login_fail] = NULL;
}
}
- (void) onSdkInitFin: (NSNotification *) notification
{
NSDictionary *res = notification.userInfo;
NSNumber *code = (NSNumber *)[res objectForKey:@"code"];
if ([code intValue] == 1)
return;
[[NSNotificationCenter defaultCenter] removeObserver:self name:UCG_SDK_MSG_LOGIN_FIN object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UCG_SDK_MSG_EXIT_WITHOUT_LOGIN object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onLoginFin:) name:UCG_SDK_MSG_LOGIN_FIN object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onUnloginExit) name:UCG_SDK_MSG_EXIT_WITHOUT_LOGIN object:nil];
[[UCGameSdk defaultSDK] loginWithDelegate:self];
}
- (void) onLoginFin: (NSNotification *) notification //UC帐号登陆
{
NSDictionary *res = notification.userInfo;
NSNumber *code = (NSNumber *)[res objectForKey:@"code"];
if (code.intValue == 0)
{
if(NULL != call_backs[Login_success] )
{
call_backs[Login_success](NULL);
call_backs[Login_success] = NULL;
}
}else{
if(NULL != call_backs[Login_fail] )
{
call_backs[Login_fail](NULL);
call_backs[Login_fail] = NULL;
}
}
}
- (NSDictionary*)verifyGameUser:(NSString *)gameUser gamePassword:(NSString *)gamePassword
{
NSString *sid;
int resultCode = 0;
NSMutableDictionary *resultDict = [[[NSMutableDictionary alloc] init] autorelease];
//开线程,并且阻塞当前线持
pthread_cond_init(&thread_cond, NULL);
pthread_mutex_init(&thread_lock, NULL);
// 发用户名和和密码给我们的服务器
pthread_mutex_lock(&thread_lock);
game_user = gameUser.UTF8String;
game_passwd = gamePassword.UTF8String;
if(NULL != call_backs[Connect_send]){
call_backs[Connect_send](NULL);
call_backs[Connect_send] = NULL;
}
//func
pthread_create(&connect_to_our_server, NULL, wait_for_cour_server_replay,NULL);
pthread_cond_wait(&thread_cond,&thread_lock);
pthread_mutex_unlock(&thread_lock);
//下面继续跑,回收等待线程和其他的变量等
pthread_join(connect_to_our_server, NULL);
pthread_mutex_destroy(&thread_lock);
pthread_cond_destroy(&thread_cond);
sid = [NSString stringWithUTF8String:ucsid.c_str()];
[resultDict setValue: sid forKey:@"sid"];
[resultDict setValue: [NSNumber numberWithInt : resultCode] forKey:@"resultCode"];
[resultDict setValue:@"OK" forKey:@"resultMsg"];
return resultDict;
}
- (void)buy:(product_t*)info
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onPayFin:) name:UCG_SDK_MSG_PAY_FIN object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(payExit:) name:UCG_SDK_MSG_PAY_EXIT object:nil];
char order[200];
sprintf(order, "serverip=sgmx#channel=%d#user=%s",info->_id,info->_name);
NSString* customInfo = [NSString stringWithUTF8String:order];
NSString* roleId = [NSString stringWithFormat:@"%d",info->_roleid];
NSString* roleName = [NSString stringWithUTF8String:info->_rolename];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES],@"allowContinuousPay",
customInfo , @"customInfo", roleId,@"roleId",
roleName, @"roleName",nil];
[[UCGameSdk defaultSDK] payWithPaymentInfo:dict];
}
- (void) onPayFin: (NSNotification *) notification
{
NSDictionary *res = notification.userInfo;
NSNumber *code = (NSNumber *)[res objectForKey:@"code"];
if (code.intValue == 0){
if(NULL != call_backs[Buy_success] )
call_backs[Buy_success](NULL);
}else{
if(NULL != call_backs[Buy_fail] )
call_backs[Buy_fail](NULL);
}
}
- (void)payExit: (NSNotification *) notification{
}
@end
void* cppChargeManager_uc::_rootView = NULL;
static UCViewController * uc_view = NULL;
cppChargeManager_uc::cppChargeManager_uc(){
uc_view = [[UCViewController alloc] init];
}
cppChargeManager_uc::~cppChargeManager_uc(){
[uc_view dealloc];
uc_view = NULL;
}
cppChargeManager_uc* cppChargeManager_uc::getInstance(void){
static cppChargeManager_uc _only;
return &_only;
}
void cppChargeManager_uc::init(void* addr){
_rootView = (UCViewController*)addr;
}
void cppChargeManager_uc::login(charge_cb suc, charge_cb fail, charge_cb cancel){
call_backs[Login_success] = suc;
call_backs[Login_fail] = fail;
call_backs[Login_cancel] = cancel;
UIViewController* root = (UIViewController*)_rootView;
[root addSubview:uc_view.view];
}
void cppChargeManager_uc::buy(product_t* info, charge_cb suc, charge_cb fail, charge_cb cancel)
{
call_backs[Buy_success] = suc;
call_backs[Buy_fail] = fail;
call_backs[Buy_cancel] = cancel;
[uc_view buy:info];
}
void cppChargeManager_uc::setConnectFunc(charge_cb sd, charge_cb rec)
{
call_backs[Connect_send] = sd;
call_backs[Connect_receive] = rec;
}
const char* cppChargeManager_uc::getUserName(void)
{
return game_user;
}
const char* cppChargeManager_uc::getUserPasswd(void)
{
return game_passwd;
}
const char* cppChargeManager_uc::getSid(){
return [[[UCGameSdk defaultSDK] sid] UTF8String];
}
对上面的一个代理可能有些人不知道用,或是要分些时间去研究,在这不如我直接就给出来,
UseStaticLibraryUc.Isrance().setConnectFunc(new MonoBindingUc.XMUtilityCallback(this.onUcLoginResult),
new MonoBindingUc.XMUtilityCallback(this.onUcLoginResult),
new MonoBindingUc.XMUtilityCallback(this.onUcLoginResult));
public string onUcLoginResult(int type)
{
NSString str = null;
return str;
}
这些在官方的实例在也有的。