iOS海外SDK-Facebook,Google登录

前言

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


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配置

<key>FacebookAppID</key>
    <string>40438916678969</string>
<key>FacebookDisplayName</key>
    <string>ios-2018-6-11-test</string>
<key>LSApplicationQueriesSchemes</key>
    <array>
        <string>fbapi</string>
        <string>fb-messenger-share-api</string>
        <string>fbauth2</string>
        <string>fbshareextension</string>
    </array>
    
<key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>fb404389166741069</string>
            </array>
        </dict>
    </array>

#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配置

<key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeRole</key>
            <string>Editor</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>com.googleusercontent.apps.4584-06u5btjgdtv82r3dlacqqkrn375</string>
            </array>
        </dict>
    </array>
    
反写凭据: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; // 2361496651@qq.com
        
//        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设置中心进行清除数据。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • __ __ |__| _____ __ __ ┌...
    wangchuang2017阅读 11,908评论 2 1
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,490评论 25 709
  • 今夜,情难了 我们轻啄酒杯浅浅笑 忘了背负的沉重稀疏的眉角 满一杯 孩子般 洒脱地把头高高昂 泻一地鸡毛蒜皮 倾一...
    石小榴阅读 1,846评论 0 0
  • 今天我要给大家介绍一下一本叫:《七只乌鸦》的一个小故事.这个故事的大体内容是:从前有一个人有七个儿子,他非...
    bae63bb56f63阅读 3,480评论 0 2
  • just do it 其实挺想问自己的,勇敢地去追梦想到底有多难?真的是长大了考虑的多了,所以很多事情多不想去了么...
    王小piapia阅读 1,769评论 0 0

友情链接更多精彩内容