前言 :
各位同學(xué)大家好 有段時(shí)間沒有更新博客 凌净, 因?yàn)槲疑洗慰捡{照都事情 過去了以后 我休息了一段時(shí)間 ,現(xiàn)在呢算是重新出發(fā) . 我能是從事安卓手游sdk 開發(fā)的 所以寫app的東西 安卓都手游sdk都·很成熟了 所以沒什么好寫的 iOS 的這種教程 非常都少 各大網(wǎng)站上面都搜索不到 可能iOS 大神們覺得沒什么好寫 我這邊就 分享下 如果有紕漏或者錯(cuò)誤 希望大家指正 謝謝大家名惩。
效果圖:
. 具體實(shí)現(xiàn):
主要說到都幾個(gè)點(diǎn) 我們
網(wǎng)絡(luò)請求部分用的是. AFNetworking
吐司提示用是 WHToast
數(shù)據(jù)存儲我們使用 NSUserDefaults
sdk 整體的接口設(shè)計(jì)
我們在ninefunsdk 類中定義了
init.初始化
checkLogin 登錄
serviceLogin 屈服登錄
updateUserInfo 等級變化上報(bào)
loginOut :注銷
pay 等接口
ninefun sdk.h 文件具體實(shí)現(xiàn)
//
// NineFunSdk.h
//
//
// Created by xuqing on 2022/4/26.
//
#import <Foundation/Foundation.h>
#import <UIkit/UIkit.h>
#import "RoleInfo.h"
#import "ServiceInfo.h"
NS_ASSUME_NONNULL_BEGIN
typedef void (^LoginFinishBlock)(BOOL success, id data);
typedef void (^InitFinishBlock)(BOOL success, NSString * data);
typedef void (^PayFinishBlock)(BOOL success, NSString * data);
typedef void (^ServiceBlock)(BOOL success);
typedef void (^UpdateUserInfoBlcok)(BOOL success);
typedef void (^loginOutFinishBlock)(BOOL success);
@interface NineFunSdk : NSObject
+ (void)viewcontroller:(UIViewController *)controller sdkCheckLogin:(LoginFinishBlock)finishBlock;
+ (void)initSdk:(InitFinishBlock)finishBlock;
+ (void)pay:(PayFinishBlock)finishBlock;
+ (void)serviceinfo:(ServiceInfo*)getserviceinfo serviceLogin:(ServiceBlock)finishBlock;
+ (void)roleinfo:(RoleInfo*)getroleinfo updateUserInfo:(UpdateUserInfoBlcok)finishBlock;
+ (void)loginOut:(loginOutFinishBlock)finishBlock;
@end
NS_ASSUME_NONNULL_END
ninefun sdk. m文件具體實(shí)現(xiàn)
//
// NineFunSdk.m
// Created by xuqing on 2022/4/26.
//
#import "NineFunSdk.h"
#import "ViewController.h"
#import "AFNetworking.h"
#import "RoleInfo.h"
#import "ServiceInfo.h"
#import "Md5Utils.h"
#import "WHToast.h"
#import "ToastUtils.h"
#import "LoginView.h"
#import "RegisterView.h"
#import "TextUtils.h""
@implementation NineFunSdk
// 登錄接口
+ (void)viewcontroller:(UIViewController *)controller sdkCheckLogin:(LoginFinishBlock)finishBlock{
NSUserDefaults * ud=[NSUserDefaults standardUserDefaults];
id object=[ud objectForKey:@"token"];
NSString * _token=(NSString *)object;
NSLog(@"token --- %@", _token);
//如果token不為空 就繼續(xù)執(zhí)行
if(![TextUtils isBlankString:_token]){
NSString * tokensignUrl=@"http://175.178.200.180:8090/boss/user/tokensign?";
NSMutableString *geturl = [NSMutableString string];
[geturl appendString:tokensignUrl];
[geturl appendString:@"token="];
[geturl appendString:_token];
NSString *url = [geturl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSLog(@"url --- %@ ",url);
[[AFHTTPSessionManager manager]GET:url parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
NSLog(@"");
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"");
NSLog(@"請求成功---%@",responseObject);
NSLog(@"請求 msg ---%@",[responseObject objectForKey:(@"msg")]);
NSString * msg=[responseObject objectForKey:(@"msg")];
NSInteger code= [[responseObject objectForKey:(@"code")] integerValue];
if(code==200){
finishBlock(YES, responseObject);
}else{
finishBlock(YES, responseObject);
}
[WHToast showMessage: msg duration:2 finishHandler:^{
NSLog(@"Toast 成功 ");
}];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"");
}];
}else{
// RegisterView * registerview=[[RegisterView alloc]init];
// registerview.registerBlock=finishBlock;
// [registerview show];
//
LoginView *loginview=[[LoginView alloc]init];
loginview.logBlock=finishBlock;
[loginview show];
}
}
// 初始化接口
+ (void)initSdk:(InitFinishBlock)finishBlock{
}
//支付接口
+ (void)pay:(PayFinishBlock)finishBlock{
}
// 區(qū)服登錄接口
+ (void)serviceinfo:(ServiceInfo*)getserviceinfo serviceLogin:(ServiceBlock)finishBlock{
NSLog(@"區(qū)服登錄");
NSString * sign = [NSString stringWithFormat:@"sid=%@role_id=%@role_anme=%@||0a649d6b-df9f-4583-bf7d-3fae84838519",getserviceinfo->_sid,getserviceinfo->_roleid,getserviceinfo->_rolename];
NSLog(@" sign %@", sign);
NSString * getsign=[Md5Utils getMd5_32Bit_String:sign isUppercase:NO];
NSLog(@" getsign --- > %@" ,getsign);
NSString * serviceLoginUrl=@"http://175.178.200.180:8090/boss/serviceinfo/servicelogin?";
NSMutableString *geturl = [NSMutableString string];
[geturl appendString:serviceLoginUrl];
[geturl appendString:@"gid="];
[geturl appendString:getserviceinfo->_gid];
[geturl appendString:@"&"];
[geturl appendString:@"sid="];
[geturl appendString:getserviceinfo->_sid];
[geturl appendString:@"&"];
[geturl appendString:@"role_name="];
[geturl appendString:getserviceinfo->_rolename];
[geturl appendString:@"&"];
[geturl appendString:@"role_level="];
[geturl appendString:getserviceinfo->_rolelevel];
[geturl appendString:@"&"];
[geturl appendString:@"role_id="];
[geturl appendString:getserviceinfo->_roleid];
[geturl appendString:@"&"];
[geturl appendString:@"sign="];
NSString *url = [geturl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
[[AFHTTPSessionManager manager]GET:url parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
NSLog(@"");
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"");
NSLog(@"請求成功---%@",responseObject);
NSLog(@"請求 msg ---%@",[responseObject objectForKey:(@"msg")]);
NSString * msg=[responseObject objectForKey:(@"msg")];
NSInteger code= [[responseObject objectForKey:(@"code")] integerValue];
if(code==200){
finishBlock(YES);
}else{
finishBlock(NO);
}
[WHToast showMessage: msg duration:2 finishHandler:^{
NSLog(@"Toast 成功 ");
}];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"");
}];
};
//等級變化上報(bào)接口
+ (void)roleinfo:(RoleInfo*)getroleinfo updateUserInfo:(UpdateUserInfoBlcok)finishBlock{
NSLog(@"等級變化上報(bào)");
NSString * sign = [NSString stringWithFormat:@"sid=%@&role_id=%@&role_anme=%@||0a649d6b-df9f-4583-bf7d-3fae84838519",getroleinfo->_sid,getroleinfo->_roleid,getroleinfo->_rolename];
NSLog(@" sign %@", sign);
// NSString * getsign=[sign MD5];
NSString * getsign=[Md5Utils getMd5_32Bit_String:sign isUppercase:NO];
NSLog(@" getsign %@", getsign);
NSString * serviceLoginUrl=@"http://175.178.200.180:8090/boss/roleinfo/submitroleinfo?";
NSMutableString *geturl = [NSMutableString string];
[geturl appendString:serviceLoginUrl];
[geturl appendString:@"gid="];
[geturl appendString:getroleinfo->_gid];
[geturl appendString:@"&"];
[geturl appendString:@"sid="];
[geturl appendString:getroleinfo->_sid];
[geturl appendString:@"&"];
[geturl appendString:@"role_name="];
[geturl appendString:getroleinfo->_rolename];
[geturl appendString:@"&"];
[geturl appendString:@"role_level="];
[geturl appendString:getroleinfo->_rolelevel];
[geturl appendString:@"&"];
[geturl appendString:@"role_id="];
[geturl appendString:getroleinfo->_roleid];
[geturl appendString:@"&"];
[geturl appendString:@"sign="];
[geturl appendString:getsign];
NSString *url = [geturl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSLog(@"url %@" ,url );
[[AFHTTPSessionManager manager]GET:url parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
NSLog(@"");
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"");
NSLog(@"請求成功---%@",responseObject);
NSLog(@"請求 msg ---%@",[responseObject objectForKey:(@"msg")]);
NSString * msg =[responseObject objectForKey:(@"msg")];
NSInteger code=[[responseObject objectForKey:(@"code")]integerValue];
if(code==200){
finishBlock(YES);
}else{
finishBlock(NO);
}
[WHToast showMessage: msg duration:2 finishHandler:^{
NSLog(@"Toast 成功 ");
}];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"");
}];
}
+ (void)loginOut:(loginOutFinishBlock)finishBlock{
NSUserDefaults * ud =[NSUserDefaults standardUserDefaults];
[ud removeObjectForKey:@"token"];
if(ud!=nil){
finishBlock(YES);
}else{
finishBlock(NO);
}
}
@end
我們這邊調(diào)用了登錄方法后 我們先判定是否有token緩存 如果有直接調(diào)用刷新token的接口 然后返回同樣都登錄數(shù)據(jù)給到 調(diào)用方 也就是游戲 如果本地沒有緩存token就 直接展示登錄界面
如圖
.登錄界面邏輯具體實(shí)現(xiàn)
我們這邊使用都是代碼布局 我個(gè)人建議不要使用xib布局 后期很難維護(hù)
.登錄界面 loginview.
//
// LoginView.h
// 016登錄界面
//
// Created by xuqing on 2022/5/5.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface LoginView : UIView{
//登錄界面的組成
UILabel *_usernameLabel;
UILabel * _passwordLabel;
UITextField *_userTextfield;
UITextField *_passwordfield;
UIButton * _loginBtn;
UIButton * _registerBtn;
UIView * _blackBgV;
UIView * _alertV;
UIButton* _quickregisterBtn;
}
@property(retain,nonatomic)UILabel * usernameLabel;
@property(retain,nonatomic)UILabel * pswLabel;
@property(retain,nonatomic)UITextField * userTextfield;
@property(retain,nonatomic)UITextField * pswfield;
@property(retain,nonatomic)UIButton * loginBtn ;
@property(retain, nonatomic)UIButton * registerBtn;
@property(retain, nonatomic)UIButton * quickregisterBtn;
@property(retain, nonatomic)UIView * blackBgV;
@property(retain, nonatomic)UIView * alertV;
@property (nonatomic,copy)void(^logBlock)(BOOL success, id data);
- (void)show;
@end
NS_ASSUME_NONNULL_END
.登錄界面 .m文件實(shí)現(xiàn)
//
// LoginView.m
// 016登錄界面
//
// Created by xuqing on 2022/5/5.
//
#import "LoginView.h"
#import "WHToast.h"
#import "AFNetworking.h"
#import "TextUtils.h"
#import "ToastUtils.h"
#import "RegisterView.h"
@implementation LoginView
@synthesize usernameLabel=_usernameLabel;
@synthesize pswLabel=_passwordLabel;
@synthesize userTextfield=_userTextfield;
@synthesize pswfield=_passwordfield;
@synthesize loginBtn=_loginBtn;
@synthesize registerBtn=_registerBtn;
@synthesize blackBgV=_blackBgV;
@synthesize alertV=_alertV;
@synthesize quickregisterBtn=_quickregisterBtn;
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self setUpSubView];
}
return self;
}
- (void)setUpSubView
{
self.frame = [UIScreen mainScreen].bounds;
// 初始化遮罩視圖
self.blackBgV = [[UIView alloc]initWithFrame:self.bounds];
self.blackBgV.backgroundColor = [UIColor grayColor];
self.blackBgV.alpha = 0;
[self addSubview:self.blackBgV];
// 初始化actionSheetView
self.alertV = [[UIView alloc] initWithFrame:CGRectMake(30,self.bounds.size.height/2-120, self.bounds.size.width-60, 240)];
self.alertV.backgroundColor = [UIColor whiteColor];
self.alertV.layer.cornerRadius = 5;
self.alertV.layer.masksToBounds = YES;
self.alertV.alpha = 0;
[self addSubview:self.alertV];
_usernameLabel =[[UILabel alloc]init];
_usernameLabel.frame=CGRectMake(60, 50, 60, 40);
_usernameLabel.text=@"賬號:";
_usernameLabel.textColor=[UIColor blueColor];
[self.alertV addSubview:_usernameLabel];
_userTextfield =[[UITextField alloc] init];
_userTextfield.frame=CGRectMake(100, 50,180, 40);
_userTextfield.font= [UIFont systemFontOfSize:15];
// 設(shè)置字體顏色
_userTextfield.textColor=[UIColor blackColor];
//設(shè)置邊框的風(fēng)格
// UITextBorderStyleBezel bezel線框
// UITextBorderStyleRoundedRect 圓角風(fēng)格
// UITextBorderStyleLine 線框風(fēng)格
// UITextBorderStyleNone 無邊框風(fēng)格
_userTextfield.borderStyle= UITextBorderStyleRoundedRect;
//設(shè)置虛擬鍵盤風(fēng)格
//UIKeyboardTypeDefault 默認(rèn)風(fēng)格
//UIKeyboardTypeNumberPad 純數(shù)字風(fēng)格
//UIKeyboardTypeNamePhonePad 字母和數(shù)字組合風(fēng)格
_userTextfield.keyboardType=UIKeyboardTypeNamePhonePad;
_userTextfield.placeholder=@"請輸入用戶名...";
//是否作為密碼輸入
//YES 做為處理 圓點(diǎn)加密
//NO: 正常顯示輸入的文字
_userTextfield.secureTextEntry= NO;
[self.alertV addSubview: _userTextfield];
_passwordLabel =[[UILabel alloc]init];
_passwordLabel.frame=CGRectMake(60, 100, 60, 40);
_passwordLabel.text=@"密碼:";
_passwordLabel.textColor=[UIColor blueColor];
[self.alertV addSubview:_passwordLabel];
_passwordfield =[[UITextField alloc] init];
_passwordfield.frame=CGRectMake(100, 100,180, 40);
_passwordfield.font= [UIFont systemFontOfSize:15];
// 設(shè)置字體顏色
_passwordfield.textColor=[UIColor blackColor];
//設(shè)置邊框的風(fēng)格
// UITextBorderStyleBezel bezel線框
// UITextBorderStyleRoundedRect 圓角風(fēng)格
// UITextBorderStyleLine 線框風(fēng)格
// UITextBorderStyleNone 無邊框風(fēng)格
_passwordfield.borderStyle= UITextBorderStyleRoundedRect;
//設(shè)置虛擬鍵盤風(fēng)格
//UIKeyboardTypeDefault 默認(rèn)風(fēng)格
//UIKeyboardTypeNumberPad 純數(shù)字風(fēng)格
//UIKeyboardTypeNamePhonePad 字母和數(shù)字組合風(fēng)格
_passwordfield.keyboardType=UIKeyboardTypeNamePhonePad;
_passwordfield.placeholder=@"請輸入密碼...";
//是否作為密碼輸入
//YES 做為處理 圓點(diǎn)加密
//NO: 正常顯示輸入的文字
_passwordfield.secureTextEntry= YES;
[self.alertV addSubview: _passwordfield];
_loginBtn=[UIButton buttonWithType: UIButtonTypeRoundedRect];
_loginBtn.frame=CGRectMake(40, 160, 80, 40);
[_loginBtn setTitle:@"登錄" forState:UIControlStateNormal];
[_loginBtn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
_loginBtn.backgroundColor=[UIColor blueColor];
[_loginBtn setTitleColor: [UIColor whiteColor ] forState:UIControlStateNormal] ;
//設(shè)置按鈕顏色
[_loginBtn setTitleColor: [UIColor grayColor] forState:UIControlStateHighlighted] ;
[self.alertV addSubview:_loginBtn];
// 注冊按鈕
_registerBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
_registerBtn.frame=CGRectMake(130, 160, 80, 40);
[_registerBtn setTitle:@"注冊" forState:UIControlStateNormal];
[_registerBtn addTarget:self action:@selector(registerMethod) forControlEvents:UIControlEventTouchUpInside];
_registerBtn.backgroundColor=[UIColor blueColor];
[_registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.alertV addSubview:_registerBtn];
// 直接玩按鈕
_quickregisterBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
_quickregisterBtn.frame=CGRectMake(220, 160, 80, 40);
[_quickregisterBtn setTitle:@"直接玩" forState:UIControlStateNormal];
[_quickregisterBtn addTarget:self action:@selector(quickregisterMethod) forControlEvents:UIControlEventTouchUpInside];
_quickregisterBtn.backgroundColor=[UIColor blueColor];
[_quickregisterBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.alertV addSubview:_quickregisterBtn];
}
-(void)registerMethod{
[self hidden];
RegisterView * registerview=[[RegisterView alloc]init];
registerview.registerBlock=_logBlock;
[registerview show];
}
//直接玩
-(void)quickregisterMethod{
// NSString * getUsername=getVerificationCode;
// NSLog(@"getUsername --- > ",getUsername);
// NSString * getPassword=getVerificationCode;
// NSLog(@"getPassword --- > ",getPassword);
}
//
//- (NSString *)getVerificationCode{
//// NSArray *strArr = [[NSArray alloc]initWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",nil] ;
//// NSMutableString *getStr = [[NSMutableString alloc]initWithCapacity:5];
//// //得到六位隨機(jī)字符,可自己設(shè)長度
//// for(int i = 0; i < 6; i++) {
//// int index = arc4random() % ([strArr count]);
//// //得到數(shù)組中隨機(jī)數(shù)的下標(biāo)
//// [getStr appendString:[strArr objectAtIndex:index]];
//// }
//// NSLog(@"驗(yàn)證碼:%@",getStr);
//// [strArr release];
//// [getStr release];
//// return getStr;
//}
-(void)login{
NSString * _strTextName=_userTextfield.text;
NSString * _strTextPsw=_passwordfield.text;
if(![TextUtils isBlankString:_strTextName]
&&![TextUtils isBlankString:_strTextPsw]){
NSLog(@"登錄成功");
NSLog(@"%@ %@",_strTextName,_strTextPsw);
NSString * registerString=@"http://175.178.200.180:8090/boss/user/validlogin?";
NSString * str1=@"username=";
NSString * str2=@"password=";
NSMutableString *geturl = [NSMutableString string];
[geturl appendString:registerString];
[geturl appendString:str1];
[geturl appendString:_strTextName];
[geturl appendString:@"&"];
[geturl appendString:str2];
[geturl appendString:_strTextPsw];
NSLog(@" geturl --- > %@",geturl);
[[AFHTTPSessionManager manager]GET:geturl parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
NSLog(@"");
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"");
NSLog(@"請求成功---%@",responseObject);
NSLog(@"請求 msg ---%@",[responseObject objectForKey:(@"msg")]);
NSString * msg=[responseObject objectForKey:(@"msg")];
NSInteger code = [[responseObject objectForKey:(@"code")] integerValue];
if(code==200){
if (self.logBlock) {
//緩存token
NSString * token=[responseObject objectForKey:(@"token")];
NSUserDefaults * ud =[NSUserDefaults standardUserDefaults];
[ud setObject:token forKey:@"token"];
self.logBlock(YES, responseObject);
[self hidden];
}
}else{
[ToastUtils showInfo:msg];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"");
}];
}else {
[ToastUtils showInfo:@"賬號或者密碼不能為空" ];
}
}
- (void)show {
[self setUpSubView];
[[UIApplication sharedApplication].keyWindow addSubview:self];
self.hidden = NO;
[UIView animateWithDuration:0.3 animations:^{
self.alertV.alpha = 1;
self.blackBgV.alpha = 0.5;
} completion:^(BOOL finished) {
}];
}
- (void)hidden {
__block typeof(self) weakSelf = self;
[UIView animateWithDuration:0.3 animations:^{
self.alertV.alpha = 0;
self.blackBgV.alpha = 0;
} completion:^(BOOL finished) {
weakSelf.hidden = YES;
[weakSelf removeFromSuperview];
}];
}
#pragma mark -- 遮罩視圖點(diǎn)擊
- (void)tapClick:(UIGestureRecognizer *)tap {
NSLog(@"遮罩視圖點(diǎn)擊");
}
@end
我們這邊登錄請求成功后就把服務(wù)端返回都數(shù)據(jù)格式 同過block 回調(diào)給我們都 游戲方 然后游戲方可以拿到我們登錄都數(shù)據(jù)去進(jìn)行校驗(yàn) 最后完成登錄都動(dòng)作
注冊界面也是同樣都邏輯 不同都是調(diào)用都注冊接口 也是注冊成功后把返回都數(shù)據(jù)格式回調(diào)給游戲方
.數(shù)據(jù)上報(bào)
-
區(qū)服登錄
// 區(qū)服登錄接口
+ (void)serviceinfo:(ServiceInfo*)getserviceinfo serviceLogin:(ServiceBlock)finishBlock{
NSLog(@"區(qū)服登錄");
NSString * sign = [NSString stringWithFormat:@"sid=%@role_id=%@role_anme=%@||0a649d6b-df9f-4583-bf7d-3fae84838519",getserviceinfo->_sid,getserviceinfo->_roleid,getserviceinfo->_rolename];
NSLog(@" sign %@", sign);
NSString * getsign=[Md5Utils getMd5_32Bit_String:sign isUppercase:NO];
NSLog(@" getsign --- > %@" ,getsign);
NSString * serviceLoginUrl=@"http://175.178.200.180:8090/boss/serviceinfo/servicelogin?";
NSMutableString *geturl = [NSMutableString string];
[geturl appendString:serviceLoginUrl];
[geturl appendString:@"gid="];
[geturl appendString:getserviceinfo->_gid];
[geturl appendString:@"&"];
[geturl appendString:@"sid="];
[geturl appendString:getserviceinfo->_sid];
[geturl appendString:@"&"];
[geturl appendString:@"role_name="];
[geturl appendString:getserviceinfo->_rolename];
[geturl appendString:@"&"];
[geturl appendString:@"role_level="];
[geturl appendString:getserviceinfo->_rolelevel];
[geturl appendString:@"&"];
[geturl appendString:@"role_id="];
[geturl appendString:getserviceinfo->_roleid];
[geturl appendString:@"&"];
[geturl appendString:@"sign="];
NSString *url = [geturl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
[[AFHTTPSessionManager manager]GET:url parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
NSLog(@"");
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"");
NSLog(@"請求成功---%@",responseObject);
NSLog(@"請求 msg ---%@",[responseObject objectForKey:(@"msg")]);
NSString * msg=[responseObject objectForKey:(@"msg")];
NSInteger code= [[responseObject objectForKey:(@"code")] integerValue];
if(code==200){
finishBlock(YES);
}else{
finishBlock(NO);
}
[WHToast showMessage: msg duration:2 finishHandler:^{
NSLog(@"Toast 成功 ");
}];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"");
}];
};
-
等級變化上報(bào)
//等級變化上報(bào)接口
+ (void)roleinfo:(RoleInfo*)getroleinfo updateUserInfo:(UpdateUserInfoBlcok)finishBlock{
NSLog(@"等級變化上報(bào)");
NSString * sign = [NSString stringWithFormat:@"sid=%@&role_id=%@&role_anme=%@||0a649d6b-df9f-4583-bf7d-3fae84838519",getroleinfo->_sid,getroleinfo->_roleid,getroleinfo->_rolename];
NSLog(@" sign %@", sign);
// NSString * getsign=[sign MD5];
NSString * getsign=[Md5Utils getMd5_32Bit_String:sign isUppercase:NO];
NSLog(@" getsign %@", getsign);
NSString * serviceLoginUrl=@"http://175.178.200.180:8090/boss/roleinfo/submitroleinfo?";
NSMutableString *geturl = [NSMutableString string];
[geturl appendString:serviceLoginUrl];
[geturl appendString:@"gid="];
[geturl appendString:getroleinfo->_gid];
[geturl appendString:@"&"];
[geturl appendString:@"sid="];
[geturl appendString:getroleinfo->_sid];
[geturl appendString:@"&"];
[geturl appendString:@"role_name="];
[geturl appendString:getroleinfo->_rolename];
[geturl appendString:@"&"];
[geturl appendString:@"role_level="];
[geturl appendString:getroleinfo->_rolelevel];
[geturl appendString:@"&"];
[geturl appendString:@"role_id="];
[geturl appendString:getroleinfo->_roleid];
[geturl appendString:@"&"];
[geturl appendString:@"sign="];
[geturl appendString:getsign];
NSString *url = [geturl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSLog(@"url %@" ,url );
[[AFHTTPSessionManager manager]GET:url parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
NSLog(@"");
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"");
NSLog(@"請求成功---%@",responseObject);
NSLog(@"請求 msg ---%@",[responseObject objectForKey:(@"msg")]);
NSString * msg =[responseObject objectForKey:(@"msg")];
NSInteger code=[[responseObject objectForKey:(@"code")]integerValue];
if(code==200){
finishBlock(YES);
}else{
finishBlock(NO);
}
[WHToast showMessage: msg duration:2 finishHandler:^{
NSLog(@"Toast 成功 ");
}];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"");
}];
}
-
注銷接口
+ (void)loginOut:(loginOutFinishBlock)finishBlock{
NSUserDefaults * ud =[NSUserDefaults standardUserDefaults];
[ud removeObjectForKey:@"token"];
if(ud!=nil){
finishBlock(YES);
}else{
finishBlock(NO);
}
}
. 模擬游戲接入
. ViewController .h 文件
//
// ViewController.h
// gamesdk
//
// Created by xuqing on 2023/3/2.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController{
UIButton * _btnInit;
UIButton * _btnlogin;
UIButton *_btnPay;
UIButton *_servicelogin;
UIButton * _updateuserinfo;
UIButton * _loginoutBtn;
}
@property(retain, nonatomic)UIButton * btnInit;
@property(retain, nonatomic)UIButton * btnlogin;
@property(retain, nonatomic)UIButton * btnPay;
@property(retain, nonatomic)UIButton * servicelogin;
@property(retain, nonatomic)UIButton * updateuserinfo;
@property(retain, nonatomic)UIButton * loginoutBtn;
@end
ViewController .m具體接入實(shí)現(xiàn) :
//
// ViewController.m
// gamesdk
//
// Created by xuqing on 2023/3/2.
//
#import "ViewController.h"
#import "NineFunSdk.h"
#import "AFNetworking.h"
#import "ToastUtils.h"
#import "SuspendView.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<SuspendViewDelegate>
@property (nonatomic, retain) SuspendView *suspendView;/**<#name#>*/
@property (nonatomic, strong) NSTimer* timer;/**<#name#>*/
@end
@implementation ViewController
@synthesize btnlogin=_btnlogin;
@synthesize btnInit=_btnInit;
@synthesize btnPay=_btnPay;
@synthesize servicelogin=_servicelogin;
@synthesize updateuserinfo=_updateuserinfo;
@synthesize loginoutBtn=_loginoutBtn;
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor whiteColor];
_timer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:false block:^(NSTimer * _Nonnull timer) {
[[UIApplication sharedApplication].keyWindow addSubview:self.suspendView];
[self->_timer invalidate];
self->_timer = nil;
}];
[self configSYSDKNotification];
_btnInit=[UIButton buttonWithType:UIButtonTypeRoundedRect];
_btnInit.frame=CGRectMake(100, 20, 80, 40);
[_btnInit setTitle:@"初始化" forState:UIControlStateNormal];
[_btnInit addTarget:self action:@selector(init) forControlEvents:UIControlEventTouchUpInside];
[_btnInit setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
// [self.view addSubview:_btnInit];
_btnlogin=[UIButton buttonWithType:UIButtonTypeRoundedRect];
_btnlogin.frame=CGRectMake(100, 60, 200, 40);
[_btnlogin setTitle:@"登錄" forState:UIControlStateNormal];
[_btnlogin addTarget:self action:@selector(loginBtn) forControlEvents:UIControlEventTouchUpInside];
[_btnlogin setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.view addSubview:_btnlogin];
_btnPay=[UIButton buttonWithType:UIButtonTypeRoundedRect];
_btnPay.frame=CGRectMake(100, 100, 200, 40);
[_btnPay setTitle:@"支付" forState:UIControlStateNormal];
[_btnPay addTarget:self action:@selector(payBtn) forControlEvents:UIControlEventTouchUpInside];
[_btnPay setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.view addSubview:_btnPay];
_servicelogin=[UIButton buttonWithType:UIButtonTypeRoundedRect];
_servicelogin.frame=CGRectMake(100, 140, 200, 40);
[_servicelogin setTitle:@"區(qū)服登錄" forState:UIControlStateNormal];
[_servicelogin addTarget:self action:@selector(serviceloginbtn) forControlEvents:UIControlEventTouchUpInside];
[_servicelogin setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.view addSubview:_servicelogin];
_updateuserinfo=[UIButton buttonWithType:UIButtonTypeRoundedRect];
_updateuserinfo.frame=CGRectMake(100, 180, 200, 40);
[_updateuserinfo setTitle:@"等級變化上報(bào)" forState:UIControlStateNormal];
[_updateuserinfo addTarget:self action:@selector(updateuserinfobtn) forControlEvents:UIControlEventTouchUpInside];
[_updateuserinfo setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.view addSubview:_updateuserinfo];
_loginoutBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
_loginoutBtn.frame=CGRectMake(100, 220, 200, 40);
[_loginoutBtn setTitle:@"注銷" forState:UIControlStateNormal];
[_loginoutBtn addTarget:self action:@selector(loginoutBtn) forControlEvents:UIControlEventTouchUpInside];
[_loginoutBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.view addSubview:_loginoutBtn];
}
- (void)uploadViewFrame{
_btnInit=[UIButton buttonWithType:UIButtonTypeRoundedRect];
_btnInit.frame=CGRectMake(100, 20, 80, 40);
[_btnInit setTitle:@"初始化" forState:UIControlStateNormal];
[_btnInit addTarget:self action:@selector(init) forControlEvents:UIControlEventTouchUpInside];
[_btnInit setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
// [self.view addSubview:_btnInit];
_btnlogin=[UIButton buttonWithType:UIButtonTypeRoundedRect];
_btnlogin.frame=CGRectMake(100, 60, 200, 40);
[_btnlogin setTitle:@"登錄" forState:UIControlStateNormal];
[_btnlogin addTarget:self action:@selector(loginBtn) forControlEvents:UIControlEventTouchUpInside];
[_btnlogin setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.view addSubview:_btnlogin];
_btnPay=[UIButton buttonWithType:UIButtonTypeRoundedRect];
_btnPay.frame=CGRectMake(100, 100, 200, 40);
[_btnPay setTitle:@"支付" forState:UIControlStateNormal];
[_btnPay addTarget:self action:@selector(payBtn) forControlEvents:UIControlEventTouchUpInside];
[_btnPay setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.view addSubview:_btnPay];
_servicelogin=[UIButton buttonWithType:UIButtonTypeRoundedRect];
_servicelogin.frame=CGRectMake(100, 140, 200, 40);
[_servicelogin setTitle:@"區(qū)服登錄" forState:UIControlStateNormal];
[_servicelogin addTarget:self action:@selector(serviceloginbtn) forControlEvents:UIControlEventTouchUpInside];
[_servicelogin setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.view addSubview:_servicelogin];
_updateuserinfo=[UIButton buttonWithType:UIButtonTypeRoundedRect];
_updateuserinfo.frame=CGRectMake(100, 180, 200, 40);
[_updateuserinfo setTitle:@"等級變化上報(bào)" forState:UIControlStateNormal];
[_updateuserinfo addTarget:self action:@selector(updateuserinfobtn) forControlEvents:UIControlEventTouchUpInside];
[_updateuserinfo setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.view addSubview:_updateuserinfo];
_loginoutBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
_loginoutBtn.frame=CGRectMake(100, 220, 200, 40);
[_loginoutBtn setTitle:@"注銷" forState:UIControlStateNormal];
[_loginoutBtn addTarget:self action:@selector(loginoutBtn) forControlEvents:UIControlEventTouchUpInside];
[_loginoutBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.view addSubview:_loginoutBtn];
}
#pragma mark ========================= SuspendViewDelegate =========================
- (void)suspendViewButtonClick:(nonnull UIButton *)sender {
NSLog(@"按鈕點(diǎn)擊事件");
}
#pragma mark ========================= 懶加載 =========================
- (SuspendView *)suspendView {
if (!_suspendView) {
_suspendView = [[SuspendView alloc] init];
_suspendView.delegate = self;
}
return _suspendView;
}
#pragma mark ========================= 通知 =========================
- (void)configSYSDKNotification{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didChangeStatusBarOrientation)
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];
}
- (void)didChangeStatusBarOrientation {
switch ([UIDevice currentDevice].orientation)
{
case UIDeviceOrientationPortraitUpsideDown:
// NSLog(@"faceBar在下面");
break;
case UIDeviceOrientationLandscapeLeft:
// NSLog(@"faceBar在左邊");
break;
case UIDeviceOrientationLandscapeRight:
// NSLog(@"faceBar在右邊");
break;
case UIDeviceOrientationPortrait:
// NSLog(@"faceBar在上邊");
break;
default: // as UIInterfaceOrientationPortrait
// NSLog(@"faceBar在上邊");
break;
}
// [self.suspendView dismissSuspendView];
[self uploadViewFrame];
}
//登出方法
-(void)loginoutBtn{
[NineFunSdk loginOut:^(BOOL success) {
if(success==YES){
[self.suspendView dismissSuspendView];
[NineFunSdk viewcontroller:self sdkCheckLogin:^(BOOL success, id data) {
NSLog(@"回調(diào)成功成功---%@",data);
NSLog(@"請求 登錄回調(diào)msg ---%@",[data objectForKey:(@"msg")]);
int * code =[[data objectForKey: (@"code")] intValue];
NSLog(@"登錄回調(diào) %d",code);
NSString * msg=[data objectForKey:(@"msg")];
NSLog(@"msg ----> %@",msg);
//登錄校驗(yàn)邏輯
if(code==200){
[self.suspendView showSuspendView];
NSString * token= [data objectForKey:(@"token")];
NSLog(@"token -- > @%",token );
NSString * userid = [NSString stringWithFormat:@"%@",data[@"user"][@"id"]] ;
NSLog(@"userid --- > %@", userid);
[self apitoken:token getid:userid];
}
}];
}
}];
}
-(void)loginBtn{
[NineFunSdk viewcontroller:self sdkCheckLogin:^(BOOL success, id data) {
NSLog(@"回調(diào)成功成功---%@",data);
NSLog(@"請求 登錄回調(diào)msg ---%@",[data objectForKey:(@"msg")]);
int * code =[[data objectForKey: (@"code")] intValue];
NSLog(@"登錄回調(diào) %d",code);
NSString * msg=[data objectForKey:(@"msg")];
NSLog(@"msg ----> %@",msg);
//登錄校驗(yàn)邏輯
if(code==200){
[self.suspendView showSuspendView];
NSString * token= [data objectForKey:(@"token")];
NSLog(@"token -- > @%",token );
NSString * userid = [NSString stringWithFormat:@"%@",data[@"user"][@"id"]] ;
NSLog(@"userid --- > %@", userid);
[self apitoken:token getid:userid];
}
}];
}
// apitoken 校驗(yàn)token 校驗(yàn)成功才是真正登錄
- (void)apitoken:(NSString *)token getid:(NSString *)userid{
NSString * url=@"http://175.178.200.180:8090/boss/user/apitoken?";
NSMutableString *geturl = [NSMutableString string];
[geturl appendString:url];
[geturl appendString:@"token="];
[geturl appendString:token];
[geturl appendString:@"&"];
[geturl appendString:@"id="];
[geturl appendString:userid];
NSLog(@"geturl --- > %@ ", geturl);
[[AFHTTPSessionManager manager]GET:geturl parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
NSLog(@"");
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSString *msg=[responseObject objectForKey:(@"msg")];
NSLog(@"");
NSLog(@"請求成功---%@",responseObject);
NSLog(@"請求 msg ---%@",[responseObject objectForKey:(@"msg")]);
// NSLog(@"請求 code ---%d",[responseObject objectForKey:(@"code")]);
NSLog(@"請求 code---轉(zhuǎn)換格式--- %d",[[responseObject objectForKey:(@"code")] intValue]);
int * code=[[responseObject objectForKey:(@"code")] intValue];
NSString * getMsg=[responseObject objectForKey:(@"msg")];
if(code==200){
[ToastUtils showInfo:msg];
}else {
// 顯示在頁面中間,duration代表多久之后消失
[ToastUtils showInfo:msg];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"");
}];
}
//支付方法
-(void)payBtn{
}
// 區(qū)服登錄
-(void)serviceloginbtn{
ServiceInfo * serviceinfo=[ServiceInfo new];
serviceinfo->_sid=@"1111";
serviceinfo->_gid=@"424251";
serviceinfo->_roleid=@"旋渦名人";
serviceinfo->_rolename=@"最強(qiáng)王者";
serviceinfo->_rolelevel=@"214141";
[NineFunSdk serviceinfo:serviceinfo serviceLogin:^(BOOL success) {
NSLog(@"區(qū)服登錄");
}];
}
-(void)updateuserinfobtn{
RoleInfo * roleinfo= [RoleInfo new];
roleinfo->_gid=@"12232";
roleinfo->_roleid=@"422422";
roleinfo->_rolename=@"醉生夢死";
roleinfo->_sid=@"測試服務(wù)器";
roleinfo->_rolelevel=@"191";
[NineFunSdk roleinfo:roleinfo updateUserInfo:^(BOOL success) {
NSLog(@"等級變化上報(bào)");
}];
}
@end
. 最后總結(jié) :
我們都手游sdk 主要是把一套完整都功能封裝成庫 然后發(fā)給接入方用 所以我們最好不要用三方庫 我這里也用 了但是我這邊是直接把代碼復(fù)制進(jìn)來嘞 所以 問題不大 只要能避免跟接入方發(fā)生沖突 我們就可以了 界面顯示 我們這邊建議最好使用彈窗 不要使用透明的控制器 透明的控制器效果不是很好 還有就是數(shù)據(jù)存儲 我們這邊建議使用 NSUserDefaults 即可 這個(gè)存儲基本數(shù)據(jù)和對象都可以 司忱。最后呢 希望我都文章能幫助到各位同學(xué)工作和學(xué)習(xí) 如果你覺得文章還不錯(cuò)麻煩給我三連 關(guān)注點(diǎn)贊和轉(zhuǎn)發(fā) 謝謝