WepsTech
The world of development
Hello Guys, Welcome to the new tutorial. Here I am going to tell you how to do Facebook login in iOS with firebase. Facebook developer tools provide lot’s of feature but today in this tutorial we are going to cove only Facebook login with firebase.
The very first step is.You have to create on project into Firebase. As shown into the video. So please first follow the procedures from firebase developer website.
After completing this step then create one application into Facebook developer account, If you are new to this then check the complete video.
After that, enable Facebook authentication in firebase and copy app id and app secrete key from the Facebook application setting. As mention into video.
After that, If you done with all the setup then write the code in your button click action.
@IBAction func fbAction(_ sender: Any) { let loginManager = LoginManager() loginManager.logIn(permissions: ["public_profile", "email"], from: self) { (result, error) in if let error = error { print("Failed to login: \(error.localizedDescription)") return } guard let accessToken = AccessToken.current else { print("Failed to get access token") return } let credential = FacebookAuthProvider.credential(withAccessToken: accessToken.tokenString) // Perform login by calling Firebase APIs Auth.auth().signIn(with: credential, completion: { (user, error) in if let error = error { print("Login error: \(error.localizedDescription)") let alertController = UIAlertController(title: "Login Error", message: error.localizedDescription, preferredStyle: .alert) let okayAction = UIAlertAction(title: "OK", style: .cancel, handler: nil) alertController.addAction(okayAction) self.present(alertController, animated: true, completion: nil) return }else { self.currentUserName() } }) } }
In addition, there is one more function which will give you the definition of self.currentUserName() function.
func currentUserName() { if let currentUser = Auth.auth().currentUser { self.btn_sign_out.isHidden = false lb_login_Status.text = "YOU ARE LOGIN AS - " + (currentUser.displayName ?? "DISPLAY NAME NOT FOUND") } }
No Finally we will see the log out button action!
@IBAction func btnSignOutAction(_ sender: Any) { let firebaseAuth = Auth.auth() do { try firebaseAuth.signOut() self.lb_login_Status.text = "Please login now" self.btn_sign_out.isHidden = true } catch let signOutError as NSError { print ("Error signing out: %@", signOutError) } }
So that was the complete scenario of Facebook login in iOS with firebase. I hope this tutorial helps you to dive into this. If you want to learn more about this then please visits on below link-
Facebook login in iOS using firebase Swift 5
Awesome tutorial. Thank you so much for sharing this tutorial!
Summer
Excelent, but how do you clear the login cache on exit!? because when you run the App again, press Facebook Login and choose CANCEL it automatically uses the last logged in account info!!
Pushpendra Kumar
Nice to hear this question, This will help for auto login! If you want to remove the last login from the application on application close then keep the following code inside your AppDelegate.swift optional func applicationWillTerminate(_ application: UIApplication)
let firebaseAuth = Auth.auth()
do {
try firebaseAuth.signOut()
} catch let signOutError as NSError {
print (“Error signing out: %@”, signOutError)
}
Khalid
Fantastic.
jack
very informative video sir, but you didn’t mention code you wrote in plist?
Pushpendra Kumar
Hi Jack – for that you can watch this video https://youtu.be/Oq004VptIt4?t=354
ปั้มไลค์
Like!! I blog frequently and I really thank you for your content. The article has truly peaked my interest.
Jaffer
please share what you did on info.plist because the app crashing for me in that!!!
Pushpendra Kumar
Hey, You need to add the App Id into the Info P List. Please get this information from the Official Blog of Facebook.