前言:一键登录不同于第三方登录,可以不接入苹果登录(苹果商店要求从2020年6月开始,接入第三方登录的应用,必须接入Apple id登录)
2021年11月2日新增内容:
最新版本SDK新加属性checkTipText,当复选框状态是未选中状态,提示勾选协议
model.checkTipText = @"请阅读并同意相关协议";
-
直接上效果图: 一键登录效果图.jpg
一、一键登录的能力和优势介绍
1.1 一键登录的能力,即通过移动认证的网络认证能力,实现APP用户无需输入帐号密码,即可使用本机手机号码自动登录的能力。利用应用层无法截取的网络层号码认证能力验证号码的真实性,本机号码自动校验是现有短信验证方式的优化,能消除现有短信验证模式等待时间长、操作繁琐和容易泄露的痛点。
1.2 一键登录的优势
● 降低应用注册/登录门槛,减轻用户记忆负担,提高用户体验;
● 降低对用户身份、通信行为等属性验证的繁琐步骤,助力企业完善风险管控系统
● 取号成功率高达99.9%。
● 两步完成注册登录,耗时仅需1.5秒。
● 节省企业短验成本
更多详细内容请查看中国移动互联网能力平台文档中心 http://dev.10086.cn/docInside?contentId=10000067529678
二、一键登录的具体接入过程
2.1 申请appid
和appkey
2.2 申请一键登录能力,创建应用,然后在页面左侧选择一键登录能力,配置应用服务器出口IP地址及验签方式。
2.3 下载SDK
,搭建开发环境
2.3.1 Xcode
版本需使用9.0以上,否则会报错
2.3.2 导入认证SDK
的framework
,直接将移动认证 TYRUIZSDK.framework
拖到项目中
2.3.3 在Xcode
中找到 TARGETS-->Build Setting-->Linking-->Other Linker Flags
在这选项中需要添加 -ObjC
或_all_load
2.3.4 资源文件:在Xcode
中务必导入TYRZResource.bundle
到项目中,否则授权界面显示异常(bundle文件里面有一些授权页面的UI)
TARGETS
-->Build Phases
-->Copy Bundle Resources
-> 点击 "+" --> Add Other
-->TYRUIZSDK.framework
--> TYRZResource.bundle
-->Open
三、一键登录的具体代码实现
3.1 在appDelegate.m
文件的 didFinishLaunchingWithOptions
函数中添加初始化代码
[UASDKLogin.shareLogin registerAppId:@"xxxxxxxx" AppKey:@"xxxxxxxx"];
3.2 在一键登录页面定制UI的实现
// 1.一键登录按钮的点击
- (void)quickLoginDidClickedLoginButton {
[self getPhoneNumber];
}
// 2.进行预约取号逻辑
- (void)getPhoneNumber {
[UASDKLogin.shareLogin getPhoneNumberCompletion:^(NSDictionary * _Nonnull result) {
NSString *resultCode = result[@"resultCode"];
if ([resultCode isEqualToString:@"103000"]) {
[self getAuthorization];
}
}];
}
// 3.获取用户授权,定制页面UI
- (void)getAuthorization {
// 定制 UI
UACustomModel *model = [[UACustomModel alloc] init];
model.currentVC = self; // 必传
model.privacyColor = [UIColor colorWithHexString:@"3478F6"];// 用户协议文字颜色
model.logBtnOffsetY = @(kScreenHeight/2.0);
model.numberOffsetY = @(kScreenHeight/2.0 - 50);
model.privacyOffsetY = @(kScreenHeight/2.0 + 70);
model.webNavColor = [UIColor colorWithHexString:@"3478F6"];
// 添加返回按钮
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[backBtn addTarget:self action:@selector(backButtonAction) forControlEvents:UIControlEventTouchUpInside];
[backBtn setImage:[UIImage imageNamed:@"newItem_backacc"] forState:UIControlStateNormal];
[backBtn setImage:[UIImage imageNamed:@"newItem_backacc"] forState:UIControlStateHighlighted];
backBtn.frame = CGRectMake(5, kStatusBarHeight, 44, 44);
// 添加用户协议和隐私政策
UILabel *acceptLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, (kScreenHeight/2.0 + 70) + 20, 200, 20)];
NSMutableAttributedString *text = [[NSMutableAttributedString alloc]initWithString:@"以及 战鹰用户协议"];
text.font = [UIFont systemFontOfSize:12];
text.color = [UIColor colorWithHexString:@"999999"];
[text addAttribute:NSForegroundColorAttributeName value:kAssistColor range:NSMakeRange(3,6)];
acceptLabel.attributedText = text;
acceptLabel.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickProtocal)];
[tapGesture setNumberOfTapsRequired:1];
[acceptLabel addGestureRecognizer:tapGesture];
acceptLabel.textAlignment = NSTextAlignmentCenter;
acceptLabel.centerX = kScreenWidth/2.0;
// 添加logo
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, kScreenHeight/2.0 - 50 - 150, 110, 110)];
imageView.centerX = kScreenWidth/2.0;
imageView.contentMode = UIViewContentModeScaleToFill;
imageView.image = [UIImage imageNamed:@"login_logo_new4"];
// 替换复选框按钮
model.uncheckedImg = [UIImage imageNamed:@"protocal_unselect"];
model.checkedImg = [UIImage imageNamed:@"protocal_select"];
model.checkboxWH = @(13);
model.privacyState = YES;
// 最新版本SDK新加属性,当复选框状态是未选中状态,提示勾选协议
// model.checkTipText = @"请阅读并同意相关协议";
NSInteger leftMargin = HAS_SAFE_BOTTOM ? 45 : 35;
model.logBtnHeight = (kScreenWidth - leftMargin * 2)*100/610.0;
model.logBtnOriginLR = @[@(leftMargin),@(leftMargin)];
// 替换登录按钮
model.logBtnImgs = @[[UIImage imageNamed:@"quick_login_selected"],[UIImage imageNamed:@"quick_login_unselected"],[UIImage imageNamed:@"quick_login_selected"]];
NSInteger appPrivacyMargin = HAS_SAFE_BOTTOM ? 45 : 25;
model.appPrivacyOriginLR = @[@(appPrivacyMargin),@(appPrivacyMargin)];
model.appPrivacyAlignment = NSTextAlignmentCenter;
model.authViewBlock = ^(UIView *customView, CGRect numberFrame, CGRect loginBtnFrame, CGRect checkBoxFrame, CGRect privacyFrame) {
customView.backgroundColor = [UIColor whiteColor];
[customView addSubview:imageView];
[customView addSubview:backBtn];
[customView addSubview:acceptLabel];
};
// 添加自定义协议的另外一种方式
// model.appPrivacyDemo = [[NSAttributedString alloc] initWithString:@"登录并同意&&默认&&和战鹰用户协议进行本机号码登录" attributes:@{
// NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor blackColor]}];
// NSAttributedString *str1 = [[NSAttributedString alloc]initWithString:@"战鹰用户协议" attributes:@{NSLinkAttributeName:@"https://yuqing.wiseweb.com.cn/clause/userAgreement.html"}];
// model.privacySymbol = YES;
// model.appPrivacy = @[str1];
// model.privacyColor = [UIColor colorWithHexString:@"3478F6"];
// 获取用户授权
[UASDKLogin.shareLogin getAuthorizationWithModel:model complete:^(id _Nonnull sender) {
NSString *resultCode = sender[@"resultCode"];
NSDictionary *dict = sender;
NSLog(@"返回结果-%@",sender);
if ([resultCode isEqualToString:@"200087"]) {
NSLog(@"成功拉起授权页");
}
// 授权成功后,移动认证会返回一个token给我们
if ([resultCode isEqualToString:@"103000"]) {
NSString *appid = @"300011976072";
NSString *version = @"2.0";
NSString *msgid = [self getRandomStringWithNum: 18];
NSString *systemtime = [self getCurrentTime];
NSString *strictcheck = @"0";
NSString *token = ([dict containsObjectForKey:@"token"]) ? dict[@"token"] : @"";
NSString *appSecret = @"85E08208672145CC8E44855AF7C2B487";
NSLog(@"获取移动token-%@",token);
NSString *str = [NSString stringWithFormat:@"%@%@%@%@%@%@%@",appid,version,msgid,systemtime,strictcheck,token,appSecret];
NSLog(@"获取str-%@",str);
NSString *sign = [self md5ForUpper32Bate:str];
NSLog(@"获取MD5-%@",sign);
NSDictionary *params = @{@"appid":appid,
@"version":version,
@"msgid":msgid,
@"systemtime":systemtime,
@"strictcheck":strictcheck,
@"token":token,
@"sign":sign
};
// 拿到token后,配置一些其他的参数,向自己的后台请求基本信息就可以了
[self getPhoneNumberFromServicesWithParams:params];
}
}];
}
#pragma mark - 通过一系列参数,向后台获取手机号
- (void)getPhoneNumberFromServicesWithParams:(NSDictionary *)params {
[WEHttpRequest dataTaskWithConfig:^(WEUrlRequestConfig * _Nonnull request) {
request.methodType = WEHttpMethodPOST;
request.baseUrl = HostUrl;
request.url = @"app/one-click/phone";
request.parameters = params;
[request setValue:[WELoginManager sharedInstance].accessToken forHeaderField:kAccessToken];
[request setValue:@"application/json" forHeaderField:@"Content-Type"];
} success:^(NSDictionary * _Nullable responseObject) {
if ([responseObject[@"httpCode"] intValue] == 200) {
if ([responseObject containsObjectForKey:@"data"] && responseObject[@"data"] != nil) {
NSString *phoneNum = responseObject[@"data"];
[self quickLoginCompleteWithPhone:phoneNum];
}
}
} failed:^(NSError * _Nullable error) {
NSLog(@"服务器取号失败");
}];
}
// 用获取到的手机号进行登录即可
- (void)quickLoginCompleteWithPhone:(NSString *)phone {
[[WELoginManager sharedInstance] quickloginWithPhone:phone successed:^{
[self dismissViewControllerAnimated:YES completion:nil]; // 关闭授权弹窗
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
appDelegate.userLoging = YES;
appDelegate.rootView = [[WERootViewController alloc]init];
WERootViewController *rootC = appDelegate.rootView;
rootC.selectedIndex = 0;
[rootC setChildrenRefresh:YES];
[self reloadLoginBtnEnabled];
[UIApplication sharedApplication].delegate.window.rootViewController = rootC;
[[NSNotificationCenter defaultCenter] postNotificationName:@"loging" object:nil userInfo:nil];
appDelegate.loginView = nil; // 现将登录成功后的登录页面置为nil,否则会保留当前页面的属性信息(self.isVistorLog)!影响逻辑
// 只需要在使用SDK功能之前调用即可,上传隐私协议授权状态
// 注册登录完成后,上传隐私协议
[MobSDK uploadPrivacyPermissionStatus:YES onResult:^(BOOL success) {
NSLog(@"用户登录-同意隐私政策");
}];
} failed:^(NSError * _Nullable error, NSDictionary * _Nullable info) {
[WEHUD showWarningHUD:@"登录失败"];
}];
}
// 点击返回
- (void)backButtonAction {
[self dismissViewControllerAnimated:YES completion:nil]; // 关闭授权弹窗
}
// 点击自定义用户协议
- (void)clickProtocal {
WEProtocolViewController *protocolC = [[WEProtocolViewController alloc]init];
protocolC.modalPresentationStyle = UIModalPresentationFullScreen;
[self.presentedViewController presentViewController:protocolC animated:YES completion:nil];
}
// 获取当前时间
- (NSString *)getCurrentTime {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyyMMddHHmmssSSS"];
NSString *dateString = [formatter stringFromDate: [NSDate date]];
NSLog(@"当前的时间戳-%@",dateString);
return dateString;
}
// 获取随机数
- (NSString *)getRandomStringWithNum:(NSInteger)num{
NSString *string = [[NSString alloc]init];
for (int i = 0; i < num; i++) {
int number = arc4random() % 36;
if (number < 10) {
int figure = arc4random() % 10;
NSString *tempString = [NSString stringWithFormat:@"%d", figure];
string = [string stringByAppendingString:tempString];
}else {
int figure = (arc4random() % 26) + 97;
char character = figure;
NSString *tempString = [NSString stringWithFormat:@"%c", character];
string = [string stringByAppendingString:tempString];
}
}
NSLog(@"获取的随机数%@",string);
return string;
}
#pragma mark - 32位 大写 (MD5加密)
- (NSString *)md5ForUpper32Bate:(NSString *)str{
//要进行UTF8的转码
const char* input = [str UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(input, (CC_LONG)strlen(input), result);
NSMutableString *digest = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for (NSInteger i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
[digest appendFormat:@"%02X", result[i]];
}
return digest;
}