我正在开发一个使用Firebase + Facebook身份验证登录并验证用户身份的iOS应用。该功能有效,仅当单击“继续使用Facebook”按钮时,它会在用户单击“继续”或“取消”之前自动关闭对话框。
我已经上传了一个gif作为示例,以进行更好的解释,我第一次单击它时,它可以工作,但是在第二次或第三次尝试后,甚至在关闭应用程序并再次启动后,问题仍然存在。
这是我的AppDelegate.swift
中的代码段:
import UIKit
import Firebase
import GoogleSignIn
import FBSDKLoginKit
import FacebookCore
var fbAccessToken: AccessToken?
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// Use Firebase library to configure APIs
FirebaseApp.configure()
// Facebook
FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
// Added from Firebase docs -- Google
GIDSignIn.sharedInstance().clientID = FirebaseApp.app()?.options.clientID
GIDSignIn.sharedInstance().delegate = self
return true
}
@available(iOS 9.0, *)
func application(_ application: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
// FB Auth
let handled: Bool = SDKApplicationDelegate.shared.application(application, open: url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String!, annotation: options[UIApplicationOpenURLOptionsKey.annotation]!)
if !handled{
// Google Auth
print("clicked google sign in")
return GIDSignIn.sharedInstance().handle(url,
sourceApplication:options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String,
annotation: [:])
}
return true
}
这是我的ViewController.Swift
import UIKit
import Firebase
import FirebaseAuth
import GoogleSignIn
import FacebookCore
import FBSDKLoginKit
import FacebookLogin
class ViewController: UIViewController, GIDSignInUIDelegate {
@IBOutlet weak var FbLogin: FBSDKLoginButton! // Facebook Login button
override func viewDidLoad() {
super.viewDidLoad()
// set delegates
GIDSignIn.sharedInstance().uiDelegate = self
//GIDSignIn.sharedInstance().signIn()
FbLogin.addTarget(self, action: #selector(handleSignInWithFacebookTapped), for: .touchUpInside)
}
@objc func handleSignInWithFacebookTapped() {
let loginManager = LoginManager()
loginManager.logIn(readPermissions: [.publicProfile, .email], viewController: self ){ (result) in
switch result{
case .success(grantedPermissions: _, declinedPermissions: _, token: _):
print("Succesfully logged into facebook")
self.signIntoFirebase()
case .failed(let err):
print(err)
case .cancelled:
print("cancelled")
}
}
}
fileprivate func signIntoFirebase(){
guard let authenticationToken = AccessToken.current?.authenticationToken else {return}
let credential = FacebookAuthProvider.credential(withAccessToken: authenticationToken)
Auth.auth().signIn(with: credential) { (user, err) in
if let err = err {
print(err)
return
}
print("Sucessfully hit Firebase")
DispatchQueue.main.async {
self.performSegue(withIdentifier: "loginPush", sender: self)
print("Logged in successfully")
}
}
}
以下是该按钮的“连接检查器”的图像: