Sign in with different google account Firebase iOS - ios

I am new to iOS and Firebase in general and I'm struggling to find out how I can get the app to show the google oAuth modal when I click the signin button. At the moment, it comes up on the first signup instance, but I have to delete the app to get it working again. This can be a bit cumbersome if someone wants to change google accounts.
// Google Sign In
#IBAction func gooSignInBtn(sender: AnyObject) {
GIDSignIn.sharedInstance().signIn()
}
When I call sign out, it signs out, but the below modal doesn't show up again. It just automatically signs into the last signed in google account.
Does the try! FIRAuth.auth()!.signOut() function only sign out the member temporarily?
#IBAction func signOut(sender: AnyObject) {
try! FIRAuth.auth()!.signOut()
FIRAuth.auth()?.addAuthStateDidChangeListener({ (auth: FIRAuth, user: FIRUser?) in
if let user = user {
// User is signed in.
print(user)
} else {
// No user is signed in.
print("user signed out")
}
})
}

Try adding GIDSignIn.sharedInstance().signOut() for signout

Related

iOS Firebase sign in. Show activity indicator after Google account choosing

I have a ViewController with a Sign in button used to sign in into Firebase with a Google Account:
GIDSignIn.sharedInstance().signIn()
When I click the button, this appears:
Google account choosing
After selecting an account and if the authentication is successful, I want to load a second ViewController. For this, I have a listener in the first ViewController that will sign in again when the authentication state changes, this time successfully, without asking the account again and sending me directly to the second ViewController:
Auth.auth().addStateDidChangeListener({ auth, user in
if let _ = user {
GIDSignIn.sharedInstance().signIn()
}
})
The problem is that I want an activity indicator to be shown when I go back to the first ViewController from the account chooser. Because the app may be there for a few seconds during the authentication process and I don't want the user to tap again the Sign In button, while the first signing in hasn't already finished.
I need a way to recognise that a signing in process is taking place, to show an activity indicator that locks the screen to prevent the user from tapping again Sign in.
WORKAROUND 1
When I click the Sign in with Google button, I set an UserDefaults integer as 1. Then, when the ViewController is reloaded after the Google account chooser, I look for this integer and if it's 1, I don't stop the activity Indicator.
Because I want the activity indicator shown since the user clicks the button until the authentication is completed.
When button is clicked I do:
GIDSignIn.sharedInstance().signIn()
UserDefaults.standard.set(1, forKey: "signingIn")
UserDefaults.standard.synchronize()
In viewWillAppear I do:
if let _ = user {
GIDSignIn.sharedInstance().signIn()
} else {
if UserDefaults.standard.integer(forKey: "signingIn") != 1 {
self.stopActivityIndicator()
} else {
UserDefaults.standard.set(0, forKey: "signingIn")
UserDefaults.standard.synchronize()
}
}
When the authentication is completed, in GIDSignInDelegate there is the function that will be called. In this function, the activity indicator must be stopped:
// The sign-in flow has finished and was successful if |error| is |nil|.
- (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error;
WORKAROUND 2
I do a put the signIn Google function into a completion handler but it doesn't work:
self.completionHandlerSigIn {
self.stopActivityIndicator()
}
And the function is this:
func completionHandlerSigIn(completion: () -> Void) {
GIDSignIn.sharedInstance().signIn()
}
The problem is that the view is reloaded during the sign in process, after the account choosing. I need a way to recognize that I come from the Google Account choosing screen.
Just show the loading indicator right when the user clicks sign in, then hide it either when the authentication process returns with error or after processing the result. I don't use google sign in, but I can give you my example with Twitter.
#IBAction func onTwitterClicked(_ sender: UIButton) {
AuthManager.shared.loginWithTwitter(self)
}
Here is the loginWithTwitter method in AuthManager:
func loginWithTwitter(_ viewController:BaseController) {
self.provider = .twitter
viewController.showLoadingPanel()
TWTRTwitter.sharedInstance().logIn(completion: {session, error in
guard (error == nil) else {
viewController.hideLoadingPanel()
viewController.showInfoAlert("Oops", error!.localizedDescription, nil)
return
}
let credential = TwitterAuthProvider.credential(withToken: session!.authToken, secret: session!.authTokenSecret)
self.auth.signIn(with: credential, completion: {user, error in
viewController.hideLoadingPanel()
guard error == nil else {
viewController.showInfoAlert("Oops", error!.localizedDescription, nil)
return
}
self.tryConfirmUserInFirestore(user, viewController)
})
})
}

Removing Firebase authState upon viewDidDisappear

I'm using Firebase's new framework and I'm attempting to monitor the login state of the user on both the Login and Signup VC's separately. The problem is if the login state changes on the SignUp view the Auth State on the Login view gets called as well. My question is, how do I remove the auth state? I found the syntax on the Firebase website but am a little confused on what to pass in considering my code for the auth state:
FIRAuth.auth()?.addAuthStateDidChangeListener { auth, user in
if let theUser = user {
// User is signed in.
print("LOGGED IN!!!! :::: \(theUser)")
self.dismissViewControllerAnimated(true, completion: nil)
} else {
// No user is signed in.
print("Need to login first.")
}
}
Code to use to remove the auth, but unsure what to pass in.
FIRAuth.auth()?.removeAuthStateDidChangeListener(FIRAuthStateDidChangeListenerHandle)
Says I pass in a FIRAuthStateDidChangeListenerHandle, but how do I obtain this, or do I rewrite my authState code differently?
Just store the auth in a variable
self.authListener = FIRAuth.auth()?.addAuthStateDidChangeListener { auth, user in
if let theUser = user {
// User is signed in.
print("LOGGED IN!!!! :::: \(theUser)")
self.dismissViewControllerAnimated(true, completion: nil)
} else {
// No user is signed in.
print("Need to login first.")
}
}
and remove it later
FIRAuth.auth()?.removeAuthStateDidChangeListener(self.authListener)

How to test if my facebook friendlist function is working in swift

I found a piece of code here on stack overflow which I use to find the facebook friend list of the current user in my app.
Now I want to test that code but, due to the version 2 restrictions I can't find any of my friends because my app is still in development and nobody can use my app. So I cannot find any using friends.
How can I test my friendlist functionality?
I was just dealing with the same problem that you have and it's as WizKid says:
https://developers.facebook.com and login to your account
Go into your app then click the "Roles" tab then the "Test Users" tab
Click "Add" and this window will come up. Authorize the user for your app and add the permissions you want them to give your app.
To add friends just click the "Edit" button then "Manage this test User's friends". Add the friends you want the user to have. (Convenient autocomplete is included for other test users)
After you do the former steps I suggest you also change the test user's name and make them a password. The password can be the same for all your test users. You should also login as the test user ("Edit" again) and change their profile picture to differentiate between them especially if you're using profile pictures.
Here is the annoying part; to be able to see (in your app) the test user's friends that you just set, you have to repeat the following for each of the friends you want to see in your list of facebook friends. (This is assuming that you're not getting "invitable friends")
Reset your simulator
Login using the test user's email and password in the safari popup
Repeat No 7, 8 for every test user (This is to make the friends "facebook friends that have used the app")
Good luck and don't drive yourself crazy :) Hope this helps
Another fix is that the problem may be the login function. Because after I've added the right permissions to my test friend it still failed.
I got it working but the main problem seemed to be my login functions. I wasn't using the FBSDKLoginManager. This was easily implemented by:
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
println("User Logged In")
if ((error) != nil)
{
// Process error
}
else if result.isCancelled {
// Handle cancellations
}
else {
// If you ask for multiple permissions at once, you
// should check if specific permissions missing
if result.grantedPermissions.contains("email")
{
self.performSegueWithIdentifier("loggedIn", sender: self)
//self.returnUserData()
// Do work
}
}
}
func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
println("User Logged Out")
}
And then
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//if (FBSDKAccessToken.currentAccessToken() == nil)
//{
// User is already logged in, do work such as go to next view controller.
// self.performSegueWithIdentifier("loggedIn", sender: self)
// self.returnUserData()
//}
//else
//{
let loginView : FBSDKLoginButton = FBSDKLoginButton()
self.view.addSubview(loginView)
loginView.center = self.view.center
loginView.readPermissions = ["public_profile", "email", "user_friends"]
loginView.delegate = self
//}
// Do any additional setup after loading the view.
}
This fixed my problem completely after giving the test friend permission.

How to change a parse's Facebook account custom fields

I am trying to enable login with a Facebook account for my new social app but I need to know how to change custom Facebook accounts fields in parse so I can know if a Facebook user already set up his account or not. I managed to do it with regular accounts but now with Facebook accounts
Here is my method:
PFFacebookUtils.logInInBackgroundWithReadPermissions(permissions) {
(user: PFUser?, error: NSError?) -> Void in
if let user = user {
if user.isNew {
println("User signed up and logged in through Facebook!")
self.performSegueWithIdentifier("procceedToSetup", sender: self)
// I need to change here a parse field named "doneSetUp" with a type of a bool to false.
} else {
self.performSegueWithIdentifier("procceedToApp", sender: self)
}
} else {
println("Uh oh. The user cancelled the Facebook login.")
}
}
When a Facebook user completes the setup he/she will press the complete button and it will change the variable "doneSetUp" to true, I do it so Facebook accounts won't exit the app in the middle of the setup and when they log in back they will access the app without setup.
By the way, I would like to know how can I access Facebook account fields I chose with the permissions array, such as country, profile picture, name, etc...
Thanks for all the helpers!
Jeez, I found the reason.
forgot to put
PFUser.currentUser()?.save()
at the end.

Facebook SDK to get user pictures

I am new to swift and trying to learn how to read data from facebook using SDK.
Here are the code which I added:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if (FBSDKAccessToken.currentAccessToken() != nil)
{
// User is already logged in, do work such as go to next view controller.
println("User is already logged in")
}
else
{
let loginView : FBSDKLoginButton = FBSDKLoginButton()
self.view.addSubview(loginView)
FBSDKProfile.enableUpdatesOnAccessTokenChange(true)
loginView.center = self.view.center
loginView.readPermissions = ["public_profile", "email", "user_friends", "user_photos"]
loginView.delegate = self
}
}
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
println("User Logged In")
if ((error) != nil)
{
// Process error
}
else if result.isCancelled {
// Handle cancellations
}
else {
// If you ask for multiple permissions at once, you
// should check if specific permissions missing
if result.grantedPermissions.contains("email")
{
println("\(result.grantedPermissions)")
// Do work
}
}
}
When I run my application xcode simulator gives following warning:
"Some of the permissions below have not been approved by facebook" and it gives me option of submitting it for review. Is it because I am trying to get read permission for "user_photos"? I am trying to read user public profile as well as all the public pictures. Please let me know what should be my next steps to achieve the above.
In developers.facebook.com create Application and In setting tab fill up Contact Email and bundle id for app same as your app and In Status and review tab make "Do you want to make this app and all its live features available to the general public?" to YES.
Now you will get permission for email,public profile and user friends
In order to let your iOS app work together with facebook, you have to create a Facebook app and link it with your iOS app.
Here is the facebook developer page explaning all you need to use it.

Resources