IOSPlatformSDK.mm 25 KB

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