12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232 |
- //
- // XenonSDK.m
- // XenonSDK
- //
- // Created by SAGESSE on 2019/3/11.
- // Copyright © 2019 SAGESSE. All rights reserved.
- //
- #import "XenonSDK.h"
- #import <Security/Security.h>
- #import <sys/utsname.h>
- #if SDK_HAS_IDFA
- #import <AdSupport/AdSupport.h>
- #endif
- #import "BubbleView.h"
- #import "RootViewController.h"
- #import "NoticeViewController.h"
- #import "UserBindPhoneViewController.h"
- #import "UserLoginFastViewController.h"
- #import "UserLoginPhoneViewController.h"
- #import "UserCenterViewController1.h"
- #import "UserCenterViewController2.h"
- #import "XSUtils.h"
- #import "KDIAPManager.h"
- #import "XSCollector.h"
- #import <objc/message.h>
- @interface XenonSDK ()
- @end
- ///
- /// Game SDK
- ///
- @implementation XenonSDK
- @synthesize user = _user;
- @synthesize token = _token;
- ///
- /// A sington instance of SDK.
- ///
- + (instancetype)sharedSDK {
- static id sharedSDK = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- sharedSDK = XenonSDK.new;
-
- if (XSNetwork.baseURL == nil) {
- XSNetwork.baseURL = @"https://app.funcheergame.com";
-
- //测试地址 @"http://47.56.223.128:9099";
- //国内正式地址 @"https://app.funcheergame.com";
- //国外正式地址 @"https://fapp.funcheergame.com"
- }
- });
- return sharedSDK;
- }
- ///
- /// Init the sdk with application code.
- /// \param parameter The parameter provided by the platform.
- ///
- /// \param complete This closure called when init complete.
- ///
- - (void)initWithParameter:(NSString*)parameter complete:(void (^)(NSError* error))complete {
-
-
- // If init is complete, ignore it this call.
- if (self.configuration != nil) {
- if (complete != nil) {
- complete(nil); // sdk is ready.
- }
- return;
- }
-
- // Parse pre-configuration for parameter.
- NSArray* configs = [parameter componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"|&"]];
- if (configs.count != 4) {
- if (complete != nil) {
- complete([XSNetwork errorWithCode:-1 message:@"参数错误"]); // error -1
- }
- return ;
- }
-
- // Update pre-configuration for parameter.
- self.appId = configs[0];
- self.channelId = configs[1];
- self.adId = configs[2];
- self.adFlag = configs[3];
- // Update system configration for server.
- [XSNetwork configureWithIdentifier:self.adId flags:self.adFlag complete:^(XSConfiguration* object, NSError *error) {
- // If an error occurs, directly callback cp.
- if (error != nil) {
- if (complete != nil) {
- complete(error);
- }
- return;
- }
- // An unknown error.
- if (object.closeState != 0) {
-
- // Display an system notice.
- id qq = self.configuration.fixLinkQQ ?: @"";
- id tel = self.configuration.fixLinkTel ?: @"";
- // Set the update title & contents.
- id contents = [NSString stringWithFormat:@"\n亲爱的玩家您好, 由于服务器紧急维护, 目前游戏暂时无法进入, 给您带来的不便我们深表歉意!\n如果您还有其他问题请与我们联系!\n\n客服QQ: %@\n客服电话: %@", qq, tel];
- UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"系统公告" message:contents preferredStyle:UIAlertControllerStyleAlert];
- [alert addAction:[UIAlertAction actionWithTitle:@"我知道了" style:UIAlertActionStyleDefault handler:nil]];
- self.rootViewController = alert;
- complete([XSNetwork errorWithCode:-8 message:nil]);
- return;
- }
-
- // Setup sdk with server configuration.
- self.configuration = object;
- // Get third-party platform parameters.
- if (self.configuration.thirdPartyPrams.length != 0) {
- NSDictionary* thirdPartyPrams = [NSJSONSerialization JSONObjectWithData:[self.configuration.thirdPartyPrams dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
-
- // If the tracker is turned on, create the object.
- int mask = 0;
- NSString* dps = thirdPartyPrams[@"dp"];
- if (dps.length != 0) {
- sscanf(dps.UTF8String, "%x", &mask);
- if (mask != 0) {
- XSTracker.shared = [XSTracker new];
- XSTracker.shared.mask = mask;
- [XSNetwork trigger:20];
- }
- }
-
- // In the initialization.
- [XSTracker.shared trace:0x010000 parameters:[NSString stringWithFormat:@"%@", NSProcessInfo.processInfo.environment]];
- // If an advertisement parameter is specified, try to parse advertising parameter.
- NSString* adPrams = self.configuration.adPrams;
- if (adPrams.length != 0) {
-
- // Parse advertising parameter.
- [[adPrams componentsSeparatedByString:@"&"] enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
- // Value must be `key=value`
- NSArray* tmp = [obj componentsSeparatedByString:@"="];
- if (tmp.count != 2) {
- return;
- }
-
- if ([tmp[0] isEqualToString:@"td_app_id"]) {
- self.configuration.tdAppId = tmp[1];
- }
- if ([tmp[0] isEqualToString:@"td_game_id"]) {
- self.configuration.tdGameId = tmp[1];
- }
- }];
-
- // If td game id is not provided, the TD sdk is not initialized.
- if (self.configuration.tdAppId.length != 0 && self.configuration.tdGameId.length != 0) {
- // Init for data analzer server.
- XSCollector.shared = [[XSCollector alloc] initWithAppId:self.configuration.tdAppId gameId:self.configuration.tdGameId];
-
- }
- }
- }
-
- [self sdk_checkVersion:^{
- [self sdk_checkNote:^{
- // Check workflow ended.
- [XSTracker.shared trace:0x010001];
-
- // Init sdk complete.
- if (complete != nil)
- complete(nil);
-
- // init workflow ended.
- [XSTracker.shared trace:0x010002];
- }];
- }];
- }];
- }
- ///
- /// Login the sdk.
- /// \param complete This closure called when login complete.
- ///
- - (void)loginWithComplete:(void (^)(id user, NSError* error))complete {
-
- // If init is complete, ignore it this call.
- if (self.configuration == nil) {
- if (complete != nil)
- complete(nil, [XSNetwork errorWithCode:-1 message:@"请先初始化"]); // error -1
-
- //发出登录失败通知. flag=NO
- NSNotification *notification = [NSNotification notificationWithName:@"LoginSuccessNotificaion" object:nil userInfo:@{@"userInfo":@"",@"flag":@"NO"}];
- [[NSNotificationCenter defaultCenter] postNotification:notification];
- return;
- }
-
- [XSTracker.shared trace:0x010010];
- // Unified operation after successful login.
- void (^loginedCallback)(void) = ^{
-
- // Build return data.
- id user = nil;
- if (self.user != nil) {
- user = @{
- @"uid": self.user.uid,
- @"token": self.user.token,
-
- @"userName": self.user.userName ?: @"",
- @"phone": self.user.phone ?: @""
- };
-
- // According to air bubbles.
- if (!self.configuration.isFloatDisable) {
- UIWindow* window = UIApplication.sharedApplication.delegate.window ?: UIApplication.sharedApplication.keyWindow;
- BubbleView* view = [[BubbleView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
- [view.contentView addTarget:self action:@selector(center) forControlEvents:UIControlEventTouchUpInside];
- UITapGestureRecognizer *tapRecognize = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
- [view addGestureRecognizer:tapRecognize];
-
- [window addSubview:view];
- // It's shown in the first one.
- UIView* xmp = [window viewWithTag:-1];
- if (xmp != nil) {
- [window bringSubviewToFront:xmp];
- }
- [self.bubbleView removeFromSuperview];
- self.bubbleView = view;
- }
-
- [XSTracker.shared trace:0x010013];
- }
-
- [XSTracker.shared trace:0x010011];
- if (complete) {
- complete(user, nil);
- }
- [XSTracker.shared trace:0x010012];
- };
-
- // Try automatically login if needed.
- if (self.user != nil) {
-
- [XSNetwork showHudLoading];
- [XSNetwork authWithUser:self.user.uid complete:^(XSUser* user, NSError *error) {
-
- // If the login fails, clear the user information and login again.
- if (user.token.length == 0 || error != nil) {
-
- [XSNetwork showHudFailure:@"登录时效期已过,请重新登录"];
- self.user = nil;
- [self loginWithComplete:complete];
- return;
- }
-
-
- // Update user info.
- XSUser* duser = self.user;
- duser.token = user.token;
- duser.isRegister = false;
- self.user = duser;
-
-
- // Login successful, display HUD.
- [XSNetwork showPrompt:duser.name];
-
- //发出登成功通知.flag=YES
-
- NSDictionary *dict = @{@"userInfo":self.user,@"flag":@"YES"};
- NSNotification *notification = [NSNotification notificationWithName:@"LoginSuccessNotificaion" object:nil userInfo:dict];
- [[NSNotificationCenter defaultCenter] postNotification:notification];
-
- // Check if the user is already connected to the phone.
- // [self sdk_checkPhone:^{
- // loginedCallback();
- // }];
- //绑定手机
- loginedCallback();
- }];
- [XSTracker.shared trace:0x010014];
- return;
- }
-
- // Setup login callback.
- __weak __typeof(self) ws = self;
- self.sdk_loginCallback = ^(XSUser* user) {
-
- [ws setUser:user];
- [ws.rootViewController dismissViewControllerAnimated:YES completion:nil];
-
- // Login successful, display HUD.
-
- //发出登录成功通知.
- NSDictionary *dict = @{@"userInfo":user,@"flag":@"YES"};
- NSNotification *notification = [NSNotification notificationWithName:@"LoginSuccessNotificaion" object:nil userInfo:dict];
- [[NSNotificationCenter defaultCenter] postNotification:notification];
-
- [XSNetwork showPrompt:user.name];
- loginedCallback();
- };
-
-
- // The last time was to log out, so the quick login list is displayed.
- if (XSUser.history.count != 0) {
- //self.rootViewController = [[RootViewController alloc] initWithRootViewController:UserLoginFastViewController.new];
- self.rootViewController = [[RootViewController alloc] initWithRootViewController:UserLoginPhoneViewController.new];
- return;
- }
-
- // To account login
- self.rootViewController = [[RootViewController alloc] initWithRootViewController:UserLoginPhoneViewController.new];
- }
- ///
- /// Logout the sdk.
- /// \param complete This closure called when logout complete.
- ///
- - (void)logoutWithComplete:(void (^)(void))complete {
-
-
- // If init is complete, ignore it this call.
- if (self.configuration == nil) {
- if (complete != nil)
- complete();
- return;
- }
-
- self.user = nil;
- [self.bubbleView removeFromSuperview];
- self.bubbleView = nil;
- //发出退出通知.
- NSNotification *notification = [NSNotification notificationWithName:@"LogoutNotificaion" object:nil userInfo:nil];
- [[NSNotificationCenter defaultCenter] postNotification:notification];
-
- if (complete != nil)
- complete();
-
- [XSTracker.shared trace:0x010020];
-
-
- }
- ///
- /// Logout the game and sdk.
- /// \param handler This closure called when user logout in web view.
- /// \note Warning this handler will retain in until next call this method.
- ///
- - (void)logoutWhenNotification:(void (^)(void))handler {
- self.sdk_logoutCallback = ^(id user) {
-
- if (handler != nil)
- handler();
-
- [XSTracker.shared trace:0x010020];
- };
- }
- ///
- /// Buy a product.
- /// \param parameters Payment request parameters.
- /// \param complete Payment result of the callback, if the apple payment is completed before the callback, other payments will be the official launch of the callback
- ///
- - (void)buy:(NSDictionary*)parameters complete:(void (^)(id order, NSError* error))complete {
-
- // Check paramrters.
- if (self.configuration == nil || self.user == nil) {
- if (complete != nil) {
- complete(nil, [XSNetwork errorWithCode:-1 message:@"请先登录"]); // error -1
- return ;
- }
- }
-
- [XSTracker.shared trace:0x010030];
-
- [XSNetwork showHudLoading];
-
- //
- // [XSNetwork routeWithComplete:^(id object, NSError *error) {
- //
- // if (error != nil) {
- // [XSNetwork showHudFailure:error];
- // if (complete != nil) {
- // complete(nil, error);
- // }
- // return;
- // }
- // Get parmeters.
- XSUser* user = self.user;
- //NSString* path = object[fqsd(@"PREAACs/NCYsJjQuLRkA")];
-
- //#if SDK_HAS_H5_PAYMENT
- //Generate h5 payment parameters.
- // if (path.length != 0) {
- //
- // [XSNetwork hideHud];
- // [XSTracker.shared trace:0x010031];
- //
- // id payments = @{
- // @"cpBillNo": parameters[@"orderId"] ?: @"",
- // @"orderAmount": parameters[@"amount"] ?: @"",
- // @"subject": parameters[@"productName"] ?: @"",
- //
- // @"roleName": parameters[@"roleName"] ?: @"",
- // @"serverId": parameters[@"serverId"] ?: @"",
- // @"extraInfo": parameters[@"extraInfo"] ?: @"",
- //
- // @"phone": @"",
- // @"isTest": @"1",
- // @"orderPlatform": @"0",
- // @"orderType": @"1",
- // @"remark": @"0",
- // @"isApp": @"0",
- //
- // @"uid": user.uid,
- // @"userName": user.userName ?: @""
- // };
- //
- // NSURLComponents* components = [NSURLComponents componentsWithString:path];
- // NSDictionary* json = [XSNetwork bodyWithParameters:payments];
- // NSString* str = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:json options:0 error:nil] encoding:NSUTF8StringEncoding];
- //
- // components.queryItems = @[[NSURLQueryItem queryItemWithName:@"jsonData" value:str],
- // [NSURLQueryItem queryItemWithName:@"v" value:@"1"]];
- //
- // if (components.URL == nil) {
- // if (complete != nil) {
- // complete(nil, [XSNetwork errorWithCode:-1 message:@"后台参数错误"]);
- // }
- // return;
- // }
- //
- // UserCenterViewController* rootViewController = UserCenterViewController.new;
- // rootViewController.custom = components.URL.absoluteString;
- // rootViewController.otherView = self.bubbleView;
- // rootViewController.modalPresentationStyle = UIModalPresentationOverFullScreen;
- // self.rootViewController = rootViewController;
- //
- // // Cannot get the callback state.
- // rootViewController.callback = ^{
- // if (complete != nil) {
- // complete(nil, nil);
- // }
- // };
- // return;
- // }
- //#endif
-
-
-
- //#if SDK_HAS_IAP_PAYMENT
- // Generate iap payment parameters.
- id payments = @{
- @"productCode": parameters[@"productCode"] ?: @"",
- @"isCoin": @"0",
- @"orderPlatform": @"0",
- @"orderType": @"1",
- @"orderAmount": parameters[@"amount"] ?: @"",
- @"isTest": @"0",
- @"subject": parameters[@"productName"] ?: @"",
- @"roleName": parameters[@"roleName"] ?: @"",
- @"cpBillNo": parameters[@"orderId"] ?: @"",
-
- @"remark": @"0",
- @"serverId": parameters[@"serverId"] ?: @"",
- @"extraInfo": parameters[@"extraInfo"] ?: @"",
-
- @"uid": user.uid,
- @"userName": user.userName ?: @""
- };
-
- [XSTracker.shared trace:0x010032];
- [XSNetwork payWithParameters:payments complete:^(id object, NSError *error) {
-
-
- if (error != nil) {
- [XSNetwork showHudFailure:error];
- if (complete != nil) {
- complete(nil, error);
- }
- return;
- }
-
- [XSTracker.shared trace:0x010034];
- [XSNetwork showHudSuccess:@"支付成功"];
-
- if (complete != nil)
- complete(payments, nil);
- }];
- //#endif
- //}];
- }
- ///点击悬浮按钮,
- -(void)handleTap:(UITapGestureRecognizer *)recognizer {
-
- if (self.configuration == nil || self.user == nil) {
- return;
- }
-
- [XSTracker.shared trace:0x010040];
- //0:横屏 1:竖屏
- #define SCREEN_Direction 0
- ///横屏
- #if SCREEN_Direction == 0
- UserCenterViewController1* rootViewController1 = UserCenterViewController1.new;
- rootViewController1.otherView = self.bubbleView;
- rootViewController1.modalPresentationStyle = UIModalPresentationOverFullScreen;
- self.rootViewController = rootViewController1;
- #endif
-
- ///竖屏
- #if SCREEN_Direction == 1
- UserCenterViewController2* rootViewController2 = UserCenterViewController2.new;
- rootViewController2.otherView = self.bubbleView;
- rootViewController2.modalPresentationStyle = UIModalPresentationOverFullScreen;
- self.rootViewController = rootViewController2;
- #endif
-
-
- }
- ///
- /// User Center.
- ///
- - (void)center {
- if (self.configuration == nil || self.user == nil) {
- return;
- }
-
- [XSTracker.shared trace:0x010040];
-
- //0:横屏 1:竖屏
- #define SCREEN_Direction 0
- ///横屏
- #if SCREEN_Direction == 0
- UserCenterViewController1* rootViewController1 = UserCenterViewController1.new;
- rootViewController1.otherView = self.bubbleView;
- rootViewController1.modalPresentationStyle = UIModalPresentationOverFullScreen;
- self.rootViewController = rootViewController1;
- #endif
-
- ///竖屏
- #if SCREEN_Direction == 1
- UserCenterViewController2* rootViewController2 = UserCenterViewController2.new;
- rootViewController2.otherView = self.bubbleView;
- rootViewController2.modalPresentationStyle = UIModalPresentationOverFullScreen;
- self.rootViewController = rootViewController2;
- #endif
-
- }
- /// Report role info.
- /// \param roleName This role name.
- ///
- /// \param level This role current level.
- ///
- /// \param serverId This role current server.
- ///
- - (void)reportWithRoleName:(NSString*)roleName level:(NSInteger)level serverId:(NSString*)serverId {
- if (self.configuration == nil || self.user == nil) {
- return;
- }
- [XSNetwork reportWithUser:self.user.userName Name:roleName level:level server:serverId];
- [XSCollector.shared reportWithAccount:self.user.uid name:roleName level:level server:serverId];
- }
- ///自动登录
- - (void)hAutoWithComplete:(void (^)(id user, NSError* error))complete {
- [XSNetwork showHudLoading];
- //XSUser* user1 = [XSDataCenter valueForKey:@"User.current"];
-
- //PS:通过这种方式获取到的user有值不会为空,其他方法获取的值为空,不准确!!!
- XSUser *user1 = self.user;
-
- //未注册,
- if (user1.uid.length == 0) {
- __weak __typeof(self) ws = self;
- [XSNetwork loginWithComplete:^(id object, NSError *error) {
- if (error != nil) {
- [XSNetwork showHudFailure:error];
- complete(nil, error);
- return;
- }
- [ws setUser:object];
- XenonSDK.sharedSDK.user = object;
- [XSNetwork showHudSuccess:@"登录成功!"];
- complete(object,nil);
- }];
-
- }else{
- //
-
- XenonSDK.sharedSDK.token = user1.token;
- __weak __typeof(self) ws = self;
-
- [XSNetwork authWithUser:user1.uid complete:^(XSUser* object, NSError *error) {
- if (error != nil && ![error.domain isEqual:@"XSNetwork"]) {
- [XSNetwork showHudFailure:error];
- complete(nil,[XSNetwork errorWithCode:-1 message:error.domain]);
- return;
- }
-
- [XSNetwork showHudSuccess:@"登录成功!"];
- if (user1.token.length != 0 && error == nil) {
- // Update user info.
- user1.token = object.token;
- user1.isRegister = NO;
- XenonSDK.sharedSDK.user = user1;
- [ws setUser:user1];
- complete(user1,nil);
- }
- }];
- }
- }
- // MARK: -检查版本更新
- - (void)sdk_checkVersion:(void(^)(void))complete {
-
- // If no configuration file is provided, initialization failed.
- if (self.configuration == nil) {
- complete();
- return;
- }
- // If update cheker is not enabled, ignore it.
- NSURL* url = [NSURL URLWithString:self.configuration.updateUrl];
- if (url == nil || self.configuration.updateState == 0) {
- complete();
- return;
- }
-
- // If the prompt is too frequent, wait.
- // 1 is normal update.
- if (self.configuration.updateState == 1 && !([XSDataCenter doubleForKey:@"verion-later"] < NSDate.new.timeIntervalSince1970)) {
- complete();
- return;
- }
- NoticeViewController* viewController = NoticeViewController.new;
- // Set the update title & contents.
- viewController.title = @"发现新版本";
- viewController.contents = [NSString stringWithFormat:@"亲爱的玩家您好,我们发布了新的版本, 更新到新版本可体验更多新奇有趣的玩法!\n\n最新版本: %@\n当前版本: %@\n\n\n", self.configuration.updateVersion, self.shortVersion];
- // Set the confirm closure.
- [viewController addActionWithName:@"立即升级" handler:^{
-
- // -[UIApplication openURL:]
- ((void(*)(id,SEL,id))objc_msgSend)(UIApplication.sharedApplication, NSSelectorFromString(fqsd(@"p2cAAMrAyrKwq5rIAA==")), url);
- }];
- // Set the cancel closure.
- if (self.configuration.updateState == 1) {
- __weak __typeof(self) ws = self;
- [viewController addActionWithName:@"稍后再说" handler:^{
- // Again 24 hours later.
- [XSDataCenter setDouble:[NSDate dateWithTimeIntervalSinceNow:24*60*60].timeIntervalSince1970 forKey:@"verion-later"];
-
- if (ws.rootViewController == nil) {
- complete();
- return;
- }
-
- // Automatic hidden controller.
- [ws.rootViewController dismissViewControllerAnimated:YES completion:complete];
- }];
- }
- // Show view contorller in key window.
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- self.rootViewController = [[RootViewController alloc] initWithRootViewController:viewController];
- });
- }
- - (void)sdk_checkNote:(void(^)(void))complete {
- // If no configuration file is provided, initialization failed.
- NSURL* url = [NSURL URLWithString:self.configuration.closeUrl];
- if (url == nil || self.configuration.linkQq == 0) {
- complete();
- return ;
- }
-
- // If the announcement is displayed, it is no longer displayed.
- if (self.configuration.linkQq != 2 && [[XSDataCenter stringForKey:@"notice-later"] isEqualToString:url.absoluteString]) {
- complete();
- return;
- }
-
- NoticeViewController* viewController = NoticeViewController.new;
-
- // Set the update title & contents.
- viewController.title = @"系统公告";
- viewController.contents = url;
-
- // Set the confirm closure.
- __weak __typeof(self) ws = self;
- [viewController addActionWithName:@"我知道了" handler:^{
-
- // Never again.
- [XSDataCenter setString:url.absoluteString forKey:@"notice-later"];
-
- if (ws.rootViewController == nil) {
- complete();
- return;
- }
-
- // Automatic hidden controller.
- [ws.rootViewController dismissViewControllerAnimated:YES completion:complete];
- }];
-
- // Show view contorller in key window.
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- self.rootViewController = [[RootViewController alloc] initWithRootViewController:viewController];
- });
- }
- - (void)sdk_checkPhone:(void(^)(void))complete {
-
- // Check if the phone character exists.
- if (self.user.isRegister || self.user.phone.length != 0 || self.configuration.serverStatus == 1) {
- complete();
- return;
- }
-
- // If the prompt is too frequent, wait.
- id key = [NSString stringWithFormat:@"%@-bind-later", self.user.uid];
- if ([XSDataCenter doubleForKey:key] > NSDate.new.timeIntervalSince1970) {
- complete();
- return;
- }
-
- UserBindPhoneViewController* bindViewController = UserBindPhoneViewController.new;
- RootViewController* rooViewController = [[RootViewController alloc] initWithRootViewController:bindViewController];
-
- UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您的帐号存在风险, 为了您的帐号安全建议您绑定手机号。" preferredStyle:UIAlertControllerStyleAlert];
-
-
- // Add confirm action.
- [alert addAction:[UIAlertAction actionWithTitle:@"绑定手机" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
- bindViewController.callback = complete;
- self.rootViewController = rooViewController;
- }]];
- // Add cancel action.
- [alert addAction:[UIAlertAction actionWithTitle:@"稍后提醒" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
-
- // Again 24 hours later.
- [XSDataCenter setDouble:[NSDate dateWithTimeIntervalSinceNow:24*60*60].timeIntervalSince1970 forKey:key];
-
- // Continue.
- complete();
- }]];
-
-
- (void)rooViewController.view; // preload;
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- self.rootViewController = alert;
- });
- // Hide all hud if needed.
- [XSNetwork hideHud];
- }
- ///check In-app Purchase lost list.
- - (void)checkOrderStatus {
- //检查内购掉单.
- [KDIAPManager checkOrderStatus];
- }
- // MARK: -
- - (void)setUser:(XSUser *)user {
-
- _user = user;
- _token = user.token;
-
- // Cache.
- [XSDataCenter setValue:user forKey:@"User.current"];
-
- if (user == nil) {
- return;
- }
-
- // Make a new login history.
- NSMutableSet* ids = [NSMutableSet set];
- NSMutableArray* history = [NSMutableArray array];
-
- user.lastTime = NSDate.new.timeIntervalSince1970;
-
- [ids addObject:user.uid];
- [history addObject:user];
-
- for (XSUser* tmp in XSUser.history) {
- if (![ids containsObject:tmp.uid]) {
- [ids addObject:tmp.uid];
- [history addObject:tmp];
- }
- }
- XSUser.history = history;
- }
- - (XSUser*)user {
- if (_user != nil) {
- return _user;
- }
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- self->_user = [XSDataCenter valueForKey:@"User.current"];
- self->_token = self->_user.token;
- });
- return _user;
- }
- - (void)setRootViewController:(UIViewController *)rootViewController {
- _rootViewController = rootViewController;
- if (rootViewController == nil) {
- return;
- }
- [UIApplication.sharedApplication.keyWindow.rootViewController presentViewController:rootViewController
- animated:YES
- completion:nil];
- }
- /// Permanent preservation of data.
- - (void)setPermanentValue:(NSString*)value forKey:(NSString*)key {
-
- // Set value to user defautls.
- [XSDataCenter setString:value forKey:key];
-
- // Configure the search setting.
- id setting = @{
- // Configure the keychain key type.
- CFBridgingRelease(kSecClass): CFBridgingRelease(kSecClassGenericPassword),
- CFBridgingRelease(kSecAttrAccount): key
- }.mutableCopy;
-
- // Delete old item before add new item
- SecItemDelete(CFBridgingRetain(setting));
-
- // Add new object to search dictionary.
- [setting setObject:[value dataUsingEncoding:NSUTF8StringEncoding] forKey:CFBridgingRelease(kSecValueData)];
-
- // Add item to keychain with the search dictionary
- SecItemAdd(CFBridgingRetain(setting), nil);
- }
- /// Read data for permanent preservationed.
- - (id)permanentValueForKey:(NSString*)key {
-
- // Read data from user defatuls.
- NSString* udid = [XSDataCenter stringForKey:key];
- if (udid.length != 0) {
- return udid;
- }
-
- // Configure the search setting.
- id setting = @{
- // Configure the keychain key type.
- CFBridgingRelease(kSecClass): CFBridgingRelease(kSecClassGenericPassword),
-
- // The class name of the SDK is dynamic.
- CFBridgingRelease(kSecAttrService): NSStringFromClass(self.class),
- CFBridgingRelease(kSecAttrAccount): key,
-
- // Gets result data of data.
- CFBridgingRelease(kSecReturnData): CFBridgingRelease(kCFBooleanTrue)
- };
-
- // Start query the keychain query.
- CFTypeRef result = nil;
- if (SecItemCopyMatching(CFBridgingRetain(setting), &result) == noErr) {
- return nil;
- }
-
- // Convert data to a string
- NSString* value = [[NSString alloc] initWithData:CFBridgingRelease(result) encoding:NSUTF8StringEncoding];
- if (value.length == 0) {
- return nil;
- }
-
- // Set value to user defautls, reduce Keychain access count.
- [XSDataCenter setString:value forKey:key];
-
- return value;
- }
- - (NSString*)advertisingIdentifier {
-
- // Read the data from keychain.
- NSString* advertisingIdentifier = [self permanentValueForKey:@"udid"];
- if (advertisingIdentifier.length != 0) {
- return advertisingIdentifier;
- }
-
- #if SDK_HAS_IDFA
- // Try to obtain idfa.
- ASIdentifierManager* advertisingManager = ASIdentifierManager.sharedManager;
- advertisingIdentifier = advertisingManager.advertisingIdentifier.UUIDString;
-
- if (!advertisingManager.isAdvertisingTrackingEnabled) {
- advertisingIdentifier = [NSString stringWithFormat:@"#%@", NSUUID.new.UUIDString];
- }
- #else
- // Generate an id to replace idfa.
- advertisingIdentifier = [NSString stringWithFormat:@"#%@", NSUUID.new.UUIDString];
- #endif
-
- // Save to keychain to prevent updates.
- [self setPermanentValue:advertisingIdentifier forKey:@"udid"];
-
- return advertisingIdentifier;
- }
- - (NSString*)advertisingVendor {
- // This parameter has been deprecated and must be empty string.
- return @"";
- }
- - (NSBundle*)bundle {
- return [NSBundle bundleForClass:self.class];
- }
- - (NSString*)model {
- struct utsname sinfo;
- uname(&sinfo);
- return [NSString stringWithUTF8String: sinfo.machine];
- }
- /// The sdk verions.
- - (NSString*)version {
- return self.bundle.infoDictionary[@"CFBundleShortVersionString"] ?: @"1.0";
- }
- /// The game verions.
- - (NSString*)shortVersion {
- return NSBundle.mainBundle.infoDictionary[@"CFBundleShortVersionString"] ?: @"1.0";
- }
- ///TalkingData 游戏运营统计
- - (void)TDGameOperationsinitWithAppID:(NSString *)appid ChannelId:(NSString *)channelId {
-
- if (appid.length == 0) {
- NSLog(@"TalkingData 游戏运营统计: appid为空!");
- return;
- }
-
- if (channelId) {
- NSLog(@"TalkingData 游戏运营统计: channelId为空!");
- }
-
- [TalkingDataGA onStart:appid withChannelId:channelId];
-
- }
- -(void)TDregisterWithAccount:(NSString *)account name:(NSString *)name {
-
- [[XSCollector sharedManager] registerWithAccount:account name:name];
- }
- -(void)TDloginWithAccount:(NSString *)account name:(NSString *)name {
-
- [[XSCollector sharedManager] loginWithAccount:account name:name];
- }
- -(void)TDreportWithAccount:(NSString*)account Name:(NSString *)name Age:(int)age Gender:(NSInteger)gender {
- [[XSCollector sharedManager] reportWithAccount:account name:name age:age gender:gender];
- }
- -(void)TDreportWithAccount:(NSString*)account name:(NSString*)name level:(NSInteger)level server:(NSString*)server {
-
- [[XSCollector sharedManager] reportWithAccount:account name:name level:level server:server];
- }
- -(void)TDonChargeRequst:(NSString *)orderId iapId:(NSString *)iapId currencyAmount:(double)currencyAmount currencyType:(NSString *)currencyType virtualCurrencyAmount:(double)virtualCurrencyAmount paymentType:(NSString *)paymentType {
- [[XSCollector sharedManager] onChargeRequst:orderId iapId:iapId currencyAmount:currencyAmount currencyType:currencyType virtualCurrencyAmount:virtualCurrencyAmount paymentType:paymentType];
- }
- - (void)TDonChargeSuccess:(NSString *)orderId {
- [[XSCollector sharedManager] onChargeSuccess:orderId];
- }
- //TD 自定义事件
- -(void)TDonEvent:(NSString *)eventId eventData:(id)parm {
- [[XSCollector sharedManager] onEvent:eventId eventData:parm];
- }
- - (void)TDAnalyticsinitWithAppID:(NSString *)appid ChannelId:(NSString *)channelId backgroundSession:(BOOL)flag {
-
- if (appid.length == 0 ) {
- NSLog(@"【TalkingData】,error=App ID参数为nil");
- return;
- }
-
- if (channelId.length == 0) {
- NSLog(@"【TalkingData】,error=渠道 ID参数为nil");
- }
-
- if (flag) {
- [TalkingData backgroundSessionEnabled]; //获取后台使用时长
- }
- [TalkingData sessionStarted:appid withChannelId:channelId]; //init
- [TalkingData setExceptionReportEnabled:YES]; //自动获取异常信息
- [TalkingData setSignalReportEnabled:YES]; //自动获取异常信号
-
- }
- -(void)TDAnalyticsTrackPageBegin:(NSString *)pageName {
- [TalkingData trackPageBegin:pageName];
- }
- -(void)TDAnalyticsTrackPageEnd:(NSString *)pageName {
- [TalkingData trackPageEnd:pageName];
- }
- //自定义事件1
- - (void)TDAnalyticsTrackEvent:(NSString *)eventID {
-
- [TalkingData trackEvent:eventID];
- }
- //自定义事件2
- - (void)TDAnalyticsTrackEvent:(NSString *)eventID label:(NSString *)event_label {
- [TalkingData trackEvent:eventID label:event_label];
- }
- //自定义事件3,eventID:自定义事件名称, event_label:自定义事件Label,用来区分具体场景. dict:自定义事件的参数及参数取值.
- - (void)TDAnalyticsTrackEvent:(NSString *)eventID label:(NSString *)event_label parameters:(NSDictionary *)dict {
-
- [TalkingData trackEvent:eventID label:event_label parameters:dict];
- }
- //orderId:订单ID total:订单总金额,货币单位为分 currencyType:货币种类,eg:人民币 CNY;美元 USD;欧元 EUR
- -(TalkingDataOrder *)TDAnalyticsCreateOrder:(NSString *)orderId total:(int)total currencyType:(NSString *)currencyType {
-
- TalkingDataOrder *order = [TalkingDataOrder createOrder:orderId total:total currencyType:currencyType];
- return order;
- }
- //应用内支付-添加订单详情.order为创建的订单
- - (void)TDAnalyticsWith:(TalkingDataOrder *)order AddItem:(NSString *)itemId category:(NSString *)category name:(NSString *)name unitPrice:(int)unitPrice amount:(int)amount {
-
- [order addItem:itemId category:category name:name unitPrice:unitPrice amount:amount];
- }
- //应用内支付-成功支付订单
- - (void)TDAnalyticsOnPlaceOrder:(NSString *)profileID order:(TalkingDataOrder *)order {
-
- [TalkingData onPlaceOrder:profileID order:order];
- }
- ///穿山甲广告Ad
- //1.init
- - (void)CSJAdSDKinit {
-
- [MLAdManager sharedInstance];
-
- }
- //优量汇广告Ad-init
- -(void)YLHAdSDKinit {
-
- [YLHAdManager sharedInstance];
- }
- //TopOn广告Ad-init
- -(void)TopOnAdSDKinit {
-
- [TOPONAdManager sharedInstance];
-
- }
- //2.打开全屏广告
- -(void)openBUDFullScreenVideoAd:(NSString *)adName callback:(void (^)(BOOL))callback {
-
- [[MLAdManager sharedInstance] openFullScreenVideoAd:adName callback:callback];
- }
- //打开新插屏广告(新增)
- -(void)openBUDNewInterstitialAd:(NSString *)adName callback:(void (^)(BOOL))callback {
-
- [[MLAdManager sharedInstance] openNewInterstitialAd:adName callback:callback];
-
- }
- //2.打开开屏广告
- -(void)openBUDSplashAd:(NSString *)adName callback:(void (^)(BOOL))callback {
-
- [[MLAdManager sharedInstance] openSplashAd:adName callback:callback];
- }
- //3. 打开插屏广告
- -(void)openBUDInterstitialAd:(NSString *)adName width:(CGFloat)w Height:(CGFloat)h callback:(void (^)(BOOL))callback {
-
- [[MLAdManager sharedInstance] openExpressInterstitialAd:adName Width:w Height:h callback:callback];
-
- }
- //4. 打开视频激励广告
- -(void)openBUDExpressRewardedVideoAd:(NSString *)adName callback:(void (^)(BOOL,BOOL))callback {
-
- [[MLAdManager sharedInstance] openExpressRewardedVideoAd:adName callback:callback];
- }
- //5. 打开原生Banner广告
- -(void)openBUDNativeBannerAd:(NSString *)adName WithX:(CGFloat)x Y:(CGFloat)y Width:(CGFloat)w Height:(CGFloat)h callback:(void (^)(BOOL))callback {
-
- [[MLAdManager sharedInstance] openNativeExpressBannerAd:adName WithX:x Y:y Width:w Height:h callback:callback];
- }
- //6. 打开原生广告
- -(void)openBUDNativeAd:(NSString *)adName WidthCGRect:(CGRect)rect BUSize:(NSInteger)size callback:(void (^)(BOOL))callback {
-
- [[MLAdManager sharedInstance] openNativeAd:adName WidthCGRect:rect BUSize:size callback:callback];
-
- }
- //5.2 关闭Banner广告
- -(void)closeBUDBannerAd:(NSString *)adName {
-
- [[MLAdManager sharedInstance] closeBannerAd:adName];
-
- }
- //优量汇广告
- //init
- -(void)openYLHSplashAd:(NSString *)adName supportZoomoutView:(BOOL)flag callback:(void (^)(BOOL))callback {
-
- [[YLHAdManager sharedInstance] openSplashAd:adName supportZoomoutView:flag callback:callback];
-
- }
- -(void)openYLHInterstitialAd:(NSString *)adName callback:(void (^)(BOOL))callback {
-
- [[YLHAdManager sharedInstance] openExpressInterstitialAd:adName callback:callback];
-
- }
- -(void)openYLHFullScreenAd:(NSString *)adName callback:(void (^)(BOOL))callback {
-
- [[YLHAdManager sharedInstance] openFullScreenVideoAd:adName callback:callback];
-
- }
- -(void)openYLHExpressRewardedVideoAd:(NSString *)adName callback:(void (^)(BOOL, BOOL))callback {
-
- [[YLHAdManager sharedInstance] openExpressRewardedVideoAd:adName callback:callback];
-
- }
- //4.5 打开横幅广告
- -(void)openYLHExpressBannerAd:(NSString *)adName callback:(void (^)(BOOL))callback {
-
- [[YLHAdManager sharedInstance] openExpressBannerVideoAd:adName callback:callback];
-
- }
- //4.6 打开原生广告
- -(void)openYLHExpressNativeAdWithPlacementId:(NSString *)placementId AdName:(NSString *)adName Frame:(CGRect)rect callback:(void (^)(BOOL))callback {
-
- [[YLHAdManager sharedInstance] openExpressNativeAdWithPlacementId:placementId AdName:adName Frame:rect callback:callback];
-
- }
- -(void)openTOPONSplashAd:(NSString *)adName callback:(void (^)(BOOL))callback {
- [[TOPONAdManager sharedInstance] opensplashAd:adName callback:callback];
- }
- -(void)openTOPONInterstitialAd:(NSString *)adName callback:(void (^)(BOOL))callback {
-
- [[TOPONAdManager sharedInstance] openInterstitialAd:adName callback:callback];
-
- }
- -(void)openTOPONRewardedVideoAd:(NSString *)adName callback:(void (^)(BOOL,BOOL))callback {
- [[TOPONAdManager sharedInstance] openRewardedVideoAd:adName callback:callback];
-
- }
- -(void)openTOPONExpressBannerAd:(NSString *)adName callback:(void (^)(BOOL))callback {
-
- [[TOPONAdManager sharedInstance] openExpressBannerAd:adName callback:callback];
- }
- -(void)openTOPONNativeAdWithX:(int)x Y:(int)y width:(int)w height:(int)h adId:(NSString *)adName callback:(void (^)(BOOL))callback {
- [[TOPONAdManager sharedInstance] openNativeAdWithX:x Y:y width:w height:h adId:adName callback:callback];
-
- }
- -(void)showHudWith:(NSString *)name {
-
- [XSNetwork showHudSuccess:name];
- }
- @end
|