123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- //
- // UserLoginPhoneViewController.m
- // XenonSDK
- //
- // Created by SAGESSE on 2019/5/29.
- // Copyright © 2019 SAGESSE. All rights reserved.
- //
- #import "XenonSDK.h"
- #import "UserLoginPhoneViewController.h"
- #import <FBSDKCoreKit/FBSDKCoreKit.h>
- #import <FBSDKLoginKit/FBSDKLoginKit.h>
- #import <AuthenticationServices/AuthenticationServices.h>
- #import "UserLoginAccountViewController.h"
- //#import <Adjust/Adjust.h>
- #import "NSBundle+KODLocalizable.h"
- #import "KODCommonConst.h"
- @interface UserLoginPhoneViewController ()<ASAuthorizationControllerDelegate,ASAuthorizationControllerPresentationContextProviding>
- @end
- @implementation UserLoginPhoneViewController
- - (NSInteger)smsType {
- return 3;
- }
- -(void)viewDidLoad {
- [super viewDidLoad];
-
- self.fastView.hidden = YES;
-
- //ios13及以上显示苹果登录功能.
- if (@available(iOS 13.0, *)) {
- self.appleLoginBtn.hidden = NO;
- }else{
- self.appleLoginBtn.hidden = YES;
- }
- }
- - (IBAction)selectButtonAction:(UIButton *)sender {
-
- self.selectBtn.selected = !self.selectBtn.selected;
- }
- - (void)automatic:(NSString*)phone {
- // If no button is retrieved, failure.
- [self loadViewIfNeeded];
-
- // Automatic data entry.
- self.phoneTextField.text = phone;
- //self.securityCode(forPrepare: button)
- }
- - (void)securityCodeForVerify:(id)sender {
- [super securityCodeForVerify:sender];
-
- // Must enter phone.
- NSString* phone = self.phoneTextField.text;
- if (phone.length == 0) {
- [XSNetwork showHudFailure:@"请输入手机号"];
- return;
- }
-
- if (!sdk_verifyPhone(phone)) {
- [XSNetwork showHudFailure:@"请输入正确的手机号格式"];
- return;
- }
-
- NSString* code = self.securityCodeTextField.text;
- if (code.length == 0) {
- [XSNetwork showHudFailure:@"请输入验证码"];
- return;
- }
-
- [XSNetwork showHudLoading];
- [XSNetwork loginWithPhone:phone code:code complete:^(id object, NSError *error) {
-
- if (error != nil) {
- [XSNetwork showHudFailure:error];
- return;
- }
-
- if (XenonSDK.sharedSDK.sdk_loginCallback) {
- XenonSDK.sharedSDK.sdk_loginCallback(object);
- }
- }];
- }
- //游客登录
- - (void)sdk_userLoginForFast:(id)sender {
-
- if (!self.selectBtn.selected) {
-
- //请勾选用户隐私协议
- NSString *tipStr = [NSBundle KOD_localizedStringForKey:SelectUserPrivacyProtocol];
- [XSNetwork showHudFailure:tipStr];
- return;
- }
-
-
- //visitor login
- // Resign editing.
- [self.view endEditing:YES];
-
- //显示游客账号和密码的界面.
- self.fastView.hidden = NO;
-
- //[XSNetwork showHudLoading];
- [XSNetwork loginWithComplete:^(id object, NSError *error) {
-
- if (error != nil) {
- [XSNetwork showHudFailure:error];
- self.fastView.hidden = YES;
- return;
- }
-
- XSUser* user1 = (XSUser *)object;
- //NSLog(@"username=%@, pwd=%@",user1.userName,user1.pwd);
- if (user1.userName.length > 0) {
- NSString *usernameStr = user1.userName;
- //您的游戏账号为
- NSString *str1 = [NSBundle KOD_localizedStringForKey:YouGameAccount];
- //您的游戏密码为
- NSString *str2 = [NSBundle KOD_localizedStringForKey:YouGamePassword];
- NSString *str = [NSString stringWithFormat:@"%@%@",str1,usernameStr];
- NSRange range1 = [str rangeOfString:usernameStr];
- NSRange range2 = [str rangeOfString:str2];
- NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:str];
- [attrStr addAttribute:NSForegroundColorAttributeName
- value:[UIColor redColor]
- range:range1];
- [attrStr addAttribute:NSForegroundColorAttributeName
- value:[UIColor blackColor]
- range:range2];
-
- self.userLabel.attributedText = attrStr;
- }
-
- if (user1.pwd.length > 0) {
- NSString *passwordStr = user1.pwd;
-
- //您的游戏账号为
- NSString *str1 = [NSBundle KOD_localizedStringForKey:YouGameAccount];
- //您的游戏密码为
- NSString *str2 = [NSBundle KOD_localizedStringForKey:YouGamePassword];
- NSString *str = [NSString stringWithFormat:@"%@%@",str1,passwordStr];
- NSRange range1 = [str rangeOfString:passwordStr];
- NSRange range2 = [str rangeOfString:str2];
- NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:str];
- [attrStr addAttribute:NSForegroundColorAttributeName
- value:[UIColor redColor]
- range:range1];
- [attrStr addAttribute:NSForegroundColorAttributeName
- value:[UIColor blackColor]
- range:range2];
- self.pwdLabel.attributedText = attrStr;
- }
-
- //延迟关闭弹窗.
- dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0/*延迟执行时间*/ * NSEC_PER_SEC));
- dispatch_after(delayTime, dispatch_get_main_queue(), ^{
- self.fastView.hidden = YES;
- [self.navigationController popViewControllerAnimated:YES];
- [XenonSDK.sharedSDK.rootViewController dismissViewControllerAnimated:YES completion:nil];
- if (XenonSDK.sharedSDK.sdk_loginCallback) {
- XenonSDK.sharedSDK.sdk_loginCallback(object);
- }
- });
- }];
- }
- //Facebook login
- - (void)sdk_userLoginForFacebook:(id)sender {
-
- if (!self.selectBtn.selected) {
-
- //请勾选用户隐私协议
- NSString *tipStr = [NSBundle KOD_localizedStringForKey:SelectUserPrivacyProtocol];
- [XSNetwork showHudFailure:tipStr];
- return;
- }
-
- //for Facebook login api
- FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
-
- [login logOut]; //清除旧的token.
- //public_profile
- [login logInWithPermissions:@[@"public_profile", @"email"]
- fromViewController:self
- handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
- NSLog(@"facebook login result.grantedPermissions = %@,error = %@",result.grantedPermissions,error);
-
- if (error) {
- [XSNetwork showHudFailure:error];
- NSLog(@"Process error");
- } else if (result.isCancelled) {
- NSLog(@"Cancelled");
- [XSNetwork showHudFailure:@"Cancelled"];
- } else {
- NSLog(@"Logged in");
- FBSDKAccessToken *token = result.token;
- NSString *appid = token.appID;
- NSString *playerid = token.userID;
- NSString *tokens = token.tokenString;
- [XSNetwork loginFacebookWithToken:tokens appId:appid playerId:playerid complete:^(id object, NSError *error) {
- if (error != nil) {
- [XSNetwork showHudFailure:error];
- return;
- }
- if (XenonSDK.sharedSDK.sdk_loginCallback) {
- XenonSDK.sharedSDK.sdk_loginCallback(object);
- }
- }];
- }
- }];
-
- }
- //Line login
- - (void)sdk_userLoginForLine:(id)sender {
- NSLog(@"line登录~");
- // [LineSDKLogin sharedInstance].delegate =self;
- // [[LineSDKLogin sharedInstance] startLoginWithPermissions:@[@"Line_login"]];
- }
- #pragma mark - -----LineSDKLoginDelegate------
- //-(void)didLogin:(LineSDKLogin *)login credential:(LineSDKCredential *)credential profile:(LineSDKProfile *)profile error:(NSError *)error {
- //
- // if (error) {
- // // Login failed with an error. Use the error parameter to identify the problem.
- // NSLog(@"Error: %@", error.localizedDescription);
- // }
- // else {
- //
- // // Login success. Extracts the access token, user profile ID, display name, status message, and profile picture.
- // NSString * accessToken = credential.accessToken.accessToken;
- // NSString * userID = profile.userID;
- // NSString * displayName = profile.displayName;
- // NSString * statusMessage = profile.statusMessage;
- // NSURL * pictureURL = profile.pictureURL;
- // NSLog(@"accessToken: %@", accessToken); // 访问令牌
- // NSLog(@"userID: %@", userID); // 用户id
- // NSLog(@"displayName: %@", displayName); // 用户昵称
- // NSLog(@"statusMessage: %@", statusMessage);
- // NSLog(@"pictureURL: %@", pictureURL); // 头像
- // }
- //}
- #pragma mark - ASAuthorizationControllerDelegate
- /// Apple登录授权出错
- - (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithError:(NSError *)error API_AVAILABLE(ios(13.0)) {
- NSLog(@"Apple登录_错误信息: %@", error.localizedDescription);
-
- NSInteger code = error.code;
- if (code == ASAuthorizationErrorUnknown) { // 授权请求未知错误
- NSLog(@"Apple登录_授权请求未知错误");
- //Apple登录授权请求未知错误
-
- NSString *tip1 = [NSBundle KOD_localizedStringForKey:AppleLoginAuthorizationErrorUnknown];
- [XSNetwork showHudFailure:tip1];
- } else if (code == ASAuthorizationErrorCanceled) { // 授权请求取消了
- NSLog(@"Apple登录_授权请求取消了");
- //Apple登录授权取消
- NSString *tip1 = [NSBundle KOD_localizedStringForKey:AppleLoginAuthorizationErrorCanceled];
- [XSNetwork showHudFailure:tip1];
-
- } else if (code == ASAuthorizationErrorInvalidResponse) { // 授权请求响应无效
- NSLog(@"Apple登录_授权请求响应无效");
- //Apple登录授权响应无效
- NSString *tip1 = [NSBundle KOD_localizedStringForKey:AppleLoginAuthorizationErrorInvalidResponse];
- [XSNetwork showHudFailure:tip1];
-
- } else if (code == ASAuthorizationErrorNotHandled) { // 授权请求未能处理
- NSLog(@"Apple登录_授权请求未能处理");
- //Apple登录授权未处理
- NSString *tip1 = [NSBundle KOD_localizedStringForKey:AppleLoginAuthorizationErrorNotHandled];
- [XSNetwork showHudFailure:tip1];
- } else if (code == ASAuthorizationErrorFailed) { // 授权请求失败
- NSLog(@"Apple登录_授权请求失败");
- //Apple登录授权失败
- NSString *tip1 = [NSBundle KOD_localizedStringForKey:AppleLoginAuthorizationErrorFailed];
- [XSNetwork showHudFailure:tip1];
- }
-
- }
- /// Apple登录授权成功
- - (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(ASAuthorization *)authorization API_AVAILABLE(ios(13.0)) {
- Class credentialClass = [authorization.credential class];
- if (credentialClass == [ASAuthorizationAppleIDCredential class]) {
- // 用户登录使用的是: ASAuthorizationAppleIDCredential,授权成功后可以取到苹果返回的全部数据,然后再与后台交互
- ASAuthorizationAppleIDCredential *credential = (ASAuthorizationAppleIDCredential *)authorization.credential;
-
- // NSString *userID = credential.user;
- NSString *identityToken = [[NSString alloc] initWithData:credential.identityToken encoding:NSUTF8StringEncoding];
- // NSLog(@"Apple登录_1_user: %@", userID);
- //这里我只用到了userID,email,[NSString stringWithFormat:@"%@%@", fullName.familyName, fullName.givenName]
- //接下来就调用自己服务器接口
- [XSNetwork loginApplekWithToken:identityToken complete:^(id object, NSError *error) {
- if (error != nil) {
- [XSNetwork showHudFailure:error];
- return;
- }
- if (XenonSDK.sharedSDK.sdk_loginCallback) {
- XenonSDK.sharedSDK.sdk_loginCallback(object);
- }
- }];
- } else if (credentialClass == [ASPasswordCredential class]) {
- // 用户登录使用的是: 现有密码凭证
- ASPasswordCredential *credential = (ASPasswordCredential *)authorization.credential;
- NSString *user = credential.user; // 密码凭证对象的用户标识(用户的唯一标识)
- NSString *password = credential.password;
- NSLog(@"Apple登录_现有密码凭证: %@, %@", user, password);
- }
- }
- #pragma mark - ASAuthorizationControllerPresentationContextProviding
- -(ASPresentationAnchor)presentationAnchorForAuthorizationController:(ASAuthorizationController *)controller API_AVAILABLE(ios(13.0)){
-
- return self.view.window;
- }
- #pragma mark - Apple login
- //Apple login
- - (void)sdk_userLoginForApple:(id)sender {
- if (!self.selectBtn.selected) {
-
- //请勾选用户隐私协议
- NSString *tipStr = [NSBundle KOD_localizedStringForKey:SelectUserPrivacyProtocol];
- [XSNetwork showHudFailure:tipStr];
- return;
- }
-
- //adjust统计登录
- // NSString *kLoginEventToken = [NSBundle mainBundle].infoDictionary[@"kLoginEventToken"];
- // ADJEvent *event = [ADJEvent eventWithEventToken:kLoginEventToken];
- // [Adjust trackEvent:event];
-
- if (@available(iOS 13.0, *)) {
- // 基于用户AppleID的授权,生成用户授权请求的一种机制
- ASAuthorizationAppleIDProvider *provider = [[ASAuthorizationAppleIDProvider alloc] init];
- // 创建新的AppleID授权请求
- ASAuthorizationAppleIDRequest *request = [provider createRequest];
- // 在用户授权期间请求的联系信息
- request.requestedScopes = @[ASAuthorizationScopeEmail, ASAuthorizationScopeFullName];
- // 由 ASAuthorizationAppleIDProvider 创建的授权请求来管理 授权请求控制器
- ASAuthorizationController *authController = [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[request]];
- authController.delegate = self;
- [authController performRequests];
- }
- }
- - (void)sdk_showViewControllerForAccountLogin:(id)sender {
-
- if(!self.selectBtn.selected) {
- //请勾选用户隐私协议
- NSString *tipStr = [NSBundle KOD_localizedStringForKey:SelectUserPrivacyProtocol];
- [XSNetwork showHudFailure:tipStr];
- return;
- }
-
- [self.view endEditing:YES];
- [self.navigationController pushViewController:UserLoginAccountViewController.new animated:YES];
- }
- @end
|