近來又開始對接UC的SDK對他應用到monodevelop平台上面的時候也是會有一些問題。這裡面是寫的源代碼。我們先看一下生成.a的工程文件
//
// UseStaticLibraryUc.h
// UseStaticLibraryUc
//
// Created by huangyushi on 13-1-22.
// Copyright (c) 2013年 huangyushi. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <pthread.h>
#import "UCGameSdk.h"
#import "UCGameSdkConst.h"
#import "StaticLibraryManager.h"
typedef NSString* (^charge_cb) (NSInteger nType);
typedef struct{
int _roleid;
NSString* _rolename;
NSString* _customInfo;
}product_t;
@interface UseStaticLibraryUc : 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:(NSInteger )roleId RoleName:(NSString* )roleName customInfo:(NSString* )customInfo;
//封裝對外接口
+(UseStaticLibraryUc* )Isrance;
-(void)setInitComplete:(charge_cb)complete;
-(void)loginUC:(charge_cb)suc Fail:(charge_cb)fail Cancel:(charge_cb)cancel;
-(void)buyUC:(NSInteger )_roleId RoleName:(NSString* )_roleName customInfo:(NSString* )_customInfo Success:(charge_cb)suc Fail:(charge_cb)fail Cancel:(charge_cb)cancel;
-(void)setConnectFunc:(charge_cb)sd Rec:(charge_cb)rec Packet:(charge_cb)packet;
-(NSString* )getUserName;
-(NSString* )getUserPasswd;
-(NSString* )getSid;
@end
//
// UseStaticLibraryUc.m
// UseStaticLibraryUc
//
// Created by huangyushi on 13-1-22.
// Copyright (c) 2013年 huangyushi. All rights reserved.
//
#import "UseStaticLibraryUc.h"
static charge_cb call_backs[10] = {NULL};
static NSString* game_user =NULL;
static NSString* 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 NSString* ucsid =NULL;
static NSMutableDictionary *resultDict;
void* wait_for_cour_server_replay(void* arg)
{
ucsid = NULL;
while (true)
{
if (call_backs[connect_packet] !=NULL) {
call_backs[connect_packet](connect_packet);
}
if (call_backs[Connect_receive] !=NULL) {
if (call_backs[Connect_receive](Connect_receive) !=NULL) {
ucsid = [NSStringstringWithFormat:@"%@",call_backs[Connect_receive](Connect_receive)];
if (ucsid ==@"error") {
[resultDictsetValue:[NSNumber numberWithInt:1]forKey:@"resultCode"];
[resultDict setValue:@"帳號或密碼錯誤,請重新輸入." forKey:@"resultMsg"];
pthread_cond_signal(&thread_cond);//喚醒阻塞的線程
break;
}
}
}
if(NULL != ucsid){
int resultCode = 0;
[resultDict setValue:ucsid forKey:@"sid"];
[resultDict setValue: [NSNumbernumberWithInt : resultCode] forKey:@"resultCode"];
[resultDict setValue:@"OK"forKey:@"resultMsg"];
pthread_cond_signal(&thread_cond);//喚醒阻塞的線程
break;
}
}
return NULL;
}
@implementation UseStaticLibraryUc
- (void)viewDidUnload{
}
-(void)viewDidLoad
{
}
//登陸部分
-(id)init
{
self = [super init];
if(NULL != self)
{
[[NSNotificationCenterdefaultCenter] removeObserver:self];
//添加 Observer來監聽 SDK 初始化事件,以便在 SDK初始化完成時進行後續處理。
[[NSNotificationCenterdefaultCenter] addObserver:self
selector:@selector(onSdkInitFin:)
name:UCG_SDK_MSG_SDK_INIT_FIN
object:nil];
//設置游戲相關參數
UCGameSdk *sdk = [UCGameSdkdefaultSDK];
/** 設置游戲相關參數*/
//正式
// sdk.cpId = 593; // 設置游戲合作商編號,該編號在游戲接入時由 UC 分配
// sdk.gameId = 105539; // 設置游戲編號,該編號在游戲接入時由 UC 分配
// sdk.serverId= 1245; // 設置游戲服務器 ID,該編號由所在的游戲分區決定
//測試
sdk.cpId =1; // 設置游戲合作商編號,該編號在游戲接入時由 UC 分配
sdk.gameId =176; // 設置游戲編號,該編號在游戲接入時由 UC分配
sdk.serverId=180; // 設置游戲服務器 ID,該編號由所在的游戲分區決定
//設置聯測模式或正式生產模式:YES:聯測模式,SDK會自動連接到 SDK 平 台的聯測環境;NO:正式生產模式,SDK會自 連接到 SDK 平台的正式生產環境。
sdk.isDebug = YES;
sdk.gameUserName = @"叮叮堂官方帳號";
sdk.allowGameUserLogin =YES;
//設置游戲附加信息
ExInfo *exInfo = [[[ExInfoalloc] init] autorelease];
exInfo.cpServiceContact =@"叮叮堂\n客服電話:021-50183061";
sdk.exInfo = exInfo;
[sdk initSDK];
NSLog(@"進入到UC_SDK的初始化");
}
return self;
}
//SDK初始化監聽處理函數
- (void) onSdkInitFin: (NSNotification *) notification
{
[[NSNotificationCenterdefaultCenter] removeObserver:selfname:UCG_SDK_MSG_SDK_INIT_FINobject:nil];
// 讀取初始化結果數據
NSDictionary *res = notification.userInfo;
NSNumber *code = (NSNumber *)[resobjectForKey:@"code"];
// 判斷收到的 code 的值,確定 SDK 是否成功初始化
// code 為 1 時表示 SDK 初始化失敗,為 0 表示 SDK 初始化成功
if ([code intValue] ==1)
{
NSLog(@"監聽到初始化失敗");
return;
}
NSLog(@"監聽到初始化成功");
if(NULL !=call_backs[init_complete])
{
call_backs[init_complete](init_complete);
}
[[NSNotificationCenterdefaultCenter] removeObserver:self
name:UCG_SDK_MSG_LOGIN_FIN
object:nil];
[[NSNotificationCenterdefaultCenter] removeObserver:self
name:UCG_SDK_MSG_EXIT_WITHOUT_LOGIN
object:nil];
// 添加 Observer 以監聽 SDK 登錄消息
[[NSNotificationCenterdefaultCenter] addObserver:self
selector:@selector(onLoginFin:)
name:UCG_SDK_MSG_LOGIN_FINobject:nil];
// 添加 Observer 以監聽 SDK 未登錄返回游戲 界面時通知
[[NSNotificationCenterdefaultCenter] addObserver:self
selector:@selector(onUnloginExit)
name:UCG_SDK_MSG_EXIT_WITHOUT_LOGINobject:nil];
[[NSNotificationCenterdefaultCenter] removeObserver:self
name:UCG_SDK_MSG_PAY_FIN
object:nil];
[[NSNotificationCenterdefaultCenter] removeObserver:self
name:UCG_SDK_MSG_PAY_EXIT
object:nil];
[[NSNotificationCenterdefaultCenter] addObserver:self
selector:@selector(onPayFin:)
name:UCG_SDK_MSG_PAY_FINobject:nil];
[[NSNotificationCenterdefaultCenter] addObserver:self
selector:@selector(payExit:)
name:UCG_SDK_MSG_PAY_EXITobject:nil];
}
//UC帳號登陸
- (void) onLoginFin: (NSNotification *) notification
{
// 讀取登錄結果數據
NSDictionary *res = notification.userInfo;
NSNumber *code = (NSNumber *)[resobjectForKey:@"code"];
// 判斷收到的 code 的值,確定 SDK 是否成功登錄
// code 為 0 時表示登錄成功,為 1 表示登錄失敗
if (code.intValue ==0)
{
NSLog(@"成功登錄");
if(NULL !=call_backs[Login_success] )
{
call_backs[Login_success](Login_success);
call_backs[Login_success] =NULL;
}
}
else
{
NSLog(@"登錄失敗");
if(NULL !=call_backs[Login_fail] )
{
call_backs[Login_fail](Login_fail);
call_backs[Login_fail] =NULL;
}
}
NSLog(@"完成登錄處理");
}
// 調用此函數即代表用戶在未登錄的情況下退出了登錄界面
- (void)onUnloginExit
{
NSLog(@"監聽到未登錄返回游戲");
if(NULL !=call_backs[Login_fail] )
{
call_backs[Login_fail](Login_fail);
call_backs[Login_fail] =NULL;
}
}
- (NSDictionary*)verifyGameUser:(NSString *)gameUser gamePassword:(NSString *)gamePassword
{
NSLog(@"線程開始");
//開線程,並且阻塞當前線持
pthread_cond_init(&thread_cond,NULL);
pthread_mutex_init(&thread_lock,NULL);
// 發用戶名和和密碼給我們的服務器
pthread_mutex_lock(&thread_lock);
resultDict = [[[NSMutableDictionaryalloc] init] autorelease];
game_user = gameUser;
game_passwd = gamePassword;
if(NULL !=call_backs[Connect_send]){
call_backs[Connect_send](Connect_send);
}
//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);
NSLog(@"string after append is111111111111111111 %@",ucsid);
returnresultDict;
}
- (void)buy:(NSInteger )_roleId RoleName:(NSString* )_roleName customInfo:(NSString* )_customInfo
{
char order[200];
constchar *char_content = [_customInfocStringUsingEncoding:NSASCIIStringEncoding];
sprintf(order, "%s",char_content);
NSString* customInfo = [NSStringstringWithUTF8String:order];
NSString* roleId = [NSStringstringWithFormat:@"%d",_roleId];
NSString* roleName = _roleName;
NSDictionary *dict = [NSDictionarydictionaryWithObjectsAndKeys:
[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 *)[resobjectForKey:@"code"];
if (code.intValue ==0){
if(NULL !=call_backs[Buy_success] ){
call_backs[Buy_success](Buy_success);
call_backs[Buy_success] =NULL;
}
}else{
if(NULL != call_backs[Buy_fail] ){
call_backs[Buy_fail](Buy_fail);
call_backs[Buy_fail] =NULL;
}
}
}
- (void)payExit: (NSNotification *) notification{
}
/**
**靜態庫對外接口
*/
static UseStaticLibraryUc *instance =nil;
+(UseStaticLibraryUc* )Isrance
{
if (instance ==nil)
{
instance = [[UseStaticLibraryUcalloc] init];
}
returninstance;
}
-(void)setInitComplete:(charge_cb)complete
{
call_backs[init_complete] = [completecopy];
}
-(void)loginUC:(charge_cb)suc Fail:(charge_cb)fail Cancel:(charge_cb)cancel
{
call_backs[Login_success] = [succopy];
call_backs[Login_fail] = [failcopy];
call_backs[Login_cancel] = [cancelcopy];
NSLog(@"點擊登錄按鈕");
[[UCGameSdk defaultSDK]loginWithDelegate:self];
// [[UCGameSdk defaultSDK] login];
}
-(void)buyUC:(NSInteger )_roleId RoleName:(NSString* )_roleName customInfo:(NSString* )_customInfo Success:(charge_cb)suc Fail:(charge_cb)fail Cancel:(charge_cb)cancel
{
NSLog(@"進入到buyUC");
call_backs[Buy_success] = [succopy];
call_backs[Buy_fail] = [failcopy];
call_backs[Buy_cancel] = [cancelcopy];
NSLog(@"運行到要調用buy");
[instance buy:_roleId
RoleName:_roleName
customInfo:_customInfo];
NSLog(@"buy調用進入了");
}
-(void)setConnectFunc:(charge_cb)sd Rec:(charge_cb)rec Packet:(charge_cb)packet
{
call_backs[Connect_send] = [sdcopy];
call_backs[Connect_receive] = [reccopy];
call_backs[connect_packet] = [packetcopy];
}
-(NSString* )getUserName
{
returngame_user;
}
-(NSString* )getUserPasswd
{
returngame_passwd;
}
-(NSString* )getSid
{
return [[UCGameSdkdefaultSDK] sid];
}
@end
//綁定到monotouch中
public delegate NSString XMUtilityCallback (int nType);
[BaseType (typeof (UIViewController), Delegates =new string[]{"UCGameSdkProtocol"} )]
interface UseStaticLibraryUc
{
[Static, Export ("Isrance")]
UseStaticLibraryUc Isrance ();
[Export ("setInitComplete:")]
string setInitComplete (XMUtilityCallback complete);
//逋サ蠖
[Export ("loginUC:Fail:Cancel:")]
void loginUC (XMUtilityCallback suc, XMUtilityCallback fail, XMUtilityCallback cancel);
[Export ("buyUC:RoleName:customInfo:Success:Fail:Cancel:")]
void buyUC (int _roleId,string _roleName, string _customInfo, XMUtilityCallback suc, XMUtilityCallback fail, XMUtilityCallback cancel);
[Export ("setConnectFunc:Rec:Packet:")]
void setConnectFunc (XMUtilityCallback sd, XMUtilityCallback rec, XMUtilityCallback packet);
[Export ("getUserName")]
string getUserNameUC ();
[Export ("getUserPasswd")]
string getUserPasswdUC ();
[Export ("getSid")]
string getSidUC ();
}
裡面有個問題一直沒有明白是為什麼,就是當初在用uc sdk 的時候在靜態庫中的函數調用了我們的回調函數,他是在他回調函數中的實現裡面我們不能夠使用斷點或是創建線程。一直在查後來發現是一個signal的東西,聽UC的技術人員說這個是一個用於錯誤信號,後來我們要求把他去了後,反而可以。一直沒有個明確的答案,因為時間關系,沒有能深入雲研究。