iOS第三方登录大全(下)

Facebook、Twitter、Google+接入

Facebook接入

官方文档

接入文档

App平台和具体平台接入流程

2.1 在配置文件Info.plist中配置应用白名单,必须添加以下所有字段,否则可能无法跳转

1
2
3
4
5
6
7
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fb-messenger-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>

2.2 点击XCode项目名,选择Info,添加FaceBook的URL Types

identifier:fb URL Schemes:fb前缀后+FaceBook App ID(用户自己申请的账号)

2.3 Appdelegate中的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//facebook
[[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
[FBSDKSettings setAppID:Facebook_APP_ID];

return YES;
}
// 2
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
return
[[FBSDKApplicationDelegate sharedInstance] application:application openURL:url sourceApplication:sourceApplication annotation:annotation]
}
// 3
- (void)applicationDidBecomeActive:(UIApplication *)application {

[FBSDKAppEvents activateApp];
}

2.4 工具类中构造loginFacebook方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
- (void)loginFacebookSuccess:(UIViewController *)viewController success:(void (^)(id response))successBlock failure:(void (^)(NSError *error))failureBlock{
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions: @[@"public_profile"]
fromViewController:viewController
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
NSError *error = [CIAccountError createError:ErrorThirdLoginFailure];
failureBlock(error);
} else if (result.isCancelled) {
NSError *error = [CIAccountError createError:ErrorThirdLoginCancel];
failureBlock(error);
} else {
NSString *token = result.token.tokenString;
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:result.token.userID
parameters:nil
HTTPMethod:@"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error) {
if (error) {
NSError *resultError = [CIAccountError createError:ErrorThirdLoginFailure];
failureBlock(resultError);
}else{
NSString *nickName = [result objectForKey:@"name"];
NSString *openId = [result objectForKey:@"id"];
NSDictionary *resultDic = @{@"openid":openId,
@"nickname":nickName,
@"account_type":@"facebook",
@"access_token":token,
@"third_appid":Facebook_APP_ID};
successBlock(resultDic);
}

}];
}
}];
}

2.5 登录时调用loginFacebook方法

1
2
3
4
5
[thirdLoginUtil loginFacebookSuccess:self success:^(id response) {
//获取数据实现客户端登录
} failure:^(NSError *error) {
[self ShowExclaHud:error.localizedDescription];
}];

Twitter接入

官方文档

接入文档

App控制台

2.1 点击XCode项目名,选择Info,添加twitter的URL Types

identifier:twitter URL Schemes:Twitter App ID(用户自己申请的账号)

2.2 Appdelegate中的配置

1
2
3
4
5
6
7

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//twitter
[[Twitter sharedInstance] startWithConsumerKey:Twitter_APP_KEY consumerSecret:Twitter_APP_SECRET];

return YES;
}

2.3 在工具类中构造loginTwitter方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
- (void)loginTwitterSuccess:(void (^)(id response))successBlock failure:(void (^)(NSError *error))failureBlock{
[[Twitter sharedInstance] logInWithCompletion:^(TWTRSession *session, NSError *error) {

if (session) {
NSString *token = session.authToken;
NSString *openId = session.userID;
NSString *nickName = session.userName;

if (token && openId && nickName) {

NSDictionary *resultDic = @{@"openid":openId,
@"nickname":nickName,
@"account_type":@"twitter",
@"access_token":token,
@"third_appid":Twitter_APP_KEY};
successBlock(resultDic);

}
else{
NSError *resultError = [CIAccountError createError:ErrorThirdLoginFailure];
failureBlock(resultError);
}


}else
{NSError *resultError = [CIAccountError createError:ErrorThirdLoginFailure];
failureBlock(resultError);
}
}];
}

2.4登录时调用loginTwitter方法

1
2
3
4
5
[thirdLoginUtil loginTwitterSuccess:^(id response) {
//获取数据实现客户端登录
} failure:^(NSError *error) {
[self ShowExclaHud:error.localizedDescription];
}];

2.5 tips

注册时一定要填回调地址

Google+接入

官方文档

接入文档

2.1 在配置文件Info.plist中配置应用白名单,必须添加以下所有字段,否则可能无法跳转

1
2
3
4
5
6
7
8
9
10
11
<key>LSApplicationQueriesSchemes</key>
<array>
<string>com.google.gppconsent.2.4.1</string>
<string>com.google.gppconsent.2.4.0</string>
<string>com.google.gppconsent.2.3.0</string>
<string>com.google.gppconsent.2.2.0</string>
<string>com.google.gppconsent</string>
<string>hasgplus4</string>
<string>googlechrome-x-callback</string>
<string>googlechrome</string>
</array>

2.2 点击XCode项目名,选择Info,添加Google的URL Types

identifier:google URL Schemes:com.googleusercontent.apps.前缀后+FaceBook App ID(用户自己申请的账号)

2.3 Appdelegate中的配置

1
2
3
4
5
6
7
8
9
10
11
12

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//google
[GIDSignIn sharedInstance].clientID = Google_APP_ID;

return YES;
}
// 2
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
return
[[GIDSignIn sharedInstance] handleURL:url sourceApplication:sourceApplication annotation:annotation];
}

2.4 在登录中输入代码

1
[[GIDSignIn sharedInstance] signIn];

2.5 实现google+代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#pragma mark - GIDSignInDelegate
- (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error
{
if (error) {
[self ShowExclaHud:@"授权失败"];
}else
{
NSString *token = user.authentication.idToken;
NSString *openId = user.userID;
NSString *nickname = user.profile.name;
NSDictionary *resultDic = @{@"openid":openId,
@"nickname":nickname,
@"account_type":@"google",
@"access_token":token,

@"third_appid":Google_APP_ID};
//获得返回数据resultDic实现客户端登录


}
}

- (void)signIn:(GIDSignIn *)signIn didDisconnectWithUser:(GIDGoogleUser *)user withError:(NSError *)error{
if (error) {
[self ShowExclaHud:@"授权失败"];
}else{
NSString *token = user.authentication.idToken;
NSString *openId = user.userID;
NSString *nikeName = user.profile.name;
NSDictionary *resultDic = @{@"openid":openId,
@"nikename":nikeName,
@"account_type":@"google",
@"access_token":token,
@"third_appid":Google_APP_ID};
//获得返回数据resultDic实现客户端登录


}
}


- (void)presentSignInViewController:(UIViewController *)viewController {
[self presentViewController:viewController animated:YES completion:nil];
}