QuickSDK_ios.mm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. //
  2. // QuickSDKCocos_ios.m
  3. // QuickSDKInterfaceCocos
  4. //
  5. // Created by 0280106PC0119 on 15/9/8.
  6. // Copyright (c) 2015年 QuickSDK. All rights reserved.
  7. //
  8. #import "QuickSDK_ios.h"
  9. #import "QuickSDKInterfaceUnity.h"
  10. #if defined(__cplusplus)
  11. extern "C"{
  12. #endif
  13. extern void UnitySendMessage(const char *, const char *, const char *);
  14. #if defined(__cplusplus)
  15. }
  16. #endif
  17. @implementation QuickSDK_ios
  18. static QuickSDK_ios * __singleton__;
  19. + (QuickSDK_ios *)shareInstance {
  20. static dispatch_once_t predicate;
  21. dispatch_once( &predicate, ^{ __singleton__ = [[[self class] alloc] init]; } );
  22. return __singleton__;
  23. }
  24. - (instancetype)init {
  25. self = [super init];
  26. if (self) {
  27. [self addNotifications];
  28. initState = -1;
  29. bU3dInited = NO;
  30. }
  31. return self;
  32. }
  33. -(void)addNotifications
  34. {
  35. static BOOL isAdded = NO;
  36. if (isAdded) {
  37. return;
  38. }
  39. isAdded = YES;
  40. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(initResult:) name:kSmpcQuickSDKNotiInitDidFinished object:nil];
  41. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginResult:) name:kSmpcQuickSDKNotiLogin object:nil];
  42. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logoutResult:) name:kSmpcQuickSDKNotiLogout object:nil];
  43. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rechargeResult:) name:kSmpcQuickSDKNotiRecharge object:nil];
  44. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(realNameAuthResult:) name:kSmpcQuickSDKNotiRealAuth object:nil];
  45. }
  46. - (NSString *)jsonStrFromDictionary:(NSDictionary *)dic {
  47. NSError *error = nil;
  48. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:0 error:&error];
  49. if ([error code]) {
  50. return @"";
  51. }
  52. return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  53. }
  54. -(void)sendU3dMessage:(NSString *)messageName :(NSDictionary *)dict
  55. {
  56. if (dict != nil)
  57. {
  58. NSString *jsonString = [self jsonStrFromDictionary:dict];
  59. // char *gameObjectNameCStr = (char *)malloc(100);
  60. // strcpy(gameObjectNameCStr, [_gameObjectName UTF8String]);
  61. // char *messageNameCStr = (char *)malloc(100);
  62. // strcpy(messageNameCStr, [messageName UTF8String]);
  63. // char *jsonStringCStr = (char *)malloc(1000);
  64. // strcpy(jsonStringCStr, [jsonString UTF8String]);
  65. // UnitySendMessage(gameObjectNameCStr, messageNameCStr, jsonStringCStr);
  66. UnitySendMessage([_gameObjectName UTF8String], [messageName UTF8String], [jsonString UTF8String]);
  67. }
  68. else{
  69. // char *gameObjectNameCStr = (char *)malloc(100);
  70. // strcpy(gameObjectNameCStr, [_gameObjectName UTF8String]);
  71. // char *messageNameCStr = (char *)malloc(100);
  72. // strcpy(messageNameCStr, [messageName UTF8String]);
  73. // UnitySendMessage(gameObjectNameCStr, messageNameCStr, "");
  74. UnitySendMessage([_gameObjectName UTF8String], [messageName UTF8String], "");
  75. }
  76. }
  77. - (void)setListener:(NSString *)gameObjectName
  78. {
  79. _gameObjectName = gameObjectName;
  80. #if !__has_feature(objc_arc)
  81. _gameObjectName = [gameObjectName retain];
  82. #endif
  83. bU3dInited = YES;
  84. //在此时发消息回去才能保证u3d加载好了,可以接收初始化消息了
  85. if (initState == -1)//未初始化
  86. {
  87. ;
  88. }
  89. else if (initState == 0)//初始化失败
  90. {
  91. NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"init failed", @"msg", nil];
  92. [self sendU3dMessage:@"onInitFailed" :dic];
  93. initState = -1;
  94. }
  95. else if (initState == 1)//初始化成功
  96. {
  97. [self sendU3dMessage:@"onInitSuccess" :nil];
  98. initState = -1;
  99. }
  100. }
  101. -(void)login
  102. {
  103. [[SMPCQuickSDK defaultInstance] login];
  104. }
  105. -(void)logout
  106. {
  107. [[SMPCQuickSDK defaultInstance] logout];
  108. }
  109. -(void)pay:(SMPCQuickSDKPayOrderInfo *)orderInfo gameRoleInfo:(SMPCQuickSDKGameRoleInfo *)gameRoleInfo
  110. {
  111. [[SMPCQuickSDK defaultInstance] payOrderInfo:orderInfo roleInfo:gameRoleInfo];
  112. }
  113. -(void)Getlocalized:(NSString *)productIds
  114. {
  115. [[SMPCQuickSDK defaultInstance] Getlocalized:productIds];
  116. }
  117. - (void)updateRoleInfoWith:(SMPCQuickSDKGameRoleInfo *)gameRoleInfo
  118. {
  119. [[SMPCQuickSDK defaultInstance] updateRoleInfoWith:gameRoleInfo];
  120. NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:gameRoleInfo.serverId,@"serverId", gameRoleInfo.serverName,@"serverName",gameRoleInfo.gameRoleName,@"gameRoleName",gameRoleInfo.gameRoleID,@"gameRoleId",gameRoleInfo.gameUserBalance,@"gameUserBalance",
  121. gameRoleInfo.vipLevel,@"vipLevel",gameRoleInfo.gameUserLevel,@"gameUserLevel",gameRoleInfo.partyName,@"partyName",nil];
  122. [self sendU3dMessage:@"onUpdateRoleInfoSuccess" :dic];
  123. }
  124. -(void)updateRoleInfoWith:(SMPCQuickSDKGameRoleInfo *)gameRoleInfo isCreate:(BOOL)isCreate{
  125. [[SMPCQuickSDK defaultInstance] updateRoleInfoWith:gameRoleInfo isCreate:isCreate];
  126. NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:gameRoleInfo.serverId,@"serverId", gameRoleInfo.serverName,@"serverName",gameRoleInfo.gameRoleName,@"gameRoleName",gameRoleInfo.gameRoleID,@"gameRoleId",gameRoleInfo.gameUserBalance,@"gameUserBalance",
  127. gameRoleInfo.vipLevel,@"vipLevel",gameRoleInfo.gameUserLevel,@"gameUserLevel",gameRoleInfo.partyName,@"partyName",nil];
  128. [self sendU3dMessage:@"onUpdateRoleInfoSuccess" :dic];
  129. }
  130. -(void)enterYunKeFuCenter:(SMPCQuickSDKGameRoleInfo *)gameRoleInfo
  131. {
  132. [[SMPCQuickSDK defaultInstance] enterYunKeFuCenter:gameRoleInfo];
  133. }
  134. - (int)enterUserCenter
  135. {
  136. return [[SMPCQuickSDK defaultInstance] enterUserCenter];
  137. }
  138. -(void)openThirdLogin:(BOOL)thirdLogin
  139. {
  140. }
  141. - (NSString *)getIdCode
  142. {
  143. }
  144. -(BOOL)isGuester
  145. {
  146. return NO;
  147. }
  148. -(void)ShowCertification
  149. {
  150. }
  151. -(void) BindCertification;
  152. {
  153. }
  154. - (int)enterCustomerCenter
  155. {
  156. return 0;
  157. }
  158. - (int)enterBBS
  159. {
  160. return 0;
  161. }
  162. - (int)showRealNameAuth:(int)show {
  163. NSDictionary * dict = [[SMPCQuickSDK defaultInstance] realNameInfo];
  164. if ([dict[kSmpcQuickSDKKeyRealName] boolValue]) {
  165. [self sendU3dMessage:@"realAuthSuccess" :dict];
  166. return 0;
  167. }
  168. return show ? [[SMPCQuickSDK defaultInstance] realAuth]:0;
  169. }
  170. - (int)showToolBar:(int)place
  171. {
  172. return [[SMPCQuickSDK defaultInstance] showToolBar:(SMPC_QUICK_SDK_TOOLBAR_PLACE)place];
  173. }
  174. - (int)hideToolBar
  175. {
  176. return [[SMPCQuickSDK defaultInstance] hideToolBar];
  177. }
  178. -(int)pausedGame
  179. {
  180. return 0;
  181. }
  182. - (bool)isFunctionTypeSupported:(int)type
  183. {
  184. if (type == 8) {
  185. NSDictionary * dict = [[SMPCQuickSDK defaultInstance] realNameInfo];
  186. if ([dict[kSmpcQuickSDKKeyRealName] boolValue]) {
  187. [self sendU3dMessage:@"realAuthSuccess" :dict];
  188. return YES;
  189. }
  190. }
  191. return [[SMPCQuickSDK defaultInstance] isFunctionTypeSupported:(SMPC_QUICK_SDK_FUNC_TYPE)type];
  192. }
  193. - (NSString *)channelName //获取渠道名称
  194. {
  195. return @"";
  196. }
  197. - (NSString *)channelVersion //获取渠道版本
  198. {
  199. return @"";
  200. }
  201. - (int)channelType //获取渠道类别 渠道唯一标识
  202. {
  203. return [[SMPCQuickSDK defaultInstance] channelType];
  204. }
  205. - (NSString *)userId
  206. {
  207. return [[SMPCQuickSDK defaultInstance] userId];
  208. }
  209. - (NSString *)userToken{
  210. return [[SMPCQuickSDK defaultInstance] userToken];
  211. }
  212. - (NSString *)SDKVersion //QuickSDK版本
  213. {
  214. return @"";
  215. }
  216. - (NSString *)getConfigValue:(NSString *)key //QuickSDK版本
  217. {
  218. return [[SMPCQuickSDK defaultInstance] getConfigValue:key];
  219. }
  220. #pragma call back
  221. - (void)realNameAuthResult:(NSNotification *)notify {
  222. [self sendU3dMessage:@"realAuthSuccess" :notify.userInfo];
  223. }
  224. -(void)initResult:(NSNotification *)notify
  225. {
  226. NSDictionary *userInfo = notify.userInfo;
  227. int errorCode = [userInfo[kSmpcQuickSDKKeyError] intValue];
  228. switch (errorCode) {
  229. case SMPC_QUICK_SDK_ERROR_NONE:
  230. {
  231. if (bU3dInited)
  232. {
  233. [self sendU3dMessage:@"onInitSuccess" :nil];
  234. }
  235. else
  236. {
  237. initState = 1;
  238. }
  239. }
  240. break;
  241. case SMPC_QUICK_SDK_ERROR_INIT_FAILED:
  242. default:
  243. {
  244. if (bU3dInited)
  245. {
  246. NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"init failed", @"msg", nil];
  247. [self sendU3dMessage:@"onInitFailed" :dic];
  248. }
  249. else
  250. {
  251. initState = 0;
  252. }
  253. }
  254. break;
  255. }
  256. }
  257. -(void)loginResult:(NSNotification *)notify
  258. {
  259. NSDictionary *rDic = [notify userInfo];
  260. if ([rDic[kSmpcQuickSDKKeyError] intValue] == 0) {
  261. NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:rDic[@"uid"],@"userId", rDic[@"user_token"], @"userToken", rDic[@"user_name"], @"userName", @"", @"msg", nil];
  262. [self sendU3dMessage:@"onLoginSuccess" :dic];
  263. }else{
  264. NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:rDic[kSmpcQuickSDKKeyMsg],@"msg", nil];
  265. [self sendU3dMessage:@"onLoginFailed" :dic];
  266. }
  267. }
  268. -(void)logoutResult:(NSNotification *)notify
  269. {
  270. NSDictionary *rDic = [notify userInfo];
  271. if ([rDic[kSmpcQuickSDKKeyError] intValue] == 0) {
  272. [self sendU3dMessage:@"onLogoutSuccess" :nil];
  273. }else{
  274. NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:rDic[kSmpcQuickSDKKeyMsg],@"msg", nil];
  275. [self sendU3dMessage:@"onLogoutFailed" :dic];
  276. }
  277. }
  278. -(void)rechargeResult:(NSNotification *)notify
  279. {
  280. NSDictionary *rDic = [notify userInfo];
  281. if ([rDic[kSmpcQuickSDKKeyError] intValue] == 0) {
  282. NSLog(@"---------------%@", rDic);
  283. NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:rDic[kSmpcQuickSDKKeyOrderId],kSmpcQuickSDKKeyOrderId,rDic[kSmpcQuickSDKKeyCpOrderId],kSmpcQuickSDKKeyCpOrderId, nil];
  284. [self sendU3dMessage:@"onPaySuccess" :dic];
  285. }else if ([rDic[kSmpcQuickSDKKeyError] intValue] == SMPC_QUICK_SDK_ERROR_RECHARGE_CANCELLED){
  286. NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:rDic[kSmpcQuickSDKKeyCpOrderId],kSmpcQuickSDKKeyCpOrderId, nil];
  287. [self sendU3dMessage:@"onPayCancel" :dic];
  288. } else {
  289. NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:rDic[kSmpcQuickSDKKeyCpOrderId],kSmpcQuickSDKKeyCpOrderId, nil];
  290. [self sendU3dMessage:@"onPayFailed" :dic];
  291. }
  292. }
  293. @end