|
@@ -0,0 +1,145 @@
|
|
|
+#import "SDKManager.h"
|
|
|
+#import <StraightPush/StraightPush.h> // douyou SDK
|
|
|
+#include "UnityInterface.h" // Example header file where UnitySendMessage is declared
|
|
|
+
|
|
|
+@implementation SDKManager
|
|
|
+
|
|
|
+//单例构造当前类
|
|
|
++ (instancetype)sharedInstance {
|
|
|
+ static SDKManager *sharedInstance = nil;
|
|
|
+ static dispatch_once_t onceToken;
|
|
|
+ dispatch_once(&onceToken, ^{
|
|
|
+ sharedInstance = [[SDKManager alloc] init];
|
|
|
+ sharedInstance.shouldLog = YES; // 初始值为 YES 或者根据需求设定
|
|
|
+ });
|
|
|
+ return sharedInstance;
|
|
|
+}
|
|
|
+
|
|
|
+//实例化sdk
|
|
|
+- (void)sdkInitWithDigitMap:(NSString *)gameId ring:(NSString *)adId shouldLog:(BOOL)shouldLog{
|
|
|
+ if (shouldLog != self.shouldLog) {
|
|
|
+ self.shouldLog = shouldLog;
|
|
|
+ }
|
|
|
+ [[PullInter bringInstance] ownDigitMap:gameId ring:adId focusedBlock:^(NSDictionary *params) {
|
|
|
+ if (self.shouldLog) {
|
|
|
+ NSLog(@"初始化成功:%@", params);
|
|
|
+ }
|
|
|
+ [self sendJsonToUnity:params gameObject:"IosReceiverGameObj" methodName:"InitSuccess"];
|
|
|
+ } failureBlock:^(NSError *error) {
|
|
|
+ if (self.shouldLog) {
|
|
|
+ NSLog(@"初始化失败:%@", error);
|
|
|
+ }
|
|
|
+ [self sendErrorToUnity:error gameObject:"IosReceiverGameObj" methodName:"InitError"];
|
|
|
+ }];
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+//调起登陆
|
|
|
+- (void)sdkLogin {
|
|
|
+ [[PullInter bringInstance] zipBannerBlock:^(NSDictionary *params) {
|
|
|
+ if (self.shouldLog) {
|
|
|
+ NSLog(@"登录成功:%@", params);
|
|
|
+ }
|
|
|
+ [self sendJsonToUnity:params gameObject:"IosReceiverGameObj" methodName:"LoginSuccess"];
|
|
|
+ } failureBlock:^(NSError *error) {
|
|
|
+ if (self.shouldLog) {
|
|
|
+ NSLog(@"登录失败:%@", error);
|
|
|
+ }
|
|
|
+ [self sendErrorToUnity:error gameObject:"IosReceiverGameObj" methodName:"LoginError"];
|
|
|
+ } forkDogFixGetBlock:^(NSDictionary *params) {
|
|
|
+ if (self.shouldLog) {
|
|
|
+ NSLog(@"切换账号回调");
|
|
|
+ }
|
|
|
+ [self sendJsonToUnity:params gameObject:"IosReceiverGameObj" methodName:"SwitchAccounts"];
|
|
|
+ }];
|
|
|
+}
|
|
|
+
|
|
|
+//退出登陆
|
|
|
+- (void)sdkLogout {
|
|
|
+ [[PullInter bringInstance] sortWetTipBlock:^(NSDictionary *params) {
|
|
|
+ if (self.shouldLog) {
|
|
|
+ NSLog(@"登出成功%@", params);
|
|
|
+ }
|
|
|
+ [self sendJsonToUnity:params gameObject:"IosReceiverGameObj" methodName:"LogoutSuccess"];
|
|
|
+ } failureBlock:^(NSError *error) {
|
|
|
+ if (self.shouldLog) {
|
|
|
+ NSLog(@"登出失败:%@", error);
|
|
|
+ }
|
|
|
+ [self sendErrorToUnity:error gameObject:"IosReceiverGameObj" methodName:"LogoutError"];
|
|
|
+ }];
|
|
|
+}
|
|
|
+
|
|
|
+//调起支付
|
|
|
+- (void)sdkPaymentWithInfo:(NSDictionary *)paymentInfo {
|
|
|
+ NSDictionary *bayInfo = paymentInfo; // 使用传入的 paymentInfo 字典
|
|
|
+ [[PullInter bringInstance] hexSlabBlock:bayInfo focusedBlock:^(NSDictionary *params) {
|
|
|
+ if (self.shouldLog) {
|
|
|
+ NSLog(@"支付成功:%@", params);
|
|
|
+ }
|
|
|
+ [self sendJsonToUnity:params gameObject:"IosReceiverGameObj" methodName:"PaySuccess"];
|
|
|
+ } failureBlock:^(NSError *error) {
|
|
|
+ if (self.shouldLog) {
|
|
|
+ NSLog(@"支付失败:%@", error);
|
|
|
+ }
|
|
|
+ [self sendErrorToUnity:error gameObject:"IosReceiverGameObj" methodName:"PayError"];
|
|
|
+ }];
|
|
|
+}
|
|
|
+
|
|
|
+//角色升级上报
|
|
|
+- (void)sdkRoleInfo:(NSDictionary *)parRoleInfo {
|
|
|
+ NSDictionary *roleInfo = parRoleInfo;
|
|
|
+ [[PullInter bringInstance] georgianDrumBlock:roleInfo focusedBlock:^(NSDictionary *params) {
|
|
|
+ if (self.shouldLog) {
|
|
|
+ NSLog(@"角色升级上报成功:%@", params);
|
|
|
+ }
|
|
|
+ [self sendJsonToUnity:params gameObject:"IosReceiverGameObj" methodName:"RoleInfoReportSuccess"];
|
|
|
+ } failureBlock:^(NSError *error) {
|
|
|
+ if (self.shouldLog) {
|
|
|
+ NSLog(@"角色升级上报失败:%@", error);
|
|
|
+ }
|
|
|
+ [self sendErrorToUnity:error gameObject:"IosReceiverGameObj" methodName:"RoleInfoReportError"];
|
|
|
+ }];
|
|
|
+}
|
|
|
+
|
|
|
+//发送正常消息给unity
|
|
|
+- (void)sendJsonToUnity:(NSDictionary *)jsonData
|
|
|
+ gameObject:(const char *)gameObject
|
|
|
+ methodName:(const char *)methodName {
|
|
|
+ NSError *error;
|
|
|
+ NSData *json = [NSJSONSerialization dataWithJSONObject:jsonData options:0 error:&error];
|
|
|
+ if (!json) {
|
|
|
+ if (self.shouldLog) {
|
|
|
+ NSLog(@"Error converting dictionary to JSON: %@", error.localizedDescription);
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ NSString *jsonString = [[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding];
|
|
|
+
|
|
|
+ const char *jsonStringC = [jsonString UTF8String];
|
|
|
+ UnitySendMessage(gameObject, methodName, jsonStringC);
|
|
|
+}
|
|
|
+
|
|
|
+//发送报错消息给unity
|
|
|
+- (void)sendErrorToUnity:(NSError *)error gameObject:(const char *)gameObject methodName:(const char *)methodName {
|
|
|
+ NSDictionary *errorInfo = @{
|
|
|
+ @"error_code" : @(error.code),
|
|
|
+ @"error_message" : error.localizedDescription
|
|
|
+ // You can add more details from NSError if needed
|
|
|
+ };
|
|
|
+
|
|
|
+ NSError *jsonError;
|
|
|
+ NSData *jsonData = [NSJSONSerialization dataWithJSONObject:errorInfo options:0 error:&jsonError];
|
|
|
+ if (!jsonData) {
|
|
|
+ if (self.shouldLog) {
|
|
|
+ NSLog(@"Error converting error info to JSON: %@", jsonError.localizedDescription);
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
|
|
|
+ const char *jsonStringC = [jsonString UTF8String];
|
|
|
+
|
|
|
+ UnitySendMessage(gameObject, methodName, jsonStringC);
|
|
|
+}
|
|
|
+@end
|
|
|
+
|