UserLoginPhoneViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. //
  2. // UserLoginPhoneViewController.m
  3. // XenonSDK
  4. //
  5. // Created by SAGESSE on 2019/5/29.
  6. // Copyright © 2019 SAGESSE. All rights reserved.
  7. //
  8. #import "XenonSDK.h"
  9. #import "UserLoginPhoneViewController.h"
  10. #import <FBSDKCoreKit/FBSDKCoreKit.h>
  11. #import <FBSDKLoginKit/FBSDKLoginKit.h>
  12. #import <AuthenticationServices/AuthenticationServices.h>
  13. #import "UserLoginAccountViewController.h"
  14. //#import <Adjust/Adjust.h>
  15. #import "NSBundle+KODLocalizable.h"
  16. #import "KODCommonConst.h"
  17. @interface UserLoginPhoneViewController ()<ASAuthorizationControllerDelegate,ASAuthorizationControllerPresentationContextProviding>
  18. @end
  19. @implementation UserLoginPhoneViewController
  20. - (NSInteger)smsType {
  21. return 3;
  22. }
  23. -(void)viewDidLoad {
  24. [super viewDidLoad];
  25. self.fastView.hidden = YES;
  26. //ios13及以上显示苹果登录功能.
  27. if (@available(iOS 13.0, *)) {
  28. self.appleLoginBtn.hidden = NO;
  29. }else{
  30. self.appleLoginBtn.hidden = YES;
  31. }
  32. }
  33. - (IBAction)selectButtonAction:(UIButton *)sender {
  34. self.selectBtn.selected = !self.selectBtn.selected;
  35. }
  36. - (void)automatic:(NSString*)phone {
  37. // If no button is retrieved, failure.
  38. [self loadViewIfNeeded];
  39. // Automatic data entry.
  40. self.phoneTextField.text = phone;
  41. //self.securityCode(forPrepare: button)
  42. }
  43. - (void)securityCodeForVerify:(id)sender {
  44. [super securityCodeForVerify:sender];
  45. // Must enter phone.
  46. NSString* phone = self.phoneTextField.text;
  47. if (phone.length == 0) {
  48. [XSNetwork showHudFailure:@"请输入手机号"];
  49. return;
  50. }
  51. if (!sdk_verifyPhone(phone)) {
  52. [XSNetwork showHudFailure:@"请输入正确的手机号格式"];
  53. return;
  54. }
  55. NSString* code = self.securityCodeTextField.text;
  56. if (code.length == 0) {
  57. [XSNetwork showHudFailure:@"请输入验证码"];
  58. return;
  59. }
  60. [XSNetwork showHudLoading];
  61. [XSNetwork loginWithPhone:phone code:code complete:^(id object, NSError *error) {
  62. if (error != nil) {
  63. [XSNetwork showHudFailure:error];
  64. return;
  65. }
  66. if (XenonSDK.sharedSDK.sdk_loginCallback) {
  67. XenonSDK.sharedSDK.sdk_loginCallback(object);
  68. }
  69. }];
  70. }
  71. //游客登录
  72. - (void)sdk_userLoginForFast:(id)sender {
  73. if (!self.selectBtn.selected) {
  74. //请勾选用户隐私协议
  75. NSString *tipStr = [NSBundle KOD_localizedStringForKey:SelectUserPrivacyProtocol];
  76. [XSNetwork showHudFailure:tipStr];
  77. return;
  78. }
  79. //visitor login
  80. // Resign editing.
  81. [self.view endEditing:YES];
  82. //显示游客账号和密码的界面.
  83. self.fastView.hidden = NO;
  84. //[XSNetwork showHudLoading];
  85. [XSNetwork loginWithComplete:^(id object, NSError *error) {
  86. if (error != nil) {
  87. [XSNetwork showHudFailure:error];
  88. self.fastView.hidden = YES;
  89. return;
  90. }
  91. XSUser* user1 = (XSUser *)object;
  92. //NSLog(@"username=%@, pwd=%@",user1.userName,user1.pwd);
  93. if (user1.userName.length > 0) {
  94. NSString *usernameStr = user1.userName;
  95. //您的游戏账号为
  96. NSString *str1 = [NSBundle KOD_localizedStringForKey:YouGameAccount];
  97. //您的游戏密码为
  98. NSString *str2 = [NSBundle KOD_localizedStringForKey:YouGamePassword];
  99. NSString *str = [NSString stringWithFormat:@"%@%@",str1,usernameStr];
  100. NSRange range1 = [str rangeOfString:usernameStr];
  101. NSRange range2 = [str rangeOfString:str2];
  102. NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:str];
  103. [attrStr addAttribute:NSForegroundColorAttributeName
  104. value:[UIColor redColor]
  105. range:range1];
  106. [attrStr addAttribute:NSForegroundColorAttributeName
  107. value:[UIColor blackColor]
  108. range:range2];
  109. self.userLabel.attributedText = attrStr;
  110. }
  111. if (user1.pwd.length > 0) {
  112. NSString *passwordStr = user1.pwd;
  113. //您的游戏账号为
  114. NSString *str1 = [NSBundle KOD_localizedStringForKey:YouGameAccount];
  115. //您的游戏密码为
  116. NSString *str2 = [NSBundle KOD_localizedStringForKey:YouGamePassword];
  117. NSString *str = [NSString stringWithFormat:@"%@%@",str1,passwordStr];
  118. NSRange range1 = [str rangeOfString:passwordStr];
  119. NSRange range2 = [str rangeOfString:str2];
  120. NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:str];
  121. [attrStr addAttribute:NSForegroundColorAttributeName
  122. value:[UIColor redColor]
  123. range:range1];
  124. [attrStr addAttribute:NSForegroundColorAttributeName
  125. value:[UIColor blackColor]
  126. range:range2];
  127. self.pwdLabel.attributedText = attrStr;
  128. }
  129. //延迟关闭弹窗.
  130. dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0/*延迟执行时间*/ * NSEC_PER_SEC));
  131. dispatch_after(delayTime, dispatch_get_main_queue(), ^{
  132. self.fastView.hidden = YES;
  133. [self.navigationController popViewControllerAnimated:YES];
  134. [XenonSDK.sharedSDK.rootViewController dismissViewControllerAnimated:YES completion:nil];
  135. if (XenonSDK.sharedSDK.sdk_loginCallback) {
  136. XenonSDK.sharedSDK.sdk_loginCallback(object);
  137. }
  138. });
  139. }];
  140. }
  141. //Facebook login
  142. - (void)sdk_userLoginForFacebook:(id)sender {
  143. if (!self.selectBtn.selected) {
  144. //请勾选用户隐私协议
  145. NSString *tipStr = [NSBundle KOD_localizedStringForKey:SelectUserPrivacyProtocol];
  146. [XSNetwork showHudFailure:tipStr];
  147. return;
  148. }
  149. //for Facebook login api
  150. FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
  151. [login logOut]; //清除旧的token.
  152. //public_profile
  153. [login logInWithPermissions:@[@"public_profile", @"email"]
  154. fromViewController:self
  155. handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
  156. NSLog(@"facebook login result.grantedPermissions = %@,error = %@",result.grantedPermissions,error);
  157. if (error) {
  158. [XSNetwork showHudFailure:error];
  159. NSLog(@"Process error");
  160. } else if (result.isCancelled) {
  161. NSLog(@"Cancelled");
  162. [XSNetwork showHudFailure:@"Cancelled"];
  163. } else {
  164. NSLog(@"Logged in");
  165. FBSDKAccessToken *token = result.token;
  166. NSString *appid = token.appID;
  167. NSString *playerid = token.userID;
  168. NSString *tokens = token.tokenString;
  169. [XSNetwork loginFacebookWithToken:tokens appId:appid playerId:playerid complete:^(id object, NSError *error) {
  170. if (error != nil) {
  171. [XSNetwork showHudFailure:error];
  172. return;
  173. }
  174. if (XenonSDK.sharedSDK.sdk_loginCallback) {
  175. XenonSDK.sharedSDK.sdk_loginCallback(object);
  176. }
  177. }];
  178. }
  179. }];
  180. }
  181. //Line login
  182. - (void)sdk_userLoginForLine:(id)sender {
  183. NSLog(@"line登录~");
  184. // [LineSDKLogin sharedInstance].delegate =self;
  185. // [[LineSDKLogin sharedInstance] startLoginWithPermissions:@[@"Line_login"]];
  186. }
  187. #pragma mark - -----LineSDKLoginDelegate------
  188. //-(void)didLogin:(LineSDKLogin *)login credential:(LineSDKCredential *)credential profile:(LineSDKProfile *)profile error:(NSError *)error {
  189. //
  190. // if (error) {
  191. // // Login failed with an error. Use the error parameter to identify the problem.
  192. // NSLog(@"Error: %@", error.localizedDescription);
  193. // }
  194. // else {
  195. //
  196. // // Login success. Extracts the access token, user profile ID, display name, status message, and profile picture.
  197. // NSString * accessToken = credential.accessToken.accessToken;
  198. // NSString * userID = profile.userID;
  199. // NSString * displayName = profile.displayName;
  200. // NSString * statusMessage = profile.statusMessage;
  201. // NSURL * pictureURL = profile.pictureURL;
  202. // NSLog(@"accessToken: %@", accessToken); // 访问令牌
  203. // NSLog(@"userID: %@", userID); // 用户id
  204. // NSLog(@"displayName: %@", displayName); // 用户昵称
  205. // NSLog(@"statusMessage: %@", statusMessage);
  206. // NSLog(@"pictureURL: %@", pictureURL); // 头像
  207. // }
  208. //}
  209. #pragma mark - ASAuthorizationControllerDelegate
  210. /// Apple登录授权出错
  211. - (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithError:(NSError *)error API_AVAILABLE(ios(13.0)) {
  212. NSLog(@"Apple登录_错误信息: %@", error.localizedDescription);
  213. NSInteger code = error.code;
  214. if (code == ASAuthorizationErrorUnknown) { // 授权请求未知错误
  215. NSLog(@"Apple登录_授权请求未知错误");
  216. //Apple登录授权请求未知错误
  217. NSString *tip1 = [NSBundle KOD_localizedStringForKey:AppleLoginAuthorizationErrorUnknown];
  218. [XSNetwork showHudFailure:tip1];
  219. } else if (code == ASAuthorizationErrorCanceled) { // 授权请求取消了
  220. NSLog(@"Apple登录_授权请求取消了");
  221. //Apple登录授权取消
  222. NSString *tip1 = [NSBundle KOD_localizedStringForKey:AppleLoginAuthorizationErrorCanceled];
  223. [XSNetwork showHudFailure:tip1];
  224. } else if (code == ASAuthorizationErrorInvalidResponse) { // 授权请求响应无效
  225. NSLog(@"Apple登录_授权请求响应无效");
  226. //Apple登录授权响应无效
  227. NSString *tip1 = [NSBundle KOD_localizedStringForKey:AppleLoginAuthorizationErrorInvalidResponse];
  228. [XSNetwork showHudFailure:tip1];
  229. } else if (code == ASAuthorizationErrorNotHandled) { // 授权请求未能处理
  230. NSLog(@"Apple登录_授权请求未能处理");
  231. //Apple登录授权未处理
  232. NSString *tip1 = [NSBundle KOD_localizedStringForKey:AppleLoginAuthorizationErrorNotHandled];
  233. [XSNetwork showHudFailure:tip1];
  234. } else if (code == ASAuthorizationErrorFailed) { // 授权请求失败
  235. NSLog(@"Apple登录_授权请求失败");
  236. //Apple登录授权失败
  237. NSString *tip1 = [NSBundle KOD_localizedStringForKey:AppleLoginAuthorizationErrorFailed];
  238. [XSNetwork showHudFailure:tip1];
  239. }
  240. }
  241. /// Apple登录授权成功
  242. - (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(ASAuthorization *)authorization API_AVAILABLE(ios(13.0)) {
  243. Class credentialClass = [authorization.credential class];
  244. if (credentialClass == [ASAuthorizationAppleIDCredential class]) {
  245. // 用户登录使用的是: ASAuthorizationAppleIDCredential,授权成功后可以取到苹果返回的全部数据,然后再与后台交互
  246. ASAuthorizationAppleIDCredential *credential = (ASAuthorizationAppleIDCredential *)authorization.credential;
  247. // NSString *userID = credential.user;
  248. NSString *identityToken = [[NSString alloc] initWithData:credential.identityToken encoding:NSUTF8StringEncoding];
  249. // NSLog(@"Apple登录_1_user: %@", userID);
  250. //这里我只用到了userID,email,[NSString stringWithFormat:@"%@%@", fullName.familyName, fullName.givenName]
  251. //接下来就调用自己服务器接口
  252. [XSNetwork loginApplekWithToken:identityToken complete:^(id object, NSError *error) {
  253. if (error != nil) {
  254. [XSNetwork showHudFailure:error];
  255. return;
  256. }
  257. if (XenonSDK.sharedSDK.sdk_loginCallback) {
  258. XenonSDK.sharedSDK.sdk_loginCallback(object);
  259. }
  260. }];
  261. } else if (credentialClass == [ASPasswordCredential class]) {
  262. // 用户登录使用的是: 现有密码凭证
  263. ASPasswordCredential *credential = (ASPasswordCredential *)authorization.credential;
  264. NSString *user = credential.user; // 密码凭证对象的用户标识(用户的唯一标识)
  265. NSString *password = credential.password;
  266. NSLog(@"Apple登录_现有密码凭证: %@, %@", user, password);
  267. }
  268. }
  269. #pragma mark - ASAuthorizationControllerPresentationContextProviding
  270. -(ASPresentationAnchor)presentationAnchorForAuthorizationController:(ASAuthorizationController *)controller API_AVAILABLE(ios(13.0)){
  271. return self.view.window;
  272. }
  273. #pragma mark - Apple login
  274. //Apple login
  275. - (void)sdk_userLoginForApple:(id)sender {
  276. if (!self.selectBtn.selected) {
  277. //请勾选用户隐私协议
  278. NSString *tipStr = [NSBundle KOD_localizedStringForKey:SelectUserPrivacyProtocol];
  279. [XSNetwork showHudFailure:tipStr];
  280. return;
  281. }
  282. //adjust统计登录
  283. // NSString *kLoginEventToken = [NSBundle mainBundle].infoDictionary[@"kLoginEventToken"];
  284. // ADJEvent *event = [ADJEvent eventWithEventToken:kLoginEventToken];
  285. // [Adjust trackEvent:event];
  286. if (@available(iOS 13.0, *)) {
  287. // 基于用户AppleID的授权,生成用户授权请求的一种机制
  288. ASAuthorizationAppleIDProvider *provider = [[ASAuthorizationAppleIDProvider alloc] init];
  289. // 创建新的AppleID授权请求
  290. ASAuthorizationAppleIDRequest *request = [provider createRequest];
  291. // 在用户授权期间请求的联系信息
  292. request.requestedScopes = @[ASAuthorizationScopeEmail, ASAuthorizationScopeFullName];
  293. // 由 ASAuthorizationAppleIDProvider 创建的授权请求来管理 授权请求控制器
  294. ASAuthorizationController *authController = [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[request]];
  295. authController.delegate = self;
  296. [authController performRequests];
  297. }
  298. }
  299. - (void)sdk_showViewControllerForAccountLogin:(id)sender {
  300. if(!self.selectBtn.selected) {
  301. //请勾选用户隐私协议
  302. NSString *tipStr = [NSBundle KOD_localizedStringForKey:SelectUserPrivacyProtocol];
  303. [XSNetwork showHudFailure:tipStr];
  304. return;
  305. }
  306. [self.view endEditing:YES];
  307. [self.navigationController pushViewController:UserLoginAccountViewController.new animated:YES];
  308. }
  309. @end