Facebook、Twitter、Google+接入
Facebook接入
官方文档
2.1 在配置文件Info.plist中配置应用白名单,必须添加以下所有字段,否则可能无法跳转
<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中的配置
- (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方法
- (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方法
[thirdLoginUtil loginFacebookSuccess:self success:^(id response) {
//获取数据实现客户端登录
} failure:^(NSError *error) {
[self ShowExclaHud:error.localizedDescription];
}];
Twitter接入
官方文档
2.1 点击XCode项目名,选择Info,添加twitter的URL Types
identifier:twitter URL Schemes:Twitter App ID(用户自己申请的账号)
2.2 Appdelegate中的配置
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//twitter
[[Twitter sharedInstance] startWithConsumerKey:Twitter_APP_KEY consumerSecret:Twitter_APP_SECRET];
return YES;
}
2.3 在工具类中构造loginTwitter方法
- (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方法
[thirdLoginUtil loginTwitterSuccess:^(id response) {
//获取数据实现客户端登录
} failure:^(NSError *error) {
[self ShowExclaHud:error.localizedDescription];
}];
2.5 tips
注册时一定要填回调地址
Google+接入
官方文档
2.1 在配置文件Info.plist中配置应用白名单,必须添加以下所有字段,否则可能无法跳转
<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中的配置
- (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 在登录中输入代码
[[GIDSignIn sharedInstance] signIn];
2.5 实现google+代理
#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];
}
网友评论