SDKManager.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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)sdkInitWithDigitMap:(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)sdkLogin {
  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)sdkLogout {
  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)sdkPaymentWithInfo:(NSDictionary *)paymentInfo {
  67. NSDictionary *bayInfo = paymentInfo; // 使用传入的 paymentInfo 字典
  68. [[PullInter bringInstance] hexSlabBlock:bayInfo focusedBlock:^(NSDictionary *params) {
  69. if (self.shouldLog) {
  70. NSLog(@"支付成功:%@", params);
  71. }
  72. [self sendJsonToUnity:params gameObject:"IosReceiverGameObj" methodName:"PaySuccess"];
  73. } failureBlock:^(NSError *error) {
  74. if (self.shouldLog) {
  75. NSLog(@"支付失败:%@", error);
  76. }
  77. [self sendErrorToUnity:error gameObject:"IosReceiverGameObj" methodName:"PayError"];
  78. }];
  79. }
  80. //角色升级上报
  81. - (void)sdkRoleInfo:(NSDictionary *)parRoleInfo {
  82. NSDictionary *roleInfo = parRoleInfo;
  83. [[PullInter bringInstance] georgianDrumBlock:roleInfo focusedBlock:^(NSDictionary *params) {
  84. if (self.shouldLog) {
  85. NSLog(@"角色升级上报成功:%@", params);
  86. }
  87. [self sendJsonToUnity:params gameObject:"IosReceiverGameObj" methodName:"RoleInfoReportSuccess"];
  88. } failureBlock:^(NSError *error) {
  89. if (self.shouldLog) {
  90. NSLog(@"角色升级上报失败:%@", error);
  91. }
  92. [self sendErrorToUnity:error gameObject:"IosReceiverGameObj" methodName:"RoleInfoReportError"];
  93. }];
  94. }
  95. //发送正常消息给unity
  96. - (void)sendJsonToUnity:(NSDictionary *)jsonData
  97. gameObject:(const char *)gameObject
  98. methodName:(const char *)methodName {
  99. NSError *error;
  100. NSData *json = [NSJSONSerialization dataWithJSONObject:jsonData options:0 error:&error];
  101. if (!json) {
  102. if (self.shouldLog) {
  103. NSLog(@"Error converting dictionary to JSON: %@", error.localizedDescription);
  104. }
  105. return;
  106. }
  107. NSString *jsonString = [[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding];
  108. const char *jsonStringC = [jsonString UTF8String];
  109. UnitySendMessage(gameObject, methodName, jsonStringC);
  110. }
  111. //发送报错消息给unity
  112. - (void)sendErrorToUnity:(NSError *)error gameObject:(const char *)gameObject methodName:(const char *)methodName {
  113. NSDictionary *errorInfo = @{
  114. @"error_code" : @(error.code),
  115. @"error_message" : error.localizedDescription
  116. // You can add more details from NSError if needed
  117. };
  118. NSError *jsonError;
  119. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:errorInfo options:0 error:&jsonError];
  120. if (!jsonData) {
  121. if (self.shouldLog) {
  122. NSLog(@"Error converting error info to JSON: %@", jsonError.localizedDescription);
  123. }
  124. return;
  125. }
  126. NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  127. const char *jsonStringC = [jsonString UTF8String];
  128. UnitySendMessage(gameObject, methodName, jsonStringC);
  129. }
  130. @end