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

google+.png
Google下的protocolbuffer是可以使用的。審核時(shí)會(huì)檢測(cè)是否存在Google的登錄SDK(GoogleSignIn.bundle,GoogleSignIn.framework,GoogleSignInDependencies.framework)
Facebook登錄
- 后臺(tái)創(chuàng)建應(yīng)用,獲取FacebookAppID;
- 工程配置,將 Bolts、FBSDKCoreKit 和 FBSDKLoginKit 框架文件拖放至項(xiàng)目,配置plist文件:URL types,FacebookAppID,FacebookDisplayName,LSApplicationQueriesSchemes
- 項(xiàng)目集成
- 在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登錄
- 后臺(tái)創(chuàng)建應(yīng)用,獲取客戶端ID;
- 工程配置,將GoogleSignIn.bundle,GoogleSignIn.framework,GoogleSignInDependencies.framework框架文件拖放至項(xiàng)目,配置plist白名單LSApplicationQueriesSchemes
- 項(xiàng)目集成
- 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):
- facebook登錄時(shí)打開(kāi)的web頁(yè)面為 SFSafariViewController。倘若在該頁(yè)面中找不到用Safari打開(kāi)的按鈕也就是沒(méi)有那個(gè)指南針的按鈕。這時(shí)需要清除緩存,請(qǐng)前往設(shè)置里面的Safari設(shè)置中心進(jìn)行清除數(shù)據(jù)。