hch
2025-07-18 2a011653190c36e6fb1f790b3819a1d6b0744aef
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#import "Enkds.h"
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
#import "SystemConfiguration/CaptiveNetwork.h"
 
@implementation Enkds
 
static int cClassBattery = -1;
static int cZhuangtaiBattery = -1;
static NSString* cYingyongID;
static NSString* cYouxiID;
static NSString* sYonghuMing;
static NSThread* _xianchengThd;
 
 
 
-(id)init
{
     self = [super init];
    
     m_jiaWKWebView = [[WKWebView alloc] initWithFrame:CGRectZero];
     NSURL *dizhi = [NSURL URLWithString:@"www.google.com"];
     NSURLRequest *request = [NSURLRequest requestWithURL:dizhi
                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                           timeoutInterval:8.f];
     [m_jiaWKWebView loadRequest:request];
 
     return self;
}
 
-(void) Init
{
    UIDevice *_device = [UIDevice currentDevice];
    _device.batteryMonitoringEnabled = true;
    
    NSString* _uniqueID = @"ddgg";
    //获取IDFA
    NSString* _idfa = @"acd";
    
    // 取得UserAgent
    [m_jiaWKWebView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id _Nullable userAgent, NSError * _Nullable error) {
 
        NSLog(@"%@", userAgent);
        NSString* _userAgent = [NSString stringWithFormat:@"%@", userAgent];
        // 发送设备信息
        NSMutableDictionary *d_i_ct = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                      _uniqueID,@"unique_id",
                                      _idfa,@"android_id",
                                      [NSNumber numberWithInt:STOUni_DeviceInfo],@"code",
                                      _userAgent,@"userAgent",nil];
        [self SendMessageToUnity:d_i_ct];
        }];
    
    
    [TUYESDK sharedGameSDK] .sdkDelegate = (id<DelegateTUYESDK>)self;
    UIViewController *view = UnityGetGLViewController();
    [[TUYESDK sharedGameSDK] initSDKwithViewController:view gameId:1141 appId:93 channelId:332];
}
 
 
- (void)smpcQpLogoutResult:(NSNotification *)notify {
    NSLog(@"sdk请求退出");
    //注销成功
    NSDictionary *d_i_ct = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:STOUni_EnkdsLogoutOk], @"code", nil];
    [self SendMessageToUnity:d_i_ct];
    
}
 
-(void)SendMessageToUnity:(NSDictionary*)dict
{
    BOOL _result = [NSJSONSerialization isValidJSONObject:dict];
    if(_result)
    {
        NSData* _jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:NULL];
        UnitySendMessage(FUNC_NAME_GAME, FUNCTION_NAME,
                         [[[NSString alloc] initWithData:_jsonData encoding:NSUTF8StringEncoding] UTF8String]);
    }
}
 
-(void) BatteryThread {
    while (TRUE)
    {
        [NSThread sleepForTimeInterval:1];
        
        UIDevice *_device = [UIDevice currentDevice];
        
        int _state = [_device batteryState];
        if(_state != cZhuangtaiBattery){
            cZhuangtaiBattery = _state;
            NSDictionary *d_i_ct = [NSDictionary dictionaryWithObjectsAndKeys:
                                   [NSNumber numberWithInt:cZhuangtaiBattery], @"status",
                                   [NSNumber numberWithInt:STOUni_BatteryCharging], @"code", nil];
            
            [self SendMessageToUnity:d_i_ct];
        }
        
        int _level = (int)([_device batteryLevel] * 100);
        if(_level != cClassBattery){
            cClassBattery = _level;
            NSDictionary *d_i_ct = [NSDictionary dictionaryWithObjectsAndKeys:
                                   [NSNumber numberWithInt:cClassBattery], @"level",
                                   [NSNumber numberWithInt:STOUni_BatteryLevel], @"code", nil];
            
            [self SendMessageToUnity:d_i_ct];
        }
    }
}
 
-(void) OpenURL:(NSString*) url {
    
    if(m_jiaWKWebView != NULL) {
        [self btnClick:NULL];
        return;
    }
    
    CGRect b_ounds = UnityGetMainWindow().bounds;
    
    float s_cale = b_ounds.size.width / (1300 + 34);
    
    float w_idth = (900 + 82) * s_cale;
    float h_eight = (500 + 60) * s_cale;
    
    float _offsetX = (b_ounds.size.width - w_idth) * .5f;
    float _offsetY = (b_ounds.size.height - h_eight) * .5f;
    
    m_jiaWKWebView = [[WKWebView alloc] initWithFrame:CGRectMake(_offsetX, _offsetY, w_idth, h_eight)];
    m_jiaWKWebView.UIDelegate = self;
    m_jiaWKWebView.navigationDelegate = self ;
    m_jiaWKWebView.scrollView.scrollEnabled = YES;
    m_jiaWKWebView.scrollView.bounces = NO;
    NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    [m_jiaWKWebView loadRequest:request];
    
    
}
 
-(void) btnClick:(UIButton*)sender {
    [c_loseBtn removeFromSuperview];
    [c_loseBtn removeTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    c_loseBtn = NULL;
    
    NSURLCache *_cache = [NSURLCache sharedURLCache];
    [_cache removeAllCachedResponses];
    [_cache setDiskCapacity:0];
    [_cache setMemoryCapacity:0];
    
    [m_jiaWKWebView removeFromSuperview];
    m_jiaWKWebView = NULL;
}
 
 
//-(void)webViewDidFinishLoad:(WKWebView *)webView{
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
    
    CGRect _bounds = UnityGetMainWindow().bounds;
    
    float _scale = _bounds.size.width / 1334;
    
    float _width = 982 * _scale;
    float _height = 560 * _scale;
    
    c_loseBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [c_loseBtn setFrame:CGRectMake(_width - 600 * _scale, _height - 86 * _scale, 360 * _scale, 72 * _scale)];
    [c_loseBtn setTitle:@"OK" forState:UIControlStateNormal];
    
    NSString *_bundlePath = [[NSBundle mainBundle] pathForResource:@"Enkds.bundle" ofType:nil];
    NSBundle *_bundle = [NSBundle bundleWithPath:_bundlePath];
    NSString *pic1Path = [_bundle pathForResource:@"anniu.png" ofType:nil];
    
    [c_loseBtn setBackgroundImage:[UIImage imageWithContentsOfFile:pic1Path] forState:UIControlStateNormal];
    [c_loseBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [c_loseBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [m_jiaWKWebView addSubview:c_loseBtn];
    
    UIView *view = UnityGetGLViewController().view;
    [view addSubview:m_jiaWKWebView];
}
 
-(void)YngameSDKInit:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSLog(@"初始化成功");
    
    [[TUYESDK sharedGameSDK] application:application
                  didFinishLaunchingWithOptions:launchOptions];
    
}
 
 
#pragma mark - 回调结果
- (void)onInitSuccess
{
    NSLog(@"\n***************初始化成功****************");
    NSDictionary *d_i_ct = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:STOUni_EnkdsInitOk], @"code", nil];
    [self SendMessageToUnity:d_i_ct];
    
    NSMutableDictionary *d_i_ct1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                  [NSNumber numberWithInt:STOUni_SdkInitComplete], @"code",
                                  @"hyenglish", @"channelPlatform", nil];
    [self SendMessageToUnity:d_i_ct1];
}
- (void)onInitFail:(NSError *)kerror
{
    NSDictionary *d_i_ct = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:STOUni_EnkdsInitFail], @"code", nil];
    [self SendMessageToUnity:d_i_ct];
}
 
 
- (void)onLoginSuccess:(id )user
{
 
    NSString *uid = user[@"user_id"];
    NSString *gameUID = [NSString stringWithFormat:@"%@@1", uid];
 
    NSLog(@" 登陆参数:「%@ %@ %@」", uid, user[@"timestamp"], user[@"sign"]);
    
    NSDictionary *d_i_ct = [NSDictionary dictionaryWithObjectsAndKeys:
                           [NSDictionary dictionaryWithObjectsAndKeys:user[@"sign"], @"token",
                            uid, @"userName",
                            user[@"timestamp"], @"token_expire",
                            gameUID, @"account", nil],@"info",
                           [NSNumber numberWithInt:STOUni_EnkdsLoginOk], @"code", nil];
    [self SendMessageToUnity:d_i_ct];
}
 
- (void)onLoginFail:(NSError *)kerror
{
    //self.resultLabel.text = [NSString stringWithFormat:@"登录失败\nerror:%@",kerror.localizedDescription];
    NSDictionary *d_i_ct = [NSDictionary dictionaryWithObjectsAndKeys:
                           [NSNumber numberWithInt:STOUni_EnkdsLoginFail], @"code", nil];
    [self SendMessageToUnity:d_i_ct];
}
 
- (void)onLogout
{
    NSLog(@"注销");
    //self.resultLabel.text = @"注销成功";
    NSDictionary *d_i_ct = [NSDictionary dictionaryWithObjectsAndKeys:
                           [NSNumber numberWithInt:STOUni_EnkdsLogoutOk], @"code", nil];
    [self SendMessageToUnity:d_i_ct];
}
- (void)onPaySuccess{
    NSLog(@"充值调用返回");
    NSDictionary *d_i_ct = [NSDictionary dictionaryWithObjectsAndKeys:
                           [NSNumber numberWithInt:STOUni_EnkdsPayOk], @"code", nil];
    [self SendMessageToUnity:d_i_ct];
}
 
 
-(id) APNativeJSONObject:(NSData *)data{
    if (!data) {
        return nil;
    }
    
    NSError *error = nil;
    id retId = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    
    if (error) {
        NSLog(@"%s trans data to obj with error: %@", __func__, error);
        return nil;
    }
    
    return retId;
}
 
-(void)HandleGameMessage:(NSString *)json {
    
    NSLog(@"收到Unity发来的消息 => %@", json);
    
    NSData *_jsonData = [json dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *d_i_ct = [self APNativeJSONObject:_jsonData];
    
    switch([[d_i_ct objectForKey:@"code"] intValue]){
        case UTOSDK_Init:
            cYingyongID = d_i_ct[@"appID"];
            cYouxiID = d_i_ct[@"gameID"];
            [self Init];
 
            break;
        case UTOSDK_CopyContent:
            [UIPasteboard generalPasteboard].string = d_i_ct[@"content"];
            break;
        case UTOSDK_EnkdsjInit:
            break;
        case UTOSDK_MakeKeyAndVisible:
        {
            UIWindow * _window = [[UIApplication sharedApplication].delegate window];
            if (_window != nil && _window.rootViewController != nil) {
                [_window makeKeyAndVisible];
            }
        }
            break;
        case UTOSDK_OpenWebView:
            [self OpenURL:d_i_ct[@"url"]];
            break;
        case UTOSDK_EnkdsjLogin:
        {
            [[TUYESDK sharedGameSDK] login];
        }
            break;
        case UTOSDK_EnkdsjLogout:
        {
            [[TUYESDK sharedGameSDK] logout];
        }
            break;
        case UTOSDK_CreateRole:
        {
            [[TUYESDK sharedGameSDK] roleCreateWithRoleId:[NSString stringWithFormat:@"%@",[d_i_ct objectForKey:@"roleID"]] roleName:[NSString stringWithFormat:@"%@",[d_i_ct objectForKey:@"roleName"]] roleLevel:[NSString stringWithFormat:@"%@",[d_i_ct objectForKey:@"level"]] serverName:[NSString stringWithFormat:@"%@",[d_i_ct objectForKey:@"serverName"]] serverId:[NSString stringWithFormat:@"%@",[d_i_ct objectForKey:@"sid"]] userMoney:[NSString stringWithFormat:@"%@",[d_i_ct objectForKey:@"money"]] vipLevel:[NSString stringWithFormat:@"%@",[d_i_ct objectForKey:@"vipLevel"]]];
            break;
        }
        case UTOSDK_RoleLogin:
        {
            // 更新角色信息
            [[TUYESDK sharedGameSDK] roleLoginWithRoleId:[NSString stringWithFormat:@"%@",[d_i_ct objectForKey:@"roleID"]] roleName:[NSString stringWithFormat:@"%@",[d_i_ct objectForKey:@"roleName"]] roleLevel:[NSString stringWithFormat:@"%@",[d_i_ct objectForKey:@"level"]] serverName:[NSString stringWithFormat:@"%@",[d_i_ct objectForKey:@"serverName"]] serverId:[NSString stringWithFormat:@"%@",[d_i_ct objectForKey:@"sid"]] userMoney:[NSString stringWithFormat:@"%@",[d_i_ct objectForKey:@"money"]] vipLevel:[NSString stringWithFormat:@"%@",[d_i_ct objectForKey:@"vipLevel"]]];
            break;
        }
            
        case UTOSDK_RoleLevelUp:
        {
            // 更新角色信息
            [[TUYESDK sharedGameSDK] roleLevelUpWithRoleId:[NSString stringWithFormat:@"%@",[d_i_ct objectForKey:@"roleID"]] roleName:[NSString stringWithFormat:@"%@",[d_i_ct objectForKey:@"roleName"]] roleLevel:[NSString stringWithFormat:@"%@",[d_i_ct objectForKey:@"level"]] serverName:[NSString stringWithFormat:@"%@",[d_i_ct objectForKey:@"serverName"]] serverId:[NSString stringWithFormat:@"%@",[d_i_ct objectForKey:@"sid"]] userMoney:[NSString stringWithFormat:@"%@",[d_i_ct objectForKey:@"money"]] vipLevel:[NSString stringWithFormat:@"%@",[d_i_ct objectForKey:@"vipLevel"]]];
            break;
        }
        case UTOSDK_EnkdsjPay:
        {
 
            NSString *extra = [NSString stringWithFormat:@"1_%@_%@_%@", [NSString stringWithFormat:@"%@", [d_i_ct objectForKey:@"orderId"]],
                               [NSString stringWithFormat:@"%@",[d_i_ct objectForKey:@"sid"]],
                               [NSString stringWithFormat:@"%@", [d_i_ct objectForKey:@"cpInfo"]]];
            
            [[TUYESDK sharedGameSDK] IAPWithProductId:[NSString stringWithFormat:@"%@", [d_i_ct objectForKey:@"storeOrderInfo"]] productDesc:[NSString stringWithFormat:@"%@", [d_i_ct objectForKey:@"title"]] amount:[NSString stringWithFormat:@"%@", [d_i_ct objectForKey:@"mount"]] cpOrderId:[NSString stringWithFormat:@"%@", [d_i_ct objectForKey:@"orderId"]] callBackUrl:@"Pxrv%3A%3EW%3B%2A%2A%2B%28%2FE7cuz8w9PGOp5%60%23%3Bd.AGQ%2CYI%5E_N5%3EPsDXi6YTE%2F%2B14~%2F%40%7C%20%5D_52." callBackInfo:extra roleId:[NSString stringWithFormat:@"%@",[d_i_ct objectForKey:@"roleID"]] roleName:[NSString stringWithFormat:@"%@",[d_i_ct objectForKey:@"roleName"]] roleLevel:[NSString stringWithFormat:@"%@",[d_i_ct objectForKey:@"level"]] serverId:[NSString stringWithFormat:@"%@",[d_i_ct objectForKey:@"sid"]] serverName:[NSString stringWithFormat:@"%@",[d_i_ct objectForKey:@"serverName"]]];
            
            break;
        }
        case UTOSDK_BatteryListenStart:
            if(_xianchengThd == NULL)
            {
                _xianchengThd = [[NSThread alloc] initWithTarget:self selector:@selector(BatteryThread) object:nil];
                [_xianchengThd setName:@"BatteryCheck"];
            }
            if(![_xianchengThd isExecuting])
            {
                [_xianchengThd start];
            }
            break;
        case UTOSDK_BatteryListenStop:
            break;
        case UTOSDK_ShareToFaceBook:
        {
            [[TUYESDK sharedGameSDK] shareUrlWithResult:^(BOOL isSuccess) {
                NSLog(@"%@%@",@"facebookShareURL ",isSuccess?@"YES":@"NO");
            }];
            
            NSDictionary *d_i_ct = [NSDictionary dictionaryWithObjectsAndKeys:
                                   [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"state", nil],@"info",
                                   [NSNumber numberWithInt:STOUni_FBShareResult], @"code", nil];
            [self SendMessageToUnity:d_i_ct];
            break;
        }
 
        case UTOSDK_GotoPingfen:
            [[TUYESDK sharedGameSDK] appStoreStar];
            break;
        case UTOSDK_FansHouse:
            [[TUYESDK sharedGameSDK] facebookFansHome];
            break;
        case UTOSDK_GotoShop:
            [[TUYESDK sharedGameSDK] appStore];
            break;
        case UTOSDK_TrackEvent:
            //isRepeat:NO仅上报一次,YES允许重复上报(一般看运营要求)
            [[TUYESDK sharedGameSDK] reportTrackEvent:[NSString stringWithFormat:@"%@",[d_i_ct objectForKey:@"eventName"]] isRepeat:[[d_i_ct objectForKey:@"eventName"] boolValue]];
            break;
    }
}
@end