IOSPlatformSDK.mm 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. //
  2. // IOSPlatformSDK.m
  3. // OYGameSKD
  4. //
  5. // Created by leon on 2021/1/21.
  6. // Copyright © 2021 Oujia. All rights reserved.
  7. #import "IOSPlatformSDK.h"
  8. #pragma mark ============================>> ios call unity C语言指针声明 start
  9. /**
  10. *运动数据
  11. * left: 左鞋动作代码
  12. * right: 右鞋动作代码
  13. * 对应 enum CMD_MOTION
  14. */
  15. typedef void (* MotionHandler) (int cusid, int left, int right);
  16. /**
  17. * 踏步状态,频率
  18. * leftStatus: 左鞋(0:停止, 1:慢,2:快)
  19. * rightStatus: 右鞋(0:停止, 1:慢,2:快)
  20. * leftFrag: 左鞋(每分钟步频)
  21. * rightFrag: 右鞋(每分钟步频)
  22. */
  23. typedef void (* StepHandler)(int cusid, int leftStatus, int rightStatus, int leftFrag, int rightFrag);
  24. /**
  25. * 返回蓝牙设备的基本信息,用于界面上显示当前的设备状况,app 跳转的时候传过来的
  26. * name: 设备名称
  27. * status: 设备状态 @see enum BLE_STATE
  28. * electricity: 设备电量 取值范围[0 - 100] 对比左右鞋 handler较低的电量
  29. */
  30. typedef void (* DeviceHandler)(int cusid, const char *name, const char *address, int status, int electricity);
  31. /**
  32. * 好友列表 unity call ios 异步请求数据后 ios call unity
  33. * 调用api GetUserFriends() 的回调
  34. * code: 请求结果代码 0: 成功, 非0: 根据业务处理
  35. * json: 好友列表数据 , json 数组
  36. *
  37. */
  38. typedef void (* UserFriendsHandler)(int code, const char * json);
  39. /**
  40. * 调用api GetUserFriends() 的回调
  41. * code: 请求结果代码 0: 成功, 非0: 根据业务处理
  42. * json: 好友列表数据 , json 数组
  43. */
  44. typedef void (* InviteFriendHandler)(int code, const char * userJson, const char * inviteInfo);//
  45. /**
  46. *游戏首页交互动作的回调
  47. * cusid: 主副设备
  48. * code: 交互代码
  49. */
  50. typedef void (* ActionHandler) (int cusid, int code);
  51. /**
  52. * 游戏榜单数据回调
  53. * cusid: 主副设备
  54. * code: 交互代码
  55. */
  56. typedef void (* GetRankHandler)(int conde,const char * userJson);
  57. ActionHandler actionHandler;
  58. MotionHandler motionHandler;
  59. StepHandler stepHandler;
  60. DeviceHandler deviceHandler;
  61. UserFriendsHandler userFriendsHandler;
  62. InviteFriendHandler inviteFriendHandler;
  63. GetRankHandler getRankHandler;
  64. //
  65. #pragma mark ============================>> ios call unity C语言指针声明 结束
  66. @interface IOSPlatformSDK ()
  67. //计算游戏启动时间
  68. @property(nonatomic,strong)NSDate * firstTime;
  69. @property(nonatomic,strong)UILabel * testLabel;
  70. @property(copy,nonatomic)NSString * macAddress;
  71. @end
  72. @implementation IOSPlatformSDK
  73. #pragma mark ============================>> SDK 单例
  74. static IOSPlatformSDK * instance;
  75. +(instancetype)sharedInstance{
  76. return [[self alloc] init];
  77. }
  78. + (instancetype)allocWithZone:(struct _NSZone *)zone{
  79. static dispatch_once_t onceToken;
  80. dispatch_once(&onceToken, ^{
  81. instance = [super allocWithZone:zone];
  82. });
  83. return instance;
  84. }
  85. - (instancetype)init{
  86. static dispatch_once_t onceToken;
  87. dispatch_once(&onceToken, ^{
  88. instance = [super init];
  89. [self initNotification];
  90. });
  91. return instance;
  92. }
  93. #pragma mark ============================>> public method app跳转直链
  94. -(void)startWithUrl:(NSURL*)url{//
  95. if (url==nil){
  96. return;
  97. }
  98. NSString * urlString = [url absoluteString];
  99. NSArray * stringArr = [urlString componentsSeparatedByString:@"//"];
  100. NSString * arr2 = stringArr[1];
  101. NSString* finsalString = [arr2 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  102. NSDictionary * urlDict = [AlgorithmTool dictionaryWithJsonString:finsalString];
  103. NSLog(@"Schemes_Url转码的dict ========= %@",urlDict);
  104. //固定一个时间戳
  105. NSString * timestamp = [AlgorithmTool returnTimestamp];
  106. [IOS_NSUSERDEFAULT setObject:timestamp forKey:IOSSDK_TIMESTAMP];
  107. [IOS_NSUSERDEFAULT synchronize];
  108. if (urlDict!=nil){
  109. //缓存用户信息数据
  110. [IOS_NSUSERDEFAULT setObject:urlDict forKey:IOSSDK_USERINFO];
  111. [IOS_NSUSERDEFAULT synchronize];
  112. //主副设备:deviceType 游戏种类:game_id mac地址:macAddress
  113. BTDataProcess* bleInstance = [BTDataProcess sharedInstance];
  114. bleInstance.deviceType = DEVICETYPE_MAIN;
  115. NSString * game_id = urlDict[@"game_id"];
  116. bleInstance.game_id = (GAME_TYPE)[game_id intValue];
  117. if ([urlDict objectForKey:@"mac"]!=nil){
  118. bleInstance.macAddress = [urlDict objectForKey:@"mac"];
  119. self.macAddress = [urlDict objectForKey:@"mac"];
  120. }
  121. //链接蓝牙
  122. [bleInstance initCBCentralManager];
  123. //数据存入plist
  124. bleInstance.allMotionCountBLock = ^(int jump_count, int crouch_count, int step_count){
  125. // NSLog(@"jump_count = %d crouch_count = %d step_count = %d ",jump_count,crouch_count,step_count);
  126. [self cacheAllMotionCount:jump_count crouchCount:crouch_count stepCount:step_count];
  127. };
  128. }
  129. //先打开app后已经创建了label,第二次open重新赋值
  130. self.testLabel.text = [NSString stringWithFormat:@"%@ mac: %@",self.openStyle,self.macAddress];
  131. }
  132. #pragma mark ============================>> private 各类通知处理事件
  133. -(void)initNotification{
  134. //监听各类通知action
  135. [CUS_NOTIFICATIONCENTER addObserver:self selector:@selector(gameStartInitData) name:@"gameStartInitData" object:nil];
  136. [CUS_NOTIFICATIONCENTER addObserver:self selector:@selector(gameEndInitData:) name:@"gameEndInitData" object:nil];
  137. [CUS_NOTIFICATIONCENTER addObserver:self selector:@selector(searchBLEAction:) name:@"searchBLEAction" object:nil];
  138. //监听 app group 消息 ==>> 好友邀请信息
  139. CFStringRef invite = (__bridge CFStringRef)@"INVITE_NOTIFICATION";
  140. CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
  141. NULL,
  142. inviteFriends,
  143. invite,
  144. NULL,
  145. CFNotificationSuspensionBehaviorDeliverImmediately);
  146. }
  147. //趣动app:广播的通知回调
  148. void inviteFriends (CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo){
  149. //group.com.Oujia.AppAndGame 沙河数据
  150. NSUserDefaults* userDefault = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.Oujia.AppAndGame"];
  151. //趣动传过来的 invite json字符串转字典
  152. NSString * string = [userDefault objectForKey:@"invite"];
  153. NSData * data = [string dataUsingEncoding:NSUTF8StringEncoding];
  154. NSDictionary * inviteDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
  155. if (inviteDict!=nil){
  156. //字典取值
  157. NSDictionary * invite = [inviteDict objectForKey:@"invite"];
  158. NSDictionary * user = [invite objectForKey:@"user"];
  159. //user字典转字符串
  160. NSData * userData = [NSJSONSerialization dataWithJSONObject:user options:NSJSONWritingPrettyPrinted error:nil];
  161. NSString * userString = [[NSString alloc] initWithData:userData encoding:NSUTF8StringEncoding];
  162. const char * userChar =[userString UTF8String];
  163. //info字典转字符串
  164. NSString * infoString = invite[@"info"];
  165. const char * infoChar =[infoString UTF8String];
  166. NSLog(@"IOS_SKD 回调好友邀请信息 GetInviteInfo ===>> %s , %s",userChar,infoChar);
  167. inviteFriendHandler(0,userChar,infoChar);
  168. }
  169. }
  170. //通知开始游戏
  171. -(void)gameStartInitData{
  172. NSLog(@"unity call ios 通知开始游戏 --> IOSPlatformSDK");
  173. [[BTDataProcess sharedInstance] gameStartInitData];
  174. if ([self existUserInfo]){
  175. self.firstTime = [NSDate date];
  176. }
  177. }
  178. //通知结束游戏
  179. -(void)gameEndInitData:(NSNotification*)notification{
  180. [[BTDataProcess sharedInstance] gameEndInitData];
  181. NSLog(@"unity call ios 通知结束游戏 --> IOSPlatformSDK");
  182. }
  183. //通知弹出蓝牙搜索框
  184. -(void)searchBLEAction:(NSNotification*)notification{
  185. NSLog(@"unity call ios 通知游戏弹窗 --> IOSPlatformSDK");
  186. if ([instance existUserInfo]==NO){
  187. return;
  188. }
  189. /***************************初始化蓝牙数据处理工具***************************/
  190. //主副设备:deviceType 游戏种类:game_id mac地址:macAddress
  191. BTDataProcess* bleInstance = [BTDataProcess sharedInstance];
  192. NSDictionary * notificationDict = notification.userInfo;
  193. NSNumber * typeNumber = [notificationDict objectForKey:@"type"];
  194. int type = [typeNumber intValue];
  195. bleInstance.deviceType = (DEVICE_TYPE)type;
  196. NSDictionary * jsonDict = [IOS_NSUSERDEFAULT objectForKey:IOSSDK_USERINFO];
  197. NSString * game_id = jsonDict[@"game_id"];
  198. bleInstance.game_id = (GAME_TYPE)[game_id intValue];
  199. bleInstance.macAddress = @"";
  200. //初始化蓝牙CBCentralManager
  201. [bleInstance initCBCentralManager];
  202. //缓存动作数据
  203. bleInstance.allMotionCountBLock = ^(int jump_count, int crouch_count, int step_count){
  204. // NSLog(@"jump_count = %d crouch_count = %d step_count = %d ",jump_count,crouch_count,step_count);
  205. [self cacheAllMotionCount:jump_count crouchCount:crouch_count stepCount:step_count];
  206. };
  207. /***************************搜索蓝牙外设弹窗***************************/
  208. SearchDeviceViewController * searchVC = [SearchDeviceViewController new];
  209. [self addTOwindow:searchVC];
  210. //searchVC搜索蓝牙Button
  211. searchVC.searchDeviceTask = ^{
  212. [bleInstance initCBCentralManager];
  213. };
  214. //BTDataProces搜索到的蓝牙外设-->searchVC
  215. bleInstance.deviceArrBLock = ^(NSMutableArray * arr){//蓝牙数据回调 刷新popView数据
  216. searchVC.deviceArray = arr;
  217. [searchVC reloadData];
  218. // [searchVC hideAnimation];
  219. };
  220. //点击searchVC的tableViewCell链接选中的蓝牙
  221. searchVC.connectDeviceBlock = ^(CBPeripheral * peripheral){
  222. bleInstance.deviceType = (DEVICE_TYPE)type;
  223. // NSLog(@"connectDeviceBlock = %@ %ld",peripheral,(long)bleInstance.deviceType);
  224. [bleInstance connectPeripheral:peripheral];
  225. bleInstance.macAddress = peripheral.identifier.UUIDString;
  226. };
  227. //bleInstance脚步交互动作-->searchVC
  228. bleInstance.getInteractionBlock = ^(int interaction){
  229. if (interaction!=-1){
  230. NSLog(@"BTDataProcess 有效交互动作 ===>> %d",interaction);
  231. // [searchVC getInteraction:interaction];
  232. }else{
  233. // NSLog(@"BTDataProcess 无效交互动作 ===>> %@",@"原地");
  234. }
  235. };
  236. }
  237. //
  238. #pragma mark ============================>> private 全局弹窗
  239. -(void)addTOwindow:(UIViewController*)searchVC{
  240. UIViewController *rootVC = [[UIApplication sharedApplication].delegate window].rootViewController;
  241. UIViewController *parent = rootVC;
  242. while ((parent = rootVC.presentedViewController) != nil ){
  243. rootVC = parent;
  244. }
  245. while ([rootVC isKindOfClass:[UINavigationController class]]){
  246. rootVC = [(UINavigationController *)rootVC topViewController];
  247. }
  248. [rootVC addChildViewController:searchVC];
  249. [rootVC.view addSubview:searchVC.view];
  250. }
  251. #pragma mark ============================>> private 缓存处理
  252. //是否已经缓存到用户信息
  253. -(BOOL)existUserInfo{
  254. NSDictionary * jsonDict = [IOS_NSUSERDEFAULT objectForKey:IOSSDK_USERINFO];
  255. if (jsonDict==nil){
  256. // [PopupView showCusHUDA:@"获取趣动用户数据失败"];
  257. return YES;
  258. }else{
  259. return YES;
  260. }
  261. }
  262. //玩游戏过程中缓存 动作数据 和时间戳
  263. -(void)cacheAllMotionCount:(int)jump_count crouchCount:(int)crouch_count stepCount:(int)step_count{
  264. //游戏持续时间
  265. NSTimeInterval durationTime = [[NSDate date] timeIntervalSinceDate:self.firstTime];//游戏总共经历的时长 秒数
  266. //缓存pilst文件
  267. NSMutableDictionary * bleDict = [NSMutableDictionary new];
  268. [bleDict setObject:[NSNumber numberWithInt:durationTime] forKey:@"duration"];
  269. [bleDict setObject:[NSNumber numberWithInt:jump_count] forKey:@"jump"];
  270. [bleDict setObject:[NSNumber numberWithInt:crouch_count] forKey:@"crouch"];
  271. [bleDict setObject:[NSNumber numberWithInt:step_count] forKey:@"step"];
  272. NSDictionary * dict = [NSDictionary dictionaryWithDictionary:bleDict];
  273. //归档缓存
  274. [IOS_NSUSERDEFAULT setObject:dict forKey:IOSSDK_BLESDKDATA];
  275. [IOS_NSUSERDEFAULT synchronize];
  276. }
  277. #pragma mark ============================>> ios call unity
  278. -(void)bridgingMotionAction:(int)cusid
  279. left:(int)left
  280. right:(int)right{
  281. if (motionHandler!=nil){
  282. motionHandler(cusid,left,right);
  283. }
  284. }
  285. -(void)bridgingStepAction:(int)cusid
  286. leftStatus:(int)leftStatus
  287. rightStatus:(int)rightStatus
  288. leftFrag:(int)leftFrag
  289. rightFrag:(int)rightFrag{
  290. // NSLog(@"deviceHandler ===>> %d %@ %@ %d %d",cusid,name,address,status,electricity);
  291. stepHandler(cusid,leftStatus,rightStatus,leftFrag,rightFrag);
  292. }
  293. -(void)bridgingDeviceAction:(int)cusid
  294. name:(NSString*)name
  295. address:(NSString*)address
  296. status:(int)status
  297. electricity:(int)electricity{
  298. // NSLog(@"deviceHandler ===>> %d %@ %@ %d %d",cusid,name,address,status,electricity);
  299. //
  300. const char * charName =[name UTF8String];
  301. const char * charAddress =[address UTF8String];
  302. if (deviceHandler != nil) {
  303. deviceHandler(cusid,charName,charAddress,status,electricity);
  304. }
  305. }
  306. -(void)bridgingInteraction:(int)cusid
  307. code:(int)code{
  308. //传给unity ---> 旧版游戏未用到
  309. // actionHandler(cusid,code);
  310. }
  311. @end
  312. #pragma mark ============================>> unity call ios
  313. /**
  314. * c语言字符串指针malloc
  315. */
  316. char* MakeStringCopy(const char* string){
  317. if (string == NULL)
  318. return NULL;
  319. char* res = (char*)malloc(strlen(string) + 1);
  320. strcpy(res, string);
  321. return res;
  322. }
  323. /**
  324. * unity call ios 主动接收 函数指针
  325. * MotionHandler 用于ios回调 "蓝牙动作",在👆🏻bridgingMotionAction中实现
  326. */
  327. extern "C" {void PointerMotionHandler(MotionHandler handler){
  328. motionHandler = handler;
  329. NSLog(@"PutIntPtr MotionHandler 指针内存地址 ===>> %p",&motionHandler);
  330. }
  331. }
  332. /**
  333. * unity call ios 主动接收 函数指针
  334. * StepHandler 用于ios回调 "鞋子步频",在👆🏻bridgingStepAction中实现
  335. */
  336. extern "C" { void PointerStepHandler(StepHandler handler){
  337. stepHandler = handler;
  338. NSLog(@"PutIntPtr StepHandler 指针内存地址 ===>> %p",&stepHandler);
  339. }
  340. }
  341. /**
  342. * unity call ios 主动接收 函数指针
  343. * StepHandler 用于ios回调 "鞋子蓝牙状态和电量",在👆🏻bridgingDeviceAction中实现
  344. */
  345. extern "C" {void PointerDeviceHandler(DeviceHandler handler){
  346. deviceHandler = handler;
  347. NSLog(@"PutIntPtr DeviceHandler 指针内存地址 ===>> %p",&deviceHandler);
  348. }
  349. }
  350. /**
  351. * unity call ios 主动接收 函数指针
  352. * ActionHandler 用于ios回调“”蓝牙和游戏大厅交互动作数据”,在👆🏻bridgingMotionAction中实现
  353. */
  354. extern "C" {void PointerActionHandler(ActionHandler handler){
  355. actionHandler = handler;
  356. NSLog(@"PutIntPtr UserFsHandler 指针内存地址 ===>> %p",&actionHandler);
  357. }
  358. }
  359. /**
  360. * unity call ios 主动接收 函数指针
  361. * UserFriendsHandler 用于ios回调 "当前用户好友列表",在👇🏻GetUserFriends中实现
  362. */
  363. extern "C" {void PointerUserFriendsHandler(UserFriendsHandler handler){
  364. userFriendsHandler = handler;
  365. NSLog(@"PutIntPtr UserFsHandler 指针内存地址 ===>> %p",&userFriendsHandler);
  366. }
  367. }
  368. /**
  369. * unity call ios 主动接收 函数指针
  370. * InviteFriendHandler 用于ios回调 "接收好友邀请" ,在👇🏻GetInviteInfo中实现
  371. */
  372. extern "C" {void PointerInviteFriendHandler(InviteFriendHandler handler){
  373. inviteFriendHandler = handler;
  374. NSLog(@"PutIntPtr UserFsHandler 指针内存地址 ===>> %p",&inviteFriendHandler);
  375. }
  376. }
  377. /**
  378. * unity call ios 主动接收 函数指针
  379. * InviteFriendHandler 用于ios回调 "接收好友邀请" ,在👇🏻GetInviteInfo中实现
  380. */
  381. extern "C" {void PointerGetRankHandler(GetRankHandler handler){
  382. getRankHandler = handler;
  383. NSLog(@"PutIntPtr GetRankHandler 指针内存地址 ===>> %p",&getRankHandler);
  384. }
  385. }
  386. /**
  387. * 游戏开始
  388. */
  389. void GameStart(){
  390. NSLog(@"Unity 请求开始游戏 GameStart ===>>");
  391. if ([instance existUserInfo]==NO){
  392. return;
  393. }
  394. [CUS_NOTIFICATIONCENTER postNotificationName:@"gameStartInitData" object:nil];
  395. [HTTPDataProcession gameStart];
  396. }
  397. /**
  398. * 游戏结束
  399. * level: level
  400. * score: score
  401. * record: record
  402. * mode: mode
  403. * opponentId: opponentId
  404. */
  405. void GameEnd(int level, double score, int record, int mode, int opponentId){
  406. NSLog(@"Unity 请求结束游戏 GameEnd ===>> %d %f %d %d %d",level,score,record,mode,opponentId);
  407. if ([instance existUserInfo]==NO){
  408. return;
  409. }
  410. [CUS_NOTIFICATIONCENTER postNotificationName:@"gameEndInitData" object:nil];
  411. [HTTPDataProcession gameEnd];
  412. //上传数据
  413. [HTTPDataProcession postGameRecord:level score:score record:record mode:mode opponentId:opponentId gameEndDataBlock:^(NSString *jsonString){
  414. NSLog(@"IOS_SKD 回调结束游戏请求 GameEnd ===> %@",jsonString);
  415. }];
  416. }
  417. /**
  418. * 搜索设备
  419. * type: 设备ID
  420. */
  421. void SearchDevice(int type){
  422. //unity 有两个search按钮 type 0是主设备 1是副设备
  423. NSDictionary * dict = @{@"type":@(type)};
  424. NSLog(@"Unity 请求搜索设备 SearchDevice ===>> %@",dict);
  425. [CUS_NOTIFICATIONCENTER postNotificationName:@"searchBLEAction" object:nil userInfo:dict];
  426. }
  427. /**
  428. * 链接设备
  429. * type: 设备ID
  430. */
  431. void ConnectDevice(int type){
  432. NSLog(@"Unity 请求链接设备 ConnectDevice ===>> %d",type);
  433. //unity 有两个connect按钮 type 0是第一个玩家 1是第二个玩家
  434. }
  435. /**
  436. * 震动
  437. * type: 设备ID
  438. * duration: 时长 ms [100 .. 1000]
  439. */
  440. void Vibrate(int type,int duration){
  441. //unity 有两个search按钮 type 0是主设备 1是副设备
  442. NSMutableDictionary * obj = [NSMutableDictionary new];
  443. [obj setObject:@(type) forKey:@"type"];
  444. [obj setObject:@(duration) forKey:@"duration"];
  445. NSDictionary * dict = [NSDictionary dictionaryWithDictionary:obj];
  446. NSLog(@"vibrate ===>> %@",dict);
  447. [CUS_NOTIFICATIONCENTER postNotificationName:@"vibrationAction" object:nil userInfo:dict];
  448. }
  449. /**
  450. * unity获取用户好友列表 -->> ios端根据用户token值请求http好友列表 ios将列表json的数组数据传给unity
  451. */
  452. void GetUserFriends(){
  453. NSLog(@"Unity 请求好友列表 GetUserFriends ===>>");
  454. if ([instance existUserInfo]==NO){
  455. return;
  456. }
  457. [HTTPDataProcession getFriendsList:^(int code, const char *jsonString){
  458. NSLog(@"IOS_SKD 回调好友列表 GetUserFriends ===> %s",jsonString);
  459. userFriendsHandler(code,jsonString);//异步回调
  460. }];
  461. }
  462. /**
  463. * unity获取用户信息 -->> 解析从Spotr App的传过来的RL_Seme ios返回json数据给游戏
  464. */
  465. char * GetUserInfoJson(){
  466. NSLog(@"Unity 请求用户信息 GetUserInfoJson ===>>");
  467. if ([instance existUserInfo]==NO){
  468. return MakeStringCopy("");
  469. }else{
  470. NSDictionary * jsonDict = [IOS_NSUSERDEFAULT objectForKey:IOSSDK_USERINFO];
  471. NSDictionary * user = jsonDict[@"user"];
  472. if (user!=nil){
  473. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:user options:NSJSONWritingPrettyPrinted error:nil];
  474. NSString * userJsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  475. const char * cuschart =[userJsonString UTF8String];
  476. NSLog(@"IOS_SKD 回调用户信息 GetUserInfoJson ===> %@",userJsonString);
  477. //要先copy 不然c#调用free的时候回闪退
  478. return MakeStringCopy(cuschart);
  479. }else{
  480. return MakeStringCopy("");
  481. }
  482. }
  483. }
  484. /**
  485. * unity可以主动断开链接
  486. * type: 设备类型 0: 主, 1: 副
  487. */
  488. void DisConnectDevice(int type){
  489. NSLog(@"Unity 请求断开蓝牙链接 disConnectDevice ===>>");
  490. }
  491. /**
  492. * 主动邀请好友
  493. * friendid: 游戏好友ID
  494. */
  495. void InviteFriend(int friendid,char * info){
  496. NSLog(@"Unity 点击邀请好友 InviteFriend ===>>");
  497. if ([instance existUserInfo]==NO){
  498. return;
  499. }
  500. [HTTPDataProcession inviteFriends:friendid info:info inviteDataBlock:^(NSString *jsonString){
  501. // NSLog(@"IOS_SKD 回调点击邀请好友事件 InviteFriend ===>>");
  502. dispatch_async(dispatch_get_main_queue(), ^{
  503. [PopupView showCusHUDA:jsonString];
  504. });
  505. }];
  506. }
  507. /**
  508. * unity界面准备好后可以申请邀请信息,回调在inviteFriendHandler
  509. * friendid: 游戏好友ID
  510. */
  511. void GetInviteInfo(){
  512. NSLog(@"Unity获取好友邀请信息 GetInviteInfo ===>>");
  513. return;
  514. if ([instance existUserInfo]==NO){
  515. return;
  516. }else{
  517. NSDictionary * jsonDict = [IOS_NSUSERDEFAULT objectForKey:IOSSDK_USERINFO];
  518. NSDictionary * invite = jsonDict[@"invite"];
  519. if (invite!=nil){
  520. //user字典 -> 字符串 -> Char
  521. NSDictionary * user = [invite objectForKey:@"user"];
  522. NSData * userData = [NSJSONSerialization dataWithJSONObject:user options:NSJSONWritingPrettyPrinted error:nil];
  523. NSString * userString = [[NSString alloc] initWithData:userData encoding:NSUTF8StringEncoding];
  524. const char * userChar =[userString UTF8String];
  525. //info字符串 -> Char
  526. NSString * info = invite[@"info"];
  527. const char * infoChar =[info UTF8String];
  528. NSLog(@"IOS_SKD 回调好友邀请信息 GetInviteInfo ===>> %s , %s",userChar,infoChar);
  529. inviteFriendHandler(0,userChar,infoChar);
  530. }
  531. }
  532. }
  533. /**
  534. * 投屏
  535. */
  536. void ScreenProjection(){
  537. NSLog(@"Unity 请求投屏事件 ScreenProjection ===>>");
  538. }
  539. /**
  540. * Unity call ios 弹出邀请好友弹窗
  541. * 旧版游戏暂未用到
  542. */
  543. void ShowInviteFriend(int code, char * info){
  544. NSLog(@"Unity 请求打开好友列表弹窗 ShowInviteFriend ===>>");
  545. }
  546. /**
  547. * Unity call ios 获取游戏榜单数据
  548. *if (type == 0) "world" else "friend"
  549. *ios 请求完数据后用 PointerGetRankHandler 回调数据给unity
  550. */
  551. void GetRank(int type){
  552. NSLog(@"Unity 获取榜单列表数据 GetRank ===>> %d",type);
  553. if ([instance existUserInfo]==NO){
  554. return;
  555. }
  556. [HTTPDataProcession GetRank:type rankDataBlock:^(const char *jsonString){
  557. NSLog(@"IOS_SKD 回调 榜单列表数据 GetRank ===>> %d %s",type,jsonString);
  558. getRankHandler(type,jsonString);
  559. }];
  560. }