iOS海外SDK-Facebook,Google登录

前言

海外SDK存在Google登录时,被拒理由如下:


iOS海外SDK-Facebook,Google登录_第1张图片
google+.png

Google下的protocolbuffer是可以使用的。审核时会检测是否存在Google的登录SDK(GoogleSignIn.bundle,GoogleSignIn.framework,GoogleSignInDependencies.framework)

Facebook登录

facebook后台控制面板

  1. 后台创建应用,获取FacebookAppID;
  2. 工程配置,将 Bolts、FBSDKCoreKit 和 FBSDKLoginKit 框架文件拖放至项目,配置plist文件:URL types,FacebookAppID,FacebookDisplayName,LSApplicationQueriesSchemes
  3. 项目集成
  4. 在Facebook后台添加测试账号进行测试,个人的账号是不能进行测试的,提示 不允许用户查看应用程序。:The user is not allowd to see this application per the developer set configuration.

info.plist配置

FacebookAppID
    40438916678969
FacebookDisplayName
    ios-2018-6-11-test
LSApplicationQueriesSchemes
    
        fbapi
        fb-messenger-share-api
        fbauth2
        fbshareextension
    
    
CFBundleURLTypes
    
        
            CFBundleURLSchemes
            
                fb404389166741069
            
        
    

#pragma mark:facebook login
+ (void)loginWithFacebook
{
    NSDLog(@"Facebook SDK Version:%@",[FBSDKSettings sdkVersion]);
    
    FBSDKAccessToken *token = [FBSDKAccessToken currentAccessToken];
    
    if (token) { //自动登录
        
        [self gotFacebookInfoAndLoginWithFbUserId:token.userID fbToken:token.tokenString];
        
        return;
    }
    
    [BJ7_MBProgressHUD3_C9S G9X_hideHUD_A8P];
    
    [self facebookLoginNormal];
}

//正常授权登录
+ (void)facebookLoginNormal
{
    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    
//    login.authType = @"reauthenticate"; //https,reauthenticate,rerequest,reauthorize
//    login.loginBehavior = FBSDKLoginBehaviorSystemAccount;//优先客户端登录,好像没啥用
    [login logInWithReadPermissions:@[@"public_profile"]
                 fromViewController:[kWindowManager getTopViewController]
                            handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
                                if (error) {
                                    NSDLog(@"fb error:%@",error.localizedDescription);
                                    [BJ7_MBProgressHUD3_C9S G9X_showError_A8P:error.localizedDescription];
                                    kGlobelVariable.isLogining = NO;
                                } else if (result.isCancelled) {
                                    NSDLog(@"Cancelled");
                                    kGlobelVariable.isLogining = NO;
                                    [BJ7_MBProgressHUD3_C9S G9X_showError_A8P:@"Cancelled"];
                                   
                                } else {
                                    NSDLog(@"Logged in");
                                    
                                    NSString *fbToken = result.token.tokenString;
                                    NSString *fbUserId = result.token.userID;
                                    
                                    [self gotFacebookInfoAndLoginWithFbUserId:fbUserId fbToken:fbToken];
                                    
                                }
                            }];
}

+ (void)gotFacebookInfoAndLoginWithFbUserId:(NSString *)fbUserId fbToken:(NSString *)fbToken
{
//    [BJ7_MBProgressHUD3_C9S G9X_showMessage_A8P:@"isLogin"];
    
    //获取Facebook 用户信息
    NSDictionary*params= @{@"fields":@"id,name,email"};
//    @{@"fields":@"id,name,email,age_range,first_name,last_name,link,gender,locale,picture,timezone,updated_time,verified"};
    
    FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:params HTTPMethod:@"GET"];
    [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
        
        //token过期,删除存储的token和profile
        if (error) {
            NSDLog(@"The user token is no longer valid.");
            [BJ7_MBProgressHUD3_C9S G9X_hideHUD_A8P];
            //--//G9X_InvokeMethod_A8P
            [FBSDKAccessToken setCurrentAccessToken:nil];
            [FBSDKProfile setCurrentProfile:nil];
            
            [self facebookLoginNormal];
        }else { //做登录完成的操作
            
            [BJ7_MBProgressHUD3_C9S G9X_showMessage_A8P:kLocalizedString(@"isLogin")];
            
            NSString *email = [result objectForKey:@"email"];
            NSString*name=[result objectForKey:@"name"];
            NSString*userid=[result objectForKey:@"id"];
            NSDLog(@"result:%@ \n name=%@ userID=%@ ",result,name,userid);//
            
            //Facebook,Google 优先显示email,不存在则显示 name(userId) ,最后选择 userId
            NSString *facebookName = email;
            if ([NSString isEmpty:email]) {
                if ([NSString isEmpty:name]) {
                    facebookName = userid;
                }else{
                    facebookName = [NSString stringWithFormat:@"%@(%@)",name,userid];
                }
            }
            
//去自己服务端登录
            [BJ7_RequestManager_C9S G9X_LoginAuthorize_A8P:RequestLoginTypeFacebook name:nil password:nil token:nil fbId:fbUserId fbToken:fbToken googleToken:nil complete:^(BOOL isSuccess, id responseObject) {
                
                [BJ7_MBProgressHUD3_C9S G9X_hideHUD_A8P];
                
                if (isSuccess) {
                    NSDLog(@"facebook登录成功");
                
                    kGlobelVariable.showAccount = facebookName;
                    
                    [kWindowManager dismissLoginWindow];
                }else{
                    [kWindowManager presentLoginWindow:YES];
//                    [self logoutWithMsg:nil];
                    //--//G9X_InvokeMethod_A8P
                }
            }];
            
        }
    }];
}

Google登录

Google登录
凭据-客户端ID

  1. 后台创建应用,获取客户端ID;
  2. 工程配置,将GoogleSignIn.bundle,GoogleSignIn.framework,GoogleSignInDependencies.framework框架文件拖放至项目,配置plist白名单LSApplicationQueriesSchemes
  3. 项目集成
  4. Google可使用自己的账号进行测试。

info.plist配置

CFBundleURLTypes
    
        
            CFBundleTypeRole
            Editor
            CFBundleURLSchemes
            
                com.googleusercontent.apps.4584-06u5btjgdtv82r3dlacqqkrn375
            
        
    
    
反写凭据:4584-06u5btjgdtv82r3dlacqqkrn375.apps.googleusercontent.com -> com.googleusercontent.apps.4584-06u5btjgdtv82r3dlacqqkrn375

info.plist配置 GoogleClientID : GoogleClientID  SDK内读取GoogleClientID值

#pragma mark: google login
+ (void)loginWithGoogle
{
    //--//G9X_InvokeMethod_A8P
    [BJ7_MBProgressHUD3_C9S G9X_hideHUD_A8P];

    [GIDSignIn sharedInstance].delegate = [self G9X_LoginManagerShare_C9S];
    [GIDSignIn sharedInstance].uiDelegate = [self G9X_LoginManagerShare_C9S];
    [[GIDSignIn sharedInstance] signIn];
    
}

#pragma mark:GIDSignInUIDelegate
// Present a view that prompts the user to sign in with Google
//如果实现,当需要显示视图控制器时,将调用此方法。
- (void)signIn:(GIDSignIn *)signIn
presentViewController:(UIViewController *)viewController {
    //--//G9X_InvokeMethod_A8P
    [[kWindowManager getTopViewController] presentViewController:viewController animated:YES completion:nil];
}

// Dismiss the "Sign in with Google" view
//如果实现,则在需要关闭视图控制器时将调用此方法。
- (void)signIn:(GIDSignIn *)signIn
dismissViewController:(UIViewController *)viewController {
    //--//G9X_InvokeMethod_A8P
    [[kWindowManager getTopViewController] dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark:GIDSignInDelegate
- (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error
{
    // Perform any operations on signed in user here.
    if (error == nil) {
        [BJ7_MBProgressHUD3_C9S G9X_showMessage_A8P:kLocalizedString(@"isLogin")];
        
        NSString *userId = user.userID; //101241414941343356764   // For client-side use only!
        NSString *idToken = user.authentication.idToken; // Safe to send to the server
        NSString *fullName = user.profile.name; //曾春军
        NSString *email = user.profile.email; // [email protected]
        
//        Facebook,Google 优先显示email,不存在则显示 name(userId) ,最后选择 userId
        NSString *googleName = email;
        if ([NSString isEmpty:email]) {
            if ([NSString isEmpty:fullName]) {
                googleName = userId;
            }else{
                googleName = [NSString stringWithFormat:@"%@(%@)",fullName,userId];
            }
        }
        
        [BJ7_LoginManager_C9S googleLoginWithIdToken:idToken showName:googleName];
        
    }else{
        //--//G9X_InvokeMethod_A8P
        NSDLog(@"google登录失败:%@",error);
        kGlobelVariable.isLogining = NO;
        [BJ7_MBProgressHUD3_C9S G9X_showError_A8P:error.localizedDescription];
        
    }
    
}

- (void)signIn:(GIDSignIn *)signIn
didDisconnectWithUser:(GIDGoogleUser *)user
     withError:(NSError *)error {
    if (error == nil) {
        [BJ7_MBProgressHUD3_C9S G9X_showMessage_A8P:kLocalizedString(@"isLogin")];
        
        NSString *userId = user.userID; //101241414941343356764   // For client-side use only!
        NSString *idToken = user.authentication.idToken; // Safe to send to the server
        NSString *email = user.profile.email;
        NSString *fullName = user.profile.name;
//        Facebook,Google 优先显示email,不存在则显示 name(userId) ,最后选择 userId
        NSString *googleName = email;
        if ([NSString isEmpty:email]) {
            if ([NSString isEmpty:fullName]) {
                googleName = userId;
            }else{
                googleName = [NSString stringWithFormat:@"%@(%@)",fullName,userId];
            }
        }
        [BJ7_LoginManager_C9S googleLoginWithIdToken:idToken showName:googleName];
        
    }else{
        //--//G9X_InvokeMethod_A8P
        NSDLog(@"google登录失败:%@",error);
        kGlobelVariable.isLogining = NO;
        [BJ7_MBProgressHUD3_C9S G9X_showError_A8P:error.localizedDescription];

    }
}

//去自己服务端登录
+ (void)googleLoginWithIdToken:(NSString *)token showName:(NSString *)showName
{
    [BJ7_RequestManager_C9S G9X_LoginAuthorize_A8P:RequestLoginTypeGoogle name:nil password:nil token:nil fbId:nil fbToken:nil googleToken:token complete:^(BOOL isSuccess, id responseObject) {
        
        [BJ7_MBProgressHUD3_C9S G9X_hideHUD_A8P];
        
        if (isSuccess) {
            NSDLog(@"Google登录成功");
            kGlobelVariable.showAccount = showName;

            [kWindowManager dismissLoginWindow];
        }else{
            [kWindowManager presentLoginWindow:YES];
//            [self logoutWithMsg:nil];
            //--//G9X_InvokeMethod_A8P
        }
        
        
    }];
}

其他注意项:

  1. facebook登录时打开的web页面为 SFSafariViewController。倘若在该页面中找不到用Safari打开的按钮也就是没有那个指南针的按钮。这时需要清除缓存,请前往设置里面的Safari设置中心进行清除数据。

你可能感兴趣的:(iOS海外SDK-Facebook,Google登录)