我想在使用 firebase auth 的 View 中实现使用 google 登录和使用 Facebook 登录
AppDelegate.swift
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
let handled = FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication,annotation: annotation)
return handled
}
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
return GIDSignIn.sharedInstance().handle(url,
sourceApplication:options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String,annotation: [:])
}
}
ViewController.swift
class ViewController: UIViewController, GIDSignInDelegate, GIDSignInUIDelegate {
override func viewDidLoad() {
super.viewDidLoad()
GIDSignIn.sharedInstance().uiDelegate = self
GIDSignIn.sharedInstance().clientID = FirebaseApp.app()?.options.clientID
GIDSignIn.sharedInstance().delegate = self
}
@IBAction func GSignInPressed(_ sender: Any) {
print("GsignIn pressed")
GIDSignIn.sharedInstance().signIn()
}
@IBAction func fbPressed(_ sender: Any) {
let facebookLogin = FBSDKLoginManager()
facebookLogin.logIn(withReadPermissions: ["email", "public_profile"], from: self) { (result, error) in
if error != nil {
print("uhhhh! unable to connect with facebook")
} else if result?.isCancelled == true {
print("Uhhh! User cancelled FB auth")
} else {
print("Uhhh! Sucessfully authenticated with FB")
let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
self.firebaseAuth(credential)
}
}
}
func firebaseAuth(_ credential: AuthCredential) {
Auth.auth().signIn(with: credential) { (user, error) in
if error != nil {
// ...
print("Error in credentials")
print(error!)
return
}
// User is signed in
// ...
let email = user?.email
print(email!)
}
}
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
if error != nil {
print("error in google sign in")
return
}
guard let authentication = user.authentication else { return }
let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,accessToken: authentication.accessToken)
self.firebaseAuth(credential)
}
}
最佳答案
//MARK: - Handle URL for FB and Google Sign -
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
//handle the URL that your application receives at the end of the authentication process -
var flag: Bool = false
// handle Facebook url scheme
if let wasHandled:Bool = FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation) {
flag = wasHandled
}
// handle Google url scheme
if let googlePlusFlag: Bool = GIDSignIn.sharedInstance().handle(url, sourceApplication: sourceApplication!, annotation: annotation) {
flag = googlePlusFlag
}
return flag
}
在 AppDelegate 中尝试使用此方法
关于ios - 在ios中使用swift 3进行Google和Facebook Firebase登录身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45755126/