iOS海外SDK-Facebook,Google登錄

前言

海外SDK存在Google登錄時(shí),被拒理由如下:


google+.png

Google下的protocolbuffer是可以使用的。審核時(shí)會(huì)檢測(cè)是否存在Google的登錄SDK(GoogleSignIn.bundle,GoogleSignIn.framework,GoogleSignInDependencies.framework)

Facebook登錄

facebook后臺(tái)控制面板

  1. 后臺(tái)創(chuàng)建應(yīng)用,獲取FacebookAppID;
  2. 工程配置,將 Bolts、FBSDKCoreKit 和 FBSDKLoginKit 框架文件拖放至項(xiàng)目,配置plist文件:URL types,FacebookAppID,FacebookDisplayName,LSApplicationQueriesSchemes
  3. 項(xiàng)目集成
  4. 在Facebook后臺(tái)添加測(cè)試賬號(hào)進(jìn)行測(cè)試,個(gè)人的賬號(hào)是不能進(jìn)行測(cè)試的,提示 不允許用戶查看應(yīng)用程序。: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) { //自動(dòng)登錄
        
        [self gotFacebookInfoAndLoginWithFbUserId:token.userID fbToken:token.tokenString];
        
        return;
    }
    
    [BJ7_MBProgressHUD3_C9S G9X_hideHUD_A8P];
    
    [self facebookLoginNormal];
}

//正常授權(quán)登錄
+ (void)facebookLoginNormal
{
    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    
//    login.authType = @"reauthenticate"; //https,reauthenticate,rerequest,reauthorize
//    login.loginBehavior = FBSDKLoginBehaviorSystemAccount;//優(yōu)先客戶端登錄,好像沒(méi)啥用
    [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過(guò)期,刪除存儲(chǔ)的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 優(yōu)先顯示email,不存在則顯示 name(userId) ,最后選擇 userId
            NSString *facebookName = email;
            if ([NSString isEmpty:email]) {
                if ([NSString isEmpty:name]) {
                    facebookName = userid;
                }else{
                    facebookName = [NSString stringWithFormat:@"%@(%@)",name,userid];
                }
            }
            
//去自己服務(wù)端登錄
            [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登錄
憑據(jù)-客戶端ID

  1. 后臺(tái)創(chuàng)建應(yīng)用,獲取客戶端ID;
  2. 工程配置,將GoogleSignIn.bundle,GoogleSignIn.framework,GoogleSignInDependencies.framework框架文件拖放至項(xiàng)目,配置plist白名單LSApplicationQueriesSchemes
  3. 項(xiàng)目集成
  4. Google可使用自己的賬號(hào)進(jìn)行測(cè)試。

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>
    
反寫(xiě)憑據(jù):4584-06u5btjgdtv82r3dlacqqkrn375.apps.googleusercontent.com -> com.googleusercontent.apps.4584-06u5btjgdtv82r3dlacqqkrn375

info.plist配置 GoogleClientID : GoogleClientID  SDK內(nèi)讀取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
//如果實(shí)現(xiàn),當(dāng)需要顯示視圖控制器時(shí),將調(diào)用此方法。
- (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
//如果實(shí)現(xiàn),則在需要關(guān)閉視圖控制器時(shí)將調(diào)用此方法。
- (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 優(yōu)先顯示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 優(yōu)先顯示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];

    }
}

//去自己服務(wù)端登錄
+ (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
        }
        
        
    }];
}

其他注意項(xiàng):

  1. facebook登錄時(shí)打開(kāi)的web頁(yè)面為 SFSafariViewController。倘若在該頁(yè)面中找不到用Safari打開(kāi)的按鈕也就是沒(méi)有那個(gè)指南針的按鈕。這時(shí)需要清除緩存,請(qǐng)前往設(shè)置里面的Safari設(shè)置中心進(jìn)行清除數(shù)據(jù)。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • __ __ |__| _____ __ __ ┌...
    wangchuang2017閱讀 7,240評(píng)論 2 1
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,058評(píng)論 25 709
  • 今夜,情難了 我們輕啄酒杯淺淺笑 忘了背負(fù)的沉重稀疏的眉角 滿一杯 孩子般 灑脫地把頭高高昂 瀉一地雞毛蒜皮 傾一...
    石小榴閱讀 296評(píng)論 0 0
  • 今天我要給大家介紹一下一本叫:《七只烏鴉》的一個(gè)小故事.這個(gè)故事的大體內(nèi)容是:從前有一個(gè)人有七個(gè)兒子,他非...
    bae63bb56f63閱讀 588評(píng)論 0 2
  • just do it 其實(shí)挺想問(wèn)自己的,勇敢地去追夢(mèng)想到底有多難?真的是長(zhǎng)大了考慮的多了,所以很多事情多不想去了么...
    王小piapia閱讀 278評(píng)論 0 0

友情鏈接更多精彩內(nèi)容