IOSPlatformSDK.mm 26 KB

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