sign in with facebook swift error - ios

I am trying to do Sign In with Facebook/Firebase Swift authentication, and I'm at the point where there's a functioning button. I am trying to run something that performs a segue once the login is complete, but I have not been successful in my tries.
func viewDidLoad(){
let loginButton = LoginButton(readPermissions: [ .publicProfile, .email])
loginButton.center = view.center
view.addSubview(loginButton)
if let accessToken = AccessToken.current {
print("signed in")
}
}
func loginButtonDidLogOut(_ loginButton: LoginButton){
print("logged out of fb")
}
func loginButton(_ loginButton: LoginButton!, didCompleteWith result: LoginResult!, error: Error!) {
if error != nil {
print(error)
return
}

Related

Issue with FB Login

Hellos developers,
who anyone can help me with one issue , I try to make a Facebook login and after that I want to go to another view.
But I can't make that happens, could some one can help me with that?
I put my code below:
import UIKit
import FBSDKLoginKit
import FBSDKCoreKit
class ViewController: UIViewController , LoginButtonDelegate{
func loginButton(_ loginButton: FBLoginButton, didCompleteWith result: LoginManagerLoginResult?,
error: Error?) {
loginButton.permissions = ["public_profile","email"]
if error != nil {
print("Something is Wrong... \(String(describing: error))")
return
}
print("Succesful loged in !")
}
func loginButtonDidLogOut(_ loginButton: FBLoginButton) {
loginButton.titleLabel?.text = "Adios"
print("GoodBye ")
}
override func viewDidLoad() {
super.viewDidLoad()
let loginButton = FBLoginButton()
loginButton.center = view.center
view.addSubview(loginButton)
loginButton.permissions = ["public_profile", "email"]
loginButton.addTarget(self, action: #selector(getUserFBInfo), for: .touchUpOutside)
}
#objc func getUserFBInfo(){
print("se logro ")
let token = AccessToken.current
if token == AccessToken.current {
performSegue(withIdentifier: "secondActivity", sender: self)
}else if token!.isExpired{
print("no sucede nada")
}
}
}
If you want the LoginButtonDelegate to call your delegate function (e.g. loginbutton:didCompleteWithResult:error:), remember to assign the delegate to the FBLoginButton.
In the above code, since the delegate is the UIViewController itself (not a must), you will need to add loginButton.delegate = self

There is not saved user's object in the authentication tab Firebase

I ran the facebook auth and it works well, but When i go to the firebase authentication tab, the user is not there
Here's the code
import UIKit
import FBSDKCoreKit
import FBSDKLoginKit
import FirebaseFacebookAuthUI
class ViewController: UIViewController {
#IBOutlet weak var name: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let loginButton = FBSDKLoginButton()
loginButton.readPermissions = ["public_profile", "email"]
// Optional: Place the button in the center of your view.
loginButton.center = view.center
view.addSubview(loginButton)
}
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
if let error = error {
print(error.localizedDescription)
return
} else {
let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
print(credential)
Auth.auth().signIn(with: credential) { (user, error) in
if let _ = error {
return
}
self.name.text = "\(user)"
print("user is sign in")
}
}
}
}
Do i need to do any extra configuration?

Can't connect to Firebase after connecting to Facebook - In Swift 3

Before moving to Swift 3 the user had to log in to the app by Facebook, and then it automatically logged in to your Firebase account. Everything was good, but then I moved to Swift 3. The log in button still exists, but after it asks for permission to log in to Facebook - nothing happens.
Here is my code before moving to Swift 3:
var loginButton: FBSDKLoginButton = FBSDKLoginButton()
override func viewDidLoad() {
super.viewDidLoad()
loginButton.hidden = true
FIRAuth.auth()?.addAuthStateDidChangeListener { auth, user in
if let user = user {
// User is signed in.
currentEmail = user.email!
print("DONE")
NSNotificationCenter.defaultCenter().postNotificationName("dontShowLogInView", object: nil)
} else {
// No user is signed in.
self.loginButton.center = self.view.center
self.loginButton.readPermissions = ["public_profile", "email", "user_friends"]
self.loginButton.delegate = self
self.view.addSubview(self.loginButton)
self.loginButton.hidden = false
}
}
// Optional: Place the button in the center of your view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
print("user logged in")
self.loginButton.hidden = true
activityIndicator.startAnimating()
if error != nil {
self.loginButton.hidden = false
activityIndicator.stopAnimating()
print("???")
//handle errors
} else if result.isCancelled {
self.loginButton.hidden = false
activityIndicator.stopAnimating()
let loginManager = FBSDKLoginManager()
loginManager.logOut()
//handle cancel
print("canceled")
} else {
let credential = FIRFacebookAuthProvider.credentialWithAccessToken(FBSDKAccessToken.currentAccessToken().tokenString)
FIRAuth.auth()?.signInWithCredential(credential) { (user, error) in
print("user logged to firebase app")
}
}
}
func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
print("user logged out")
}
And this is the code after moving to Swift 3:
var loginButton: FBSDKLoginButton = FBSDKLoginButton()
override func viewDidLoad() {
super.viewDidLoad()
//getFBUserData()
loginButton.isHidden = true
FIRAuth.auth()?.addStateDidChangeListener { auth, user in
if let user = user {
// User is signed in.
currentEmail = user.email!
print("DONE")
print(currentEmail)
NotificationCenter.default.post(name: Notification.Name(rawValue: "dontShowLogInView"), object: nil)
} else {
// No user is signed in.
self.loginButton.center = self.view.center
self.loginButton.readPermissions = ["public_profile", "email", "user_friends"]
self.loginButton.delegate = self
self.view.addSubview(self.loginButton)
self.loginButton.isHidden = false
}
}
// Optional: Place the button in the center of your view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: NSError!) {
print("user logged in")
self.loginButton.isHidden = true
activityIndicator.startAnimating()
if error != nil {
self.loginButton.isHidden = false
activityIndicator.stopAnimating()
print("???")
//handle errors
} else if result.isCancelled {
self.loginButton.isHidden = false
activityIndicator.stopAnimating()
let loginManager = FBSDKLoginManager()
loginManager.logOut()
//handle cancel
print("canceled")
} else {
let credential = FIRFacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
FIRAuth.auth()?.signIn(with: credential) { (user, error) in
print("user logged to firebase app")
}
}
}
func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
print("user logged out")
}

Facebook Login with Firebase and LoginButton For IOS

I am trying to perform a FacebookLogin using Firebase and the Facebook Login button, but I keep getting this
error:Facebook login was cancelled.
2016-04-01 19:08:15.721
biblos[778:408049] Warning: Attempt to present
on
whose view is not in the
window hierarchy!
User logged out...
Facebook login was cancelled.
2016-04-01 19:08:53.675 biblos[778:408049] Warning: Attempt to present
on
whose view is not in the
window hierarchy!
My ViewController code is as follows:
import UIKit
import Firebase
import FBSDKCoreKit
import FBSDKLoginKit
class ViewController: UIViewController, FBSDKLoginButtonDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var loginButton = FBSDKLoginButton()
loginButton.readPermissions = ["public_profile", "email", "user_friends"]
loginButton.center = self.view.center
loginButton.delegate = self
self.view.addSubview(loginButton)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Facebook Login
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!)
{
let ref = Firebase(url: "https://biblos-thebookapp.firebaseio.com")
let facebookLogin = FBSDKLoginManager()
facebookLogin.logInWithReadPermissions(["email"], handler: {
(facebookResult, facebookError) -> Void in
if facebookError != nil {
print("Facebook login failed. Error \(facebookError)")
} else if facebookResult.isCancelled {
print("Facebook login was cancelled.")
} else {
let accessToken = FBSDKAccessToken.currentAccessToken().tokenString
ref.authWithOAuthProvider("facebook", token: accessToken,
withCompletionBlock: { error, authData in
if error != nil {
print("Login failed. \(error)")
} else {
print("Logged in! \(authData)")
}
})
}
})
}
func loginButtonDidLogOut(loginButton: FBSDKLoginButton!)
{
print("User logged out...")
}
}
This method is deprecated. Try to use logInWithReadPermissions:fromViewController:handler: instead and pass your UIViewController or nil (topmost will be used).
https://developers.facebook.com/docs/reference/ios/current/class/FBSDKLoginManager/

I'm trying integerate facebook login it shows "view controller does not conform protocol"

here's the code
override func viewDidLoad()
{
if (FBSDKAccessToken.currentAccessToken() == nil)
{
print("Not logged in..")
}
else
{
let loginView : FBSDKLoginButton = FBSDKLoginButton()
self.view.addSubview(loginView)
loginView.center = self.view.center
loginView.readPermissions = ["public_profile", "email", "user_friends"]
loginView.delegate = self //shows error:view controller does not conform to protocol 'FBSDKLoginButtonDelegate'
}
super.viewDidLoad()
}
And the loginButton method
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!)
{
if error == nil
{
print("Login complete.")
self.performSegueWithIdentifier("showNew", sender: self)
}
else
{
print(error.localizedDescription)
}
}
func loginButtonDidLogOut(loginButton: FBSDKLoginButton!)
{
print("User logged out...")
}
The view controller is frontpageViewConroller,I have logged into facebook registered with the AppId downloaded the iOS SDK and imported them,unable to solve this help me out
thanks in advance!
Have you setting the delegate on you viewController like following code:
class ViewController: UIViewController,FBSDKLoginButtonDelegate {
Please check the following full tutorial login with Facebook for swift
Tutorial: How To Use Login in Facebook SDK 4.1.x for Swift

Resources