我试图通过Firebase使用Facebook和Google登录进行用户管理。
P.S。我关注Firebase文档
现在一切都很好但我有问题......
1。我不明白为什么每次我按下facebbok登录按钮或谷歌登录按钮我都会看到这个警告......
是否可以仅显示一次警报?它应该永远显示什么?
2。我们来到第二个问题。每当我的应用程序到达用户选择登录的页面时,我会在照片顶部显示相同的警告...在这种情况下,用户尚未选择任何登录方法Google的警报不应该立即出现。
你能帮我解决这个问题吗?
这是来自App Delegate的代码,其中有Google按钮和Facebook设置
#import "AppDelegate.h"
@import Firebase;
@import GoogleSignIn;
@import FBSDKCoreKit;
@interface AppDelegate () <GIDSignInDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[FIRApp configure];
// GOOGLE
[GIDSignIn sharedInstance].clientID = [FIRApp defaultApp].options.clientID;
[GIDSignIn sharedInstance].delegate = self;
// FACEBOOK
[[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
return YES;
}
- (BOOL)application:(nonnull UIApplication *)application openURL:(nonnull NSURL *)url options:(nonnull NSDictionary<NSString *, id> *)options {
// FACEBOOK
BOOL handled = [[FBSDKApplicationDelegate sharedInstance] application:application openURL:url sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey] annotation:options[UIApplicationOpenURLOptionsAnnotationKey]];
//GOOGLE
[[GIDSignIn sharedInstance] handleURL:url sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey] annotation:options[UIApplicationOpenURLOptionsAnnotationKey]];
return handled;
}
// GOOGLE GIDSignInDelegate
- (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error {
if (error == nil) {
GIDAuthentication *authentication = user.authentication;
FIRAuthCredential *credential = [FIRGoogleAuthProvider credentialWithIDToken:authentication.idToken accessToken:authentication.accessToken];
[[FIRAuth auth] signInWithCredential:credential completion:^(FIRUser * _Nullable user, NSError * _Nullable error) {
if (error) {
NSLog(@"Nessun account registrato su FireBase da Google errore: %@", error);
NSLog(@"%@", credential);
return ;
}
NSLog(@"Registrazione Completata");
}];
} else NSLog(@"LOGIN GOOGLE ERRORE %@", error);
}
这是我的带有facebook按钮的viewController
#import "KPValidation.h"
@import Firebase;
@import GoogleSignIn;
@import FBSDKLoginKit;
@import FBSDKCoreKit;
@interface KPValidation () <GIDSignInUIDelegate, FBSDKLoginButtonDelegate>
@end
@implementation KPValidation
- (void)viewDidLoad {
[super viewDidLoad];
[self setupGoogleButton];
[self setupFacebookButton];
}
-(void)setupFacebookButton {
FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];
loginButton.delegate = self;
loginButton.translatesAutoresizingMaskIntoConstraints = NO;
loginButton.readPermissions = @[@"email", @"public_profile"];
[self.view addSubview:loginButton];
[loginButton.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor constant:-100].active = YES;
[loginButton.leftAnchor constraintEqualToAnchor:self.view.leftAnchor].active = YES;
[loginButton.rightAnchor constraintEqualToAnchor:self.view.rightAnchor].active = YES;
[loginButton.heightAnchor constraintEqualToConstant:50].active = YES;
}
- (void)loginButton:(FBSDKLoginButton *)loginButton didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result error:(NSError *)error {
FIRAuthCredential *credential = [FIRFacebookAuthProvider credentialWithAccessToken:[FBSDKAccessToken currentAccessToken].tokenString];
[[FIRAuth auth] signInWithCredential:credential completion:^(FIRUser *user, NSError *error) {
if (error) {
// ...
return;
}
// User successfully signed in. Get user data from the FIRUser object
// ...
NSLog(@"%@", user.email);
// [self showEmail];
}];
}
答案 0 :(得分:1)
问题1:您应该查看此Additional alert shows up using AppAuth to authenticate in swift4
问题2:我认为在您的application:openURL:options:
方法中,您应该检查输入网址。如果是Facebook的网址,请使用FBSDKApplicationDelegate
否则使用GIDSignIn
。
- (BOOL)application:(nonnull UIApplication *)application openURL:(nonnull NSURL *)url options:(nonnull NSDictionary<NSString *, id> *)options {
if (/*url is Facebook url*/) {
return [[FBSDKApplicationDelegate sharedInstance] application:application openURL:url sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey] annotation:options[UIApplicationOpenURLOptionsAnnotationKey]];
} else {
return [[GIDSignIn sharedInstance] handleURL:url sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey] annotation:options[UIApplicationOpenURLOptionsAnnotationKey]];
}
}