#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 <UIKit/UIKit.h> #import <pthread.h> #import <Foundation/Foundation.h> #import "UCGameSdk/UCGameSdk.h" #import "UCGameSdk/UCGameSdkConst.h" #import "ChargeManager.h" #include <iostream> 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<UCGameSdkProtocol>{ } //加載 - (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{ } //登陸部分 // UC-技術接口人-陳子浩: 17:00:04 // 上古世界的生產環境gameid請用105174 // sdk.cpId = 593; // sdk.gameId = 64710; //105174 // sdk.serverId = 1375; // 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 = @"上古世界\n客服電話:021-50183061";//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) { www.2cto.com NSString str = null; return str; } 這些在官方的實例在也有的。