前语 :
各位同学大家好 有段时间没有更新博客 , 因为我上次考驾照都事情 过去了今后 我休息了一段时间 ,现在呢算是重新动身 . 我能是从事安卓手游sdk 开发的 所以写app的东西 安卓都手游sdk都很成熟了 所以没什么好写的 iOS 的这种教程 非常都少 各大网站上面都搜索不到 可能iOS 大神们觉得没什么好写 我这边就 共享下 假如有疏忽或者错误 期望大家纠正 谢谢大家。
作用图:
. 详细完结:
主要说到都几个点 咱们
网络恳求部分用的是. AFNetworking
吐司提示用是 WHToast
数据存储咱们运用 NSUserDefaults
sdk 全体的接口规划
咱们在ninefunsdk 类中定义了
init.初始化
checkLogin 登录
serviceLogin 屈从登录
updateUserInfo 等级改变上报
loginOut :刊出
pay 等接口
ninefun sdk.h 文件详细完结
//
// 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文件详细完结
//
// 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不为空 就继续执行
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{
}
// 区服登录接口
+ (void)serviceinfo:(ServiceInfo*)getserviceinfo serviceLogin:(ServiceBlock)finishBlock{
NSLog(@"区服登录");
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(@"");
}];
};
//等级改变上报接口
+ (void)roleinfo:(RoleInfo*)getroleinfo updateUserInfo:(UpdateUserInfoBlcok)finishBlock{
NSLog(@"等级改变上报");
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
咱们这边调用了登录方法后 咱们先判定是否有token缓存 假如有直接调用改写token的接口 然后回来同样都登录数据给到 调用方 也便是游戏 假如本地没有缓存token就 直接展现登录界面
如图
##.登录界面逻辑详细完结
咱们这边运用都是代码布局 我个人主张不要运用xib布局 后期很难保护
##.登录界面 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文件完结
//
// 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];
// 设置字体色彩
_userTextfield.textColor=[UIColor blackColor];
//设置边框的风格
// UITextBorderStyleBezel bezel线框
// UITextBorderStyleRoundedRect 圆角风格
// UITextBorderStyleLine 线框风格
// UITextBorderStyleNone 无边框风格
_userTextfield.borderStyle= UITextBorderStyleRoundedRect;
//设置虚拟键盘风格
//UIKeyboardTypeDefault 默许风格
//UIKeyboardTypeNumberPad 纯数字风格
//UIKeyboardTypeNamePhonePad 字母和数字组合风格
_userTextfield.keyboardType=UIKeyboardTypeNamePhonePad;
_userTextfield.placeholder=@"请输入用户名...";
//是否作为暗码输入
//YES 做为处理 圆点加密
//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];
// 设置字体色彩
_passwordfield.textColor=[UIColor blackColor];
//设置边框的风格
// UITextBorderStyleBezel bezel线框
// UITextBorderStyleRoundedRect 圆角风格
// UITextBorderStyleLine 线框风格
// UITextBorderStyleNone 无边框风格
_passwordfield.borderStyle= UITextBorderStyleRoundedRect;
//设置虚拟键盘风格
//UIKeyboardTypeDefault 默许风格
//UIKeyboardTypeNumberPad 纯数字风格
//UIKeyboardTypeNamePhonePad 字母和数字组合风格
_passwordfield.keyboardType=UIKeyboardTypeNamePhonePad;
_passwordfield.placeholder=@"请输入暗码...";
//是否作为暗码输入
//YES 做为处理 圆点加密
//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] ;
//设置按钮色彩
[_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];
//// //得到六位随机字符,可自己设长度
//// for(int i = 0; i < 6; i++) {
//// int index = arc4random() % ([strArr count]);
//// //得到数组中随机数的下标
//// [getStr appendString:[strArr objectAtIndex:index]];
//// }
//// NSLog(@"验证码:%@",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 -- 遮罩视图点击
- (void)tapClick:(UIGestureRecognizer *)tap {
NSLog(@"遮罩视图点击");
}
@end
咱们这边登录恳求成功后就把服务端回来都数据格局 同过block 回调给咱们都 游戏方 然后游戏方可以拿到咱们登录都数据去进行校验 最终完结登录都动作 注册界面也是同样都逻辑 不同都是调用都注册接口 也是注册成功后把回来都数据格局回调给游戏方 ##.数据上报
-
区服登录
// 区服登录接口
+ (void)serviceinfo:(ServiceInfo*)getserviceinfo serviceLogin:(ServiceBlock)finishBlock{
NSLog(@"区服登录");
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(@"");
}];
};
-
等级改变上报
//等级改变上报接口
+ (void)roleinfo:(RoleInfo*)getroleinfo updateUserInfo:(UpdateUserInfoBlcok)finishBlock{
NSLog(@"等级改变上报");
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详细接入完结 :
//
// 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:@"区服登录" 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:@"等级改变上报" 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:@"区服登录" 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:@"等级改变上报" 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(@"按钮点击事件");
}
#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(@"回调成功成功---%@",data);
NSLog(@"恳求 登录回调msg ---%@",[data objectForKey:(@"msg")]);
int * code =[[data objectForKey: (@"code")] intValue];
NSLog(@"登录回调 %d",code);
NSString * msg=[data objectForKey:(@"msg")];
NSLog(@"msg ----> %@",msg);
//登录校验逻辑
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(@"回调成功成功---%@",data);
NSLog(@"恳求 登录回调msg ---%@",[data objectForKey:(@"msg")]);
int * code =[[data objectForKey: (@"code")] intValue];
NSLog(@"登录回调 %d",code);
NSString * msg=[data objectForKey:(@"msg")];
NSLog(@"msg ----> %@",msg);
//登录校验逻辑
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 校验token 校验成功才是真正登录
- (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---转化格局--- %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{
}
// 区服登录
-(void)serviceloginbtn{
ServiceInfo * serviceinfo=[ServiceInfo new];
serviceinfo->_sid=@"1111";
serviceinfo->_gid=@"424251";
serviceinfo->_roleid=@"旋涡名人";
serviceinfo->_rolename=@"最强王者";
serviceinfo->_rolelevel=@"214141";
[NineFunSdk serviceinfo:serviceinfo serviceLogin:^(BOOL success) {
NSLog(@"区服登录");
}];
}
-(void)updateuserinfobtn{
RoleInfo * roleinfo= [RoleInfo new];
roleinfo->_gid=@"12232";
roleinfo->_roleid=@"422422";
roleinfo->_rolename=@"醉生梦死";
roleinfo->_sid=@"测验服务器";
roleinfo->_rolelevel=@"191";
[NineFunSdk roleinfo:roleinfo updateUserInfo:^(BOOL success) {
NSLog(@"等级改变上报");
}];
}
@end
最终总结 :
咱们都手游sdk 主要是把一套完整都功能封装成库 然后发给接入方用 所以咱们最好不要用三方库 我这儿也用 了可是我这边是直接把代码复制进来嘞 所以 问题不大 只要能避免跟接入方发生冲突 咱们就可以了 界面显现 咱们这边主张最好运用弹窗 不要运用透明的控制器 透明的控制器作用不是很好 还有便是数据存储 咱们这边主张运用 NSUserDefaults 即可 这个存储根本数据和对象都可以 。最终呢 期望我都文章能协助到各位同学工作和学习 假如你觉得文章还不错麻烦给我三连 关注点赞和转发 谢谢