LoginThirdAuthV.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 *identityToken = [[NSString alloc] initWithData:credential.identityToken encoding:NSUTF8StringEncoding];
  88. NSMutableDictionary *paramsDic= [NSMutableDictionary dictionary];
  89. NSDictionary *userInfo = @{@"identifier":AS_String_NotNull(credential.user) ,
  90. @"email":AS_String_NotNull(credential.email),
  91. @"firstName":AS_String_NotNull(credential.fullName.familyName),
  92. @"lastName":AS_String_NotNull(credential.fullName.givenName),
  93. @"displayName":@""};
  94. NSString *userInfoStr = [userInfo mj_JSONString];
  95. [paramsDic setObject:userInfoStr forKey:@"userInfo"];
  96. [paramsDic setObject:@"apple" forKey:@"hauth_done"];
  97. [paramsDic setObject:identityToken forKey:@"token"];
  98. NSLog(@"paramsDic----%@",paramsDic);
  99. [self generaltriggermethodType:ThirdTypeApple data:paramsDic];
  100. }else if([authorization.credential isKindOfClass:[ASPasswordCredential class]]){
  101. ASPasswordCredential *psdCredential = (ASPasswordCredential *)authorization.credential;
  102. // 密码凭证对象的用户标识 用户的唯一标识
  103. NSString *user = psdCredential.user;
  104. NSString *psd = psdCredential.password;
  105. NSLog(@"psduer -- %@----%@",user,psd);
  106. }
  107. }
  108. //授权失败
  109. - (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithError:(NSError *)error API_AVAILABLE(ios(13.0)){
  110. NSLog(@"错误信息:%@", error);
  111. NSString *errorMsg;
  112. switch (error.code) {
  113. case ASAuthorizationErrorCanceled:
  114. errorMsg = @"用户取消了授权请求";
  115. NSLog(@"errorMsg - %@",errorMsg);
  116. break;
  117. case ASAuthorizationErrorFailed:
  118. errorMsg = @"授权请求失败";
  119. NSLog(@"errorMsg - %@",errorMsg);
  120. break;
  121. case ASAuthorizationErrorInvalidResponse:
  122. errorMsg = @"授权请求响应无效";
  123. NSLog(@"errorMsg - %@",errorMsg);
  124. break;
  125. case ASAuthorizationErrorNotHandled:
  126. errorMsg = @"未能处理授权请求";
  127. NSLog(@"errorMsg - %@",errorMsg);
  128. break;
  129. case ASAuthorizationErrorUnknown:
  130. errorMsg = @"授权请求失败未知原因";
  131. NSLog(@"errorMsg - %@",errorMsg);
  132. break;
  133. default:
  134. break;
  135. }
  136. }
  137. - (ASPresentationAnchor)presentationAnchorForAuthorizationController:(ASAuthorizationController *)controller API_AVAILABLE(ios(13.0)){
  138. UIViewController *topvc = topViewController();
  139. return topvc.view.window;
  140. }
  141. -(void)tool_facebook{
  142. FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
  143. UIViewController *topvc = topViewController();
  144. [MBProgressHUD showHUDAddedTo:topvc.view animated:YES];
  145. [loginManager logInWithPermissions:@[@"public_profile", @"email"] fromViewController:topvc handler:^(FBSDKLoginManagerLoginResult * _Nullable result, NSError * _Nullable error) {
  146. if(error){
  147. [MBProgressHUD hideHUDForView:topvc.view animated:YES];
  148. NSLog(@"NSError-----%@",error);
  149. return;
  150. }else if(result.isCancelled){
  151. [MBProgressHUD hideHUDForView:topvc.view animated:YES];
  152. NSLog(@"Face---Cancelled");
  153. }else{
  154. NSDictionary*params= @{@"fields":@"id,name,email,age_range,first_name,last_name,link,gender,locale,picture,timezone,updated_time,verified"};
  155. NSString *tmpToken = MM_str(result.token.tokenString);
  156. FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
  157. initWithGraphPath:result.token.userID
  158. parameters:params
  159. HTTPMethod:@"GET"];
  160. [request startWithCompletion:^(id<FBSDKGraphRequestConnecting> _Nullable connection, id _Nullable result, NSError * _Nullable error) {
  161. [MBProgressHUD hideHUDForView:topvc.view animated:YES];
  162. NSLog(@"%@",result[@"name"]);
  163. if (error == nil) {
  164. NSMutableDictionary *paramsDic= [NSMutableDictionary dictionary];
  165. // [paramsDic setObject:@(1) forKey:@"type"];
  166. // [paramsDic setObject:tmpToken forKey:@"token"];
  167. // [paramsDic setObject:MM_str(result[@"email"]) forKey:@"email"];
  168. // [paramsDic setObject:MM_str(result[@"id"]) forKey:@"uid"];
  169. // [paramsDic setObject:MM_str(result[@"first_name"]) forKey:@"firstName"];
  170. // [paramsDic setObject:MM_str(result[@"last_name"]) forKey:@"lastName"];
  171. // [paramsDic setObject:@"" forKey:@"birthday"];
  172. // [paramsDic setObject:@"" forKey:@"gender"];
  173. NSDictionary *userInfo = @{@"identifier":MM_str(result[@"id"]) ,
  174. @"email":MM_str(result[@"email"]),
  175. @"firstName":MM_str(result[@"first_name"]),
  176. @"lastName":MM_str(result[@"last_name"]),
  177. @"displayName":@""};
  178. NSString *userInfoStr = [userInfo mj_JSONString];
  179. [paramsDic setObject:userInfoStr forKey:@"userInfo"];
  180. [paramsDic setObject:@"facebook" forKey:@"hauth_done"];
  181. [paramsDic setObject:tmpToken forKey:@"token"];
  182. NSLog(@"paramsDic----%@",paramsDic);
  183. [self generaltriggermethodType:ThirdTypeFaceBook data:paramsDic];
  184. }else{
  185. [topvc.view makeToast:[NSString stringWithFormat:@"Login failed with error code:%ld",error.code] duration:3 position:CSToastPositionCenter];
  186. }
  187. }];
  188. }
  189. }];
  190. }
  191. -(void)tool_google{
  192. UIViewController *topvc = topViewController();
  193. K_WEAK_SELF;
  194. [MBProgressHUD showHUDAddedTo:topvc.view animated:YES];
  195. [GIDSignIn.sharedInstance signInWithPresentingViewController:topvc completion:^(GIDSignInResult * _Nullable signInResult, NSError * _Nullable error) {
  196. K_STRONG_SELF;
  197. [MBProgressHUD hideHUDForView:topvc.view animated:YES];
  198. if (error) {
  199. NSLog(@"NSError-----%@",error);
  200. return;
  201. }
  202. GIDGoogleUser *user = signInResult.user;
  203. if (user == nil) {
  204. return;
  205. }
  206. // NSString *name = user.profile.name;
  207. NSString *givenName = user.profile.givenName;
  208. NSString *familyName = user.profile.familyName;
  209. NSMutableDictionary *paramsDic= [NSMutableDictionary dictionary];
  210. NSDictionary *userInfo = @{@"identifier":user.userID,
  211. @"email":user.profile.email,
  212. @"firstName":familyName,
  213. @"lastName":givenName,
  214. @"displayName":@""};
  215. NSString *userInfoStr = [userInfo mj_JSONString];
  216. [paramsDic setObject:userInfoStr forKey:@"userInfo"];
  217. [paramsDic setObject:@"google" forKey:@"hauth_done"];
  218. [paramsDic setObject:user.accessToken.tokenString forKey:@"token"];
  219. // [paramsDic setObject:user.profile.email forKey:@"email"];
  220. // [paramsDic setObject:user.userID forKey:@"identifier"];
  221. // [paramsDic setObject:familyName forKey:@"firstName"];
  222. // [paramsDic setObject:givenName forKey:@"lastName"];
  223. // [paramsDic setObject:@"" forKey:@"birthday"];
  224. // [paramsDic setObject:@"" forKey:@"gender"];
  225. // [paramsDic setObject:signInResult.serverAuthCode forKey:@"code"];
  226. // [paramsDic setObject:@"google" forKey:@"state"];
  227. NSLog(@"paramsDic----%@",paramsDic);
  228. [self generaltriggermethodType:ThirdTypeGoogle data:paramsDic];
  229. }];
  230. }
  231. @end