⼀ 、google登录接⼊
cocoapods集成 pod 'GoogleSignIn'
1. 配置:
info.plist⾥加⼊ GIDClientID 字段,客户端id运营提供
2.配置urlTypes,添加 schemes 值为
如客户端id为:617823678-dashkdhaslkd.apps.googleusercontent.com
则schemes 填写为 :com.googleusercontent.apps.617823678-dashkdhaslkd
identifer同上
代码集成:
#import <GoogleSignIn/GoogleSignIn.h>
//注册调⽤⽅法
[[GIDSignIn sharedInstance]
restorePreviousSignInWithCompletion:^(GIDGoogleUser *
_Nullable user, NSError * _Nullable error) {
if (error) {
// Show the app's signed-out state.
} else {
// Show the app's signed-in state.
}
}];
//登录调⽤,成功将token传给后台
[GIDSignIn.sharedInstance
signInWithPresentingViewController:vc
completion:^(GIDSignInResult *signInResult, NSError *error) {
if (error) {
NSLog(@"%@",[NSString stringWithFormat:@"Status:
Authentication error: %@", error]);
return;
}
GIDGoogleUser *googleUser = [GIDSignIn.sharedInstance
currentUser];
if (googleUser) {
NSLog(@"%@",googleUser.idToken.tokenString);
} else {
NSLog(@"Status: Not authenticated");
}
}];- (BOOL)application:(UIApplication *)application openURL:
(NSURL *)url options:(nonnull
NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {
return [GIDSignIn.sharedInstance handleURL:url];
}
2.facebook登录接⼊
cocoapods集成
pod 'FBSDKCoreKit'
pod 'FBSDKLoginKit'
配置:
1.info.plist⾥加⼊ 字段,运营提供
FacebookAppID
FacebookClientToken
FacebookDisplayName
LSApplicationQueriesSchemes 中添加fbapi、fb-messenger�
share-api
2. 配置urlTypes添加 schemes 值为 fb+appid
Identifer: fb
如客户端id为:12345678
则schemes 填写为 :fb12345678
3. 代码集成
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>
//注册调⽤
[[FBSDKApplicationDelegate sharedInstance]
application:application
didFinishLaunchingWithOptions:launchOptions];
//登录调⽤,将userId和token传给后台
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logOut];//登录之前调⽤⼀次登出,防⽌换其他账号⽆法登录情况发⽣。
NSArray *permission = [[NSArray alloc]
initWithObjects:@"public_profile",@"email", nil];
[login logInWithPermissions:permission fromViewController:self
handler:^(FBSDKLoginManagerLoginResult *result, NSError
*error) {
if (error) {
NSLog(@"------------------------------Logged in
error");
} else if (result.isCancelled) {
NSLog(@"------------------------------Cancelled");
} else {
NSString *token = result.token.tokenString;
NSString *userId = result.token.userID;
NSLog(@"------------------------------Logged in
success");
}
}];
- (BOOL)application:(UIApplication *)application openURL:
(NSURL *)url options:(nonnull
NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {
return [[FBSDKApplicationDelegate sharedInstance]
application:application
openURL:url
options:options];
}