123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- #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)InitWithDigitMap:(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)Login {
- [[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)Logout {
- [[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)PaymentWithJson:(NSString *)paymentJson {
- NSData *jsonData = [paymentJson dataUsingEncoding:NSUTF8StringEncoding];
- NSError *jsonError;
- NSDictionary *paymentInfo = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&jsonError];
-
- if (jsonError) {
- NSLog(@"JSON解析失败: %@", jsonError);
- [self sendErrorToUnity:jsonError gameObject:"IosReceiverGameObj" methodName:"RoleInfoReportError"];
- return;
- }
-
- [[PullInter bringInstance] hexSlabBlock:paymentInfo 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)RoleInfoWithJson:(NSString *)roleJson {
- NSData *jsonData = [roleJson dataUsingEncoding:NSUTF8StringEncoding];
- NSError *jsonError;
- NSDictionary *roleInfo = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&jsonError];
-
- if (jsonError) {
- NSLog(@"JSON解析失败: %@", jsonError);
- [self sendErrorToUnity:jsonError gameObject:"IosReceiverGameObj" methodName:"RoleInfoReportError"];
- return;
- }
-
- [[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
|