LoginThirdAuthV.m 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //
  2. // LoginThirdAuthV.m
  3. // Asteria
  4. //
  5. // Created by 王猛 on 2023/5/16.
  6. //
  7. #import "LoginThirdAuthV.h"
  8. #import <AuthenticationServices/AuthenticationServices.h>
  9. #import <GoogleSignIn/GoogleSignIn.h>
  10. #import <FBSDKLoginKit/FBSDKLoginKit.h>
  11. #define LoginBtnTag 10000
  12. @interface LoginThirdAuthV ()<ASAuthorizationControllerDelegate,ASAuthorizationControllerPresentationContextProviding>
  13. @end
  14. @implementation LoginThirdAuthV
  15. - (void)tt_setupViews{
  16. UILabel *tipLab = [[UILabel alloc]init];
  17. tipLab.text = @"Continue With";
  18. tipLab.font = [UIFont fontWithName:Rob_Regular size:12];
  19. [self addSubview:tipLab];
  20. tipLab.frame = CGRectMake(20, 0, KScreenWidth-40, 15);
  21. NSArray *titleAry = @[@"Apple",@"Facebook",@"Google"];
  22. NSArray *imgAry = @[@"login_Apple",@"login_Facebook",@"login_Google"];
  23. for (int i= 0; i<titleAry.count; i++) {
  24. QMUIButton *btn = [self laod_authItem:titleAry[i] img:imgAry[i]];
  25. btn.tag = LoginBtnTag + i;
  26. [btn addTarget:self action:@selector(handle_BtnEvent:) forControlEvents:UIControlEventTouchUpInside];
  27. btn.mj_y = CGRectGetMaxY(tipLab.frame)+20 + (36+10)*i;
  28. [self addSubview:btn];
  29. }
  30. QMUIButton *tepBtn = [self viewWithTag:LoginBtnTag];
  31. if(@available(iOS 13.0, *)){
  32. tepBtn.hidden = NO;
  33. }else{
  34. tepBtn.hidden = YES;
  35. }
  36. }
  37. -(CGFloat)xxx_Vheight{
  38. return 15+20+(36+10)*3;
  39. }
  40. -(QMUIButton *)laod_authItem:(NSString *)title img:(NSString *)imgStr{
  41. QMUIButton *authBtn = [[QMUIButton alloc] initWithFrame:CGRectMake(20, 0, 132, 36)];
  42. authBtn.backgroundColor = ThemeLightColor;
  43. authBtn.imageView.qmui_size = CGSizeMake(24, 24);
  44. [authBtn setImage:[UIImage imageNamed:imgStr] forState:UIControlStateNormal];
  45. [authBtn setTitle:title forState:UIControlStateNormal];
  46. [authBtn setTitleColor:[UIColor colorWithHexString:@"#000000"] forState:UIControlStateNormal];
  47. authBtn.titleLabel.font = [UIFont fontWithName:Rob_Regular size:12];
  48. authBtn.cornerRadius = 4;
  49. authBtn.spacingBetweenImageAndTitle = 20.0f;
  50. CGSize imageSize = authBtn.imageView.frame.size;
  51. CGSize titleSize = authBtn.titleLabel.frame.size;
  52. authBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 132-imageSize.width-titleSize.width-20-10);
  53. authBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
  54. return authBtn;
  55. }
  56. -(void)handle_BtnEvent:(UIButton *)btn{
  57. switch (btn.tag) {
  58. case LoginBtnTag:
  59. [self tool_apple];
  60. break;
  61. case LoginBtnTag+1:
  62. [self tool_facebook];
  63. break;
  64. case LoginBtnTag+2:
  65. [self tool_google];
  66. break;
  67. default:
  68. break;
  69. }
  70. }
  71. -(void)tool_apple{
  72. if(@available(iOS 13.0, *)){
  73. ASAuthorizationAppleIDProvider *appleProvider = [[ASAuthorizationAppleIDProvider alloc]init];
  74. ASAuthorizationAppleIDRequest *request = appleProvider.createRequest;
  75. request.requestedScopes = @[ASAuthorizationScopeEmail,ASAuthorizationScopeFullName];
  76. ASAuthorizationController *controller = [[ASAuthorizationController alloc]initWithAuthorizationRequests:@[request]];
  77. controller.delegate = self;
  78. controller.presentationContextProvider = self;
  79. [controller performRequests];
  80. }
  81. }
  82. #pragma mark - **************** ASAuthorizationControllerDelegate ****************
  83. ///授权成功回调
  84. - (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(nonnull ASAuthorization *)authorization API_AVAILABLE(ios(13.0)){
  85. if([authorization.credential isKindOfClass:[ASAuthorizationAppleIDCredential class]]){
  86. ASAuthorizationAppleIDCredential *credential = (ASAuthorizationAppleIDCredential *)authorization.credential;
  87. NSString *user = credential.user;
  88. NSString *email = credential.email;
  89. NSData *identityToken = credential.identityToken;
  90. NSLog(@"user - %@ --- %@--%@",user,email,identityToken);
  91. }else if([authorization.credential isKindOfClass:[ASPasswordCredential class]]){
  92. ASPasswordCredential *psdCredential = (ASPasswordCredential *)authorization.credential;
  93. // 密码凭证对象的用户标识 用户的唯一标识
  94. NSString *user = psdCredential.user;
  95. NSString *psd = psdCredential.password;
  96. NSLog(@"psduer -- %@----%@",user,psd);
  97. }
  98. }
  99. //授权失败
  100. - (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithError:(NSError *)error API_AVAILABLE(ios(13.0)){
  101. NSLog(@"错误信息:%@", error);
  102. NSString *errorMsg;
  103. switch (error.code) {
  104. case ASAuthorizationErrorCanceled:
  105. errorMsg = @"用户取消了授权请求";
  106. NSLog(@"errorMsg - %@",errorMsg);
  107. break;
  108. case ASAuthorizationErrorFailed:
  109. errorMsg = @"授权请求失败";
  110. NSLog(@"errorMsg - %@",errorMsg);
  111. break;
  112. case ASAuthorizationErrorInvalidResponse:
  113. errorMsg = @"授权请求响应无效";
  114. NSLog(@"errorMsg - %@",errorMsg);
  115. break;
  116. case ASAuthorizationErrorNotHandled:
  117. errorMsg = @"未能处理授权请求";
  118. NSLog(@"errorMsg - %@",errorMsg);
  119. break;
  120. case ASAuthorizationErrorUnknown:
  121. errorMsg = @"授权请求失败未知原因";
  122. NSLog(@"errorMsg - %@",errorMsg);
  123. break;
  124. default:
  125. break;
  126. }
  127. }
  128. - (ASPresentationAnchor)presentationAnchorForAuthorizationController:(ASAuthorizationController *)controller API_AVAILABLE(ios(13.0)){
  129. UIViewController *topvc = topViewController();
  130. return topvc.view.window;
  131. }
  132. -(void)tool_facebook{
  133. FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
  134. UIViewController *topvc = topViewController();
  135. [MBProgressHUD showHUDAddedTo:topvc.view animated:YES];
  136. [loginManager logInWithPermissions:@[@"public_profile", @"email"] fromViewController:topvc handler:^(FBSDKLoginManagerLoginResult * _Nullable result, NSError * _Nullable error) {
  137. if(error){
  138. [MBProgressHUD hideHUDForView:topvc.view animated:YES];
  139. NSLog(@"NSError-----%@",error);
  140. return;
  141. }else if(result.isCancelled){
  142. [MBProgressHUD hideHUDForView:topvc.view animated:YES];
  143. NSLog(@"Face---Cancelled");
  144. }else{
  145. NSDictionary*params= @{@"fields":@"id,name,email,age_range,first_name,last_name,link,gender,locale,picture,timezone,updated_time,verified"};
  146. NSString *tmpToken = MM_str(result.token.tokenString);
  147. FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
  148. initWithGraphPath:result.token.userID
  149. parameters:params
  150. HTTPMethod:@"GET"];
  151. [request startWithCompletion:^(id<FBSDKGraphRequestConnecting> _Nullable connection, id _Nullable result, NSError * _Nullable error) {
  152. [MBProgressHUD hideHUDForView:topvc.view animated:YES];
  153. NSLog(@"%@",result[@"name"]);
  154. if (error == nil) {
  155. NSMutableDictionary *paramsDic= [NSMutableDictionary dictionary];
  156. [paramsDic setObject:@(1) forKey:@"type"];
  157. [paramsDic setObject:tmpToken forKey:@"token"];
  158. [paramsDic setObject:MM_str(result[@"email"]) forKey:@"email"];
  159. [paramsDic setObject:MM_str(result[@"id"]) forKey:@"uid"];
  160. [paramsDic setObject:MM_str(result[@"first_name"]) forKey:@"firstName"];
  161. [paramsDic setObject:MM_str(result[@"last_name"]) forKey:@"lastName"];
  162. [paramsDic setObject:@"" forKey:@"birthday"];
  163. [paramsDic setObject:@"" forKey:@"gender"];
  164. NSLog(@"paramsDic----%@",paramsDic);
  165. [self generaltriggermethodType:ThirdTypeFaceBook data:paramsDic];
  166. }else{
  167. [topvc.view makeToast:[NSString stringWithFormat:@"Login failed with error code:%ld",error.code] duration:3 position:CSToastPositionCenter];
  168. }
  169. }];
  170. }
  171. }];
  172. }
  173. -(void)tool_google{
  174. UIViewController *topvc = topViewController();
  175. [MBProgressHUD showHUDAddedTo:topvc.view animated:YES];
  176. [GIDSignIn.sharedInstance signInWithPresentingViewController:topvc completion:^(GIDSignInResult * _Nullable signInResult, NSError * _Nullable error) {
  177. if (error) {
  178. NSLog(@"NSError-----%@",error);
  179. return;
  180. }
  181. GIDGoogleUser *user = signInResult.user;
  182. if (user == nil) { return; }
  183. NSString *name = user.profile.name;
  184. NSString *givenName = user.profile.givenName;
  185. NSString *familyName = user.profile.familyName;
  186. NSMutableDictionary *paramsDic= [NSMutableDictionary dictionary];
  187. [paramsDic setObject:@(2) forKey:@"type"];
  188. [paramsDic setObject:user.accessToken forKey:@"token"];
  189. [paramsDic setObject:user.profile.email forKey:@"email"];
  190. [paramsDic setObject:user.userID forKey:@"uid"];
  191. [paramsDic setObject:familyName forKey:@"firstName"];
  192. [paramsDic setObject:givenName forKey:@"lastName"];
  193. [paramsDic setObject:@"" forKey:@"birthday"];
  194. [paramsDic setObject:@"" forKey:@"gender"];
  195. NSLog(@"paramsDic----%@",paramsDic);
  196. [self generaltriggermethodType:ThirdTypeGoogle data:paramsDic];
  197. }];
  198. }
  199. @end