SDKManager.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #import "SDKManager.h"
  2. #import <StraightPush/StraightPush.h> // douyou SDK
  3. #include "UnityInterface.h" // Example header file where UnitySendMessage is declared
  4. @implementation SDKManager
  5. //单例构造当前类
  6. + (instancetype)sharedInstance {
  7. static SDKManager *sharedInstance = nil;
  8. static dispatch_once_t onceToken;
  9. dispatch_once(&onceToken, ^{
  10. sharedInstance = [[SDKManager alloc] init];
  11. sharedInstance.shouldLog = YES; // 初始值为 YES 或者根据需求设定
  12. });
  13. return sharedInstance;
  14. }
  15. //实例化sdk
  16. - (void)InitWithDigitMap:(NSString *)gameId ring:(NSString *)adId shouldLog:(BOOL)shouldLog{
  17. if (shouldLog != self.shouldLog) {
  18. self.shouldLog = shouldLog;
  19. }
  20. [[PullInter bringInstance] ownDigitMap:gameId ring:adId focusedBlock:^(NSDictionary *params) {
  21. if (self.shouldLog) {
  22. NSLog(@"初始化成功:%@", params);
  23. }
  24. [self sendJsonToUnity:params gameObject:"IosReceiverGameObj" methodName:"InitSuccess"];
  25. } failureBlock:^(NSError *error) {
  26. if (self.shouldLog) {
  27. NSLog(@"初始化失败:%@", error);
  28. }
  29. [self sendErrorToUnity:error gameObject:"IosReceiverGameObj" methodName:"InitError"];
  30. }];
  31. }
  32. //调起登陆
  33. - (void)Login {
  34. [[PullInter bringInstance] zipBannerBlock:^(NSDictionary *params) {
  35. if (self.shouldLog) {
  36. NSLog(@"登录成功:%@", params);
  37. }
  38. [self sendJsonToUnity:params gameObject:"IosReceiverGameObj" methodName:"LoginSuccess"];
  39. } failureBlock:^(NSError *error) {
  40. if (self.shouldLog) {
  41. NSLog(@"登录失败:%@", error);
  42. }
  43. [self sendErrorToUnity:error gameObject:"IosReceiverGameObj" methodName:"LoginError"];
  44. } forkDogFixGetBlock:^(NSDictionary *params) {
  45. if (self.shouldLog) {
  46. NSLog(@"切换账号回调");
  47. }
  48. [self sendJsonToUnity:params gameObject:"IosReceiverGameObj" methodName:"SwitchAccounts"];
  49. }];
  50. }
  51. //退出登陆
  52. - (void)Logout {
  53. [[PullInter bringInstance] sortWetTipBlock:^(NSDictionary *params) {
  54. if (self.shouldLog) {
  55. NSLog(@"登出成功%@", params);
  56. }
  57. [self sendJsonToUnity:params gameObject:"IosReceiverGameObj" methodName:"LogoutSuccess"];
  58. } failureBlock:^(NSError *error) {
  59. if (self.shouldLog) {
  60. NSLog(@"登出失败:%@", error);
  61. }
  62. [self sendErrorToUnity:error gameObject:"IosReceiverGameObj" methodName:"LogoutError"];
  63. }];
  64. }
  65. // 调起支付
  66. - (void)PaymentWithJson:(NSString *)paymentJson {
  67. NSData *jsonData = [paymentJson dataUsingEncoding:NSUTF8StringEncoding];
  68. NSError *jsonError;
  69. NSDictionary *paymentInfo = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&jsonError];
  70. if (jsonError) {
  71. NSLog(@"JSON解析失败: %@", jsonError);
  72. [self sendErrorToUnity:jsonError gameObject:"IosReceiverGameObj" methodName:"RoleInfoReportError"];
  73. return;
  74. }
  75. [[PullInter bringInstance] hexSlabBlock:paymentInfo focusedBlock:^(NSDictionary *params) {
  76. if (self.shouldLog) {
  77. NSLog(@"支付成功:%@", params);
  78. }
  79. [self sendJsonToUnity:params gameObject:"IosReceiverGameObj" methodName:"PaySuccess"];
  80. } failureBlock:^(NSError *error) {
  81. if (self.shouldLog) {
  82. NSLog(@"支付失败:%@", error);
  83. }
  84. [self sendErrorToUnity:error gameObject:"IosReceiverGameObj" methodName:"PayError"];
  85. }];
  86. }
  87. // 角色升级上报
  88. - (void)RoleInfoWithJson:(NSString *)roleJson {
  89. NSData *jsonData = [roleJson dataUsingEncoding:NSUTF8StringEncoding];
  90. NSError *jsonError;
  91. NSDictionary *roleInfo = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&jsonError];
  92. if (jsonError) {
  93. NSLog(@"JSON解析失败: %@", jsonError);
  94. [self sendErrorToUnity:jsonError gameObject:"IosReceiverGameObj" methodName:"RoleInfoReportError"];
  95. return;
  96. }
  97. [[PullInter bringInstance] georgianDrumBlock:roleInfo focusedBlock:^(NSDictionary *params) {
  98. if (self.shouldLog) {
  99. NSLog(@"角色升级上报成功:%@", params);
  100. }
  101. [self sendJsonToUnity:params gameObject:"IosReceiverGameObj" methodName:"RoleInfoReportSuccess"];
  102. } failureBlock:^(NSError *error) {
  103. if (self.shouldLog) {
  104. NSLog(@"角色升级上报失败:%@", error);
  105. }
  106. [self sendErrorToUnity:error gameObject:"IosReceiverGameObj" methodName:"RoleInfoReportError"];
  107. }];
  108. }
  109. //发送正常消息给unity
  110. - (void)sendJsonToUnity:(NSDictionary *)jsonData
  111. gameObject:(const char *)gameObject
  112. methodName:(const char *)methodName {
  113. NSError *error;
  114. NSData *json = [NSJSONSerialization dataWithJSONObject:jsonData options:0 error:&error];
  115. if (!json) {
  116. if (self.shouldLog) {
  117. NSLog(@"Error converting dictionary to JSON: %@", error.localizedDescription);
  118. }
  119. return;
  120. }
  121. NSString *jsonString = [[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding];
  122. const char *jsonStringC = [jsonString UTF8String];
  123. UnitySendMessage(gameObject, methodName, jsonStringC);
  124. }
  125. //发送报错消息给unity
  126. - (void)sendErrorToUnity:(NSError *)error gameObject:(const char *)gameObject methodName:(const char *)methodName {
  127. NSDictionary *errorInfo = @{
  128. @"error_code" : @(error.code),
  129. @"error_message" : error.localizedDescription
  130. // You can add more details from NSError if needed
  131. };
  132. NSError *jsonError;
  133. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:errorInfo options:0 error:&jsonError];
  134. if (!jsonData) {
  135. if (self.shouldLog) {
  136. NSLog(@"Error converting error info to JSON: %@", jsonError.localizedDescription);
  137. }
  138. return;
  139. }
  140. NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  141. const char *jsonStringC = [jsonString UTF8String];
  142. UnitySendMessage(gameObject, methodName, jsonStringC);
  143. }
  144. @end