123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- //
- // LoginThirdAuthV.m
- // Asteria
- //
- // Created by 王猛 on 2023/5/16.
- //
- #import "LoginThirdAuthV.h"
- #import <AuthenticationServices/AuthenticationServices.h>
- #import <GoogleSignIn/GoogleSignIn.h>
- #import <FBSDKLoginKit/FBSDKLoginKit.h>
- #define LoginBtnTag 10000
- @interface LoginThirdAuthV ()<ASAuthorizationControllerDelegate,ASAuthorizationControllerPresentationContextProviding>
- @end
- @implementation LoginThirdAuthV
- - (void)tt_setupViews{
- UILabel *tipLab = [[UILabel alloc]init];
- tipLab.text = @"Continue With";
- tipLab.font = [UIFont fontWithName:Rob_Regular size:12];
- [self addSubview:tipLab];
- tipLab.frame = CGRectMake(20, 0, KScreenWidth-40, 15);
-
-
- NSArray *titleAry = @[@"Apple",@"Facebook",@"Google"];
- NSArray *imgAry = @[@"login_Apple",@"login_Facebook",@"login_Google"];
- for (int i= 0; i<titleAry.count; i++) {
-
- QMUIButton *btn = [self laod_authItem:titleAry[i] img:imgAry[i]];
- btn.tag = LoginBtnTag + i;
- [btn addTarget:self action:@selector(handle_BtnEvent:) forControlEvents:UIControlEventTouchUpInside];
- btn.mj_y = CGRectGetMaxY(tipLab.frame)+20 + (36+10)*i;
- [self addSubview:btn];
- }
- QMUIButton *tepBtn = [self viewWithTag:LoginBtnTag];
- if(@available(iOS 13.0, *)){
- tepBtn.hidden = NO;
- }else{
- tepBtn.hidden = YES;
- }
- }
- -(CGFloat)xxx_Vheight{
- return 15+20+(36+10)*3;
- }
- -(QMUIButton *)laod_authItem:(NSString *)title img:(NSString *)imgStr{
- QMUIButton *authBtn = [[QMUIButton alloc] initWithFrame:CGRectMake(20, 0, 132, 36)];
- authBtn.backgroundColor = ThemeLightColor;
- authBtn.imageView.qmui_size = CGSizeMake(24, 24);
- [authBtn setImage:[UIImage imageNamed:imgStr] forState:UIControlStateNormal];
- [authBtn setTitle:title forState:UIControlStateNormal];
- [authBtn setTitleColor:[UIColor colorWithHexString:@"#000000"] forState:UIControlStateNormal];
- authBtn.titleLabel.font = [UIFont fontWithName:Rob_Regular size:12];
- authBtn.cornerRadius = 4;
- authBtn.spacingBetweenImageAndTitle = 20.0f;
-
- CGSize imageSize = authBtn.imageView.frame.size;
- CGSize titleSize = authBtn.titleLabel.frame.size;
-
- authBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 132-imageSize.width-titleSize.width-20-10);
- authBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
- return authBtn;
- }
- -(void)handle_BtnEvent:(UIButton *)btn{
- switch (btn.tag) {
- case LoginBtnTag:
- [self tool_apple];
- break;
- case LoginBtnTag+1:
- [self tool_facebook];
- break;
- case LoginBtnTag+2:
- [self tool_google];
- break;
- default:
- break;
- }
-
- }
- -(void)tool_apple{
- if(@available(iOS 13.0, *)){
- ASAuthorizationAppleIDProvider *appleProvider = [[ASAuthorizationAppleIDProvider alloc]init];
-
- ASAuthorizationAppleIDRequest *request = appleProvider.createRequest;
- request.requestedScopes = @[ASAuthorizationScopeEmail,ASAuthorizationScopeFullName];
-
- ASAuthorizationController *controller = [[ASAuthorizationController alloc]initWithAuthorizationRequests:@[request]];
- controller.delegate = self;
- controller.presentationContextProvider = self;
- [controller performRequests];
- }
-
- }
- #pragma mark - **************** ASAuthorizationControllerDelegate ****************
- ///授权成功回调
- - (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(nonnull ASAuthorization *)authorization API_AVAILABLE(ios(13.0)){
- if([authorization.credential isKindOfClass:[ASAuthorizationAppleIDCredential class]]){
- ASAuthorizationAppleIDCredential *credential = (ASAuthorizationAppleIDCredential *)authorization.credential;
- NSString *user = credential.user;
- NSString *email = credential.email;
- NSData *identityToken = credential.identityToken;
- NSLog(@"user - %@ --- %@--%@",user,email,identityToken);
- }else if([authorization.credential isKindOfClass:[ASPasswordCredential class]]){
- ASPasswordCredential *psdCredential = (ASPasswordCredential *)authorization.credential;
- // 密码凭证对象的用户标识 用户的唯一标识
- NSString *user = psdCredential.user;
- NSString *psd = psdCredential.password;
- NSLog(@"psduer -- %@----%@",user,psd);
- }
- }
- //授权失败
- - (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithError:(NSError *)error API_AVAILABLE(ios(13.0)){
- NSLog(@"错误信息:%@", error);
- NSString *errorMsg;
- switch (error.code) {
- case ASAuthorizationErrorCanceled:
- errorMsg = @"用户取消了授权请求";
- NSLog(@"errorMsg - %@",errorMsg);
- break;
-
- case ASAuthorizationErrorFailed:
- errorMsg = @"授权请求失败";
- NSLog(@"errorMsg - %@",errorMsg);
- break;
-
- case ASAuthorizationErrorInvalidResponse:
- errorMsg = @"授权请求响应无效";
- NSLog(@"errorMsg - %@",errorMsg);
- break;
-
- case ASAuthorizationErrorNotHandled:
- errorMsg = @"未能处理授权请求";
- NSLog(@"errorMsg - %@",errorMsg);
- break;
-
- case ASAuthorizationErrorUnknown:
- errorMsg = @"授权请求失败未知原因";
- NSLog(@"errorMsg - %@",errorMsg);
- break;
-
- default:
- break;
- }
- }
- - (ASPresentationAnchor)presentationAnchorForAuthorizationController:(ASAuthorizationController *)controller API_AVAILABLE(ios(13.0)){
- UIViewController *topvc = topViewController();
- return topvc.view.window;
- }
- -(void)tool_facebook{
- FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
- UIViewController *topvc = topViewController();
- [MBProgressHUD showHUDAddedTo:topvc.view animated:YES];
- [loginManager logInWithPermissions:@[@"public_profile", @"email"] fromViewController:topvc handler:^(FBSDKLoginManagerLoginResult * _Nullable result, NSError * _Nullable error) {
-
- if(error){
- [MBProgressHUD hideHUDForView:topvc.view animated:YES];
- NSLog(@"NSError-----%@",error);
- return;
- }else if(result.isCancelled){
- [MBProgressHUD hideHUDForView:topvc.view animated:YES];
- NSLog(@"Face---Cancelled");
- }else{
- NSDictionary*params= @{@"fields":@"id,name,email,age_range,first_name,last_name,link,gender,locale,picture,timezone,updated_time,verified"};
- NSString *tmpToken = MM_str(result.token.tokenString);
- FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
- initWithGraphPath:result.token.userID
- parameters:params
- HTTPMethod:@"GET"];
- [request startWithCompletion:^(id<FBSDKGraphRequestConnecting> _Nullable connection, id _Nullable result, NSError * _Nullable error) {
- [MBProgressHUD hideHUDForView:topvc.view animated:YES];
- NSLog(@"%@",result[@"name"]);
- if (error == nil) {
- NSMutableDictionary *paramsDic= [NSMutableDictionary dictionary];
- [paramsDic setObject:@(1) forKey:@"type"];
- [paramsDic setObject:tmpToken forKey:@"token"];
- [paramsDic setObject:MM_str(result[@"email"]) forKey:@"email"];
- [paramsDic setObject:MM_str(result[@"id"]) forKey:@"uid"];
- [paramsDic setObject:MM_str(result[@"first_name"]) forKey:@"firstName"];
- [paramsDic setObject:MM_str(result[@"last_name"]) forKey:@"lastName"];
- [paramsDic setObject:@"" forKey:@"birthday"];
- [paramsDic setObject:@"" forKey:@"gender"];
- NSLog(@"paramsDic----%@",paramsDic);
- [self generaltriggermethodType:ThirdTypeFaceBook data:paramsDic];
-
- }else{
-
- [topvc.view makeToast:[NSString stringWithFormat:@"Login failed with error code:%ld",error.code] duration:3 position:CSToastPositionCenter];
- }
- }];
- }
- }];
-
- }
- -(void)tool_google{
- UIViewController *topvc = topViewController();
- [MBProgressHUD showHUDAddedTo:topvc.view animated:YES];
- [GIDSignIn.sharedInstance signInWithPresentingViewController:topvc completion:^(GIDSignInResult * _Nullable signInResult, NSError * _Nullable error) {
- if (error) {
- NSLog(@"NSError-----%@",error);
- return;
- }
- GIDGoogleUser *user = signInResult.user;
- if (user == nil) { return; }
- NSString *name = user.profile.name;
- NSString *givenName = user.profile.givenName;
- NSString *familyName = user.profile.familyName;
- NSMutableDictionary *paramsDic= [NSMutableDictionary dictionary];
- [paramsDic setObject:@(2) forKey:@"type"];
- [paramsDic setObject:user.accessToken forKey:@"token"];
- [paramsDic setObject:user.profile.email forKey:@"email"];
- [paramsDic setObject:user.userID forKey:@"uid"];
- [paramsDic setObject:familyName forKey:@"firstName"];
- [paramsDic setObject:givenName forKey:@"lastName"];
- [paramsDic setObject:@"" forKey:@"birthday"];
- [paramsDic setObject:@"" forKey:@"gender"];
- NSLog(@"paramsDic----%@",paramsDic);
- [self generaltriggermethodType:ThirdTypeGoogle data:paramsDic];
- }];
- }
- @end
|