How to change a parse's Facebook account custom fields - ios

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.

Related

Twitter / Fabric login button not behaving as expected

I have a very simple iOS project where I'm using Twitter/Fabric login button for user login to my app.
I've managed to get the Fabric login button working. When the user clicks on the Twitter login button they are automatically authenticated (that's if they are logged into the Twitter app) otherwise the user is presented with a Twitter login screen.
I'm not sure why the user is automatically authenticated when they are logged into the Twitter app on their phone.
Is there a way to use the Twitter/Fabic API to open the Twitter app and ask for permission to grant access to my app similar to Facebook login even if the user is logged into the Twitter App.
This is what my AppDelegate looks like:
Twitter.sharedInstance().start(withConsumerKey: "someKey", consumerSecret: "someSecret")
Fabric.with([Twitter.self])
This is what my ViewController looks like:
#IBOutlet private weak var twitterLoginButton: TWTRLogInButton!
// and
twitterLoginButton.logInCompletion = {(session, error) in
if error != nil {
print("ERROR: \(error)")
} else {
if let unwrappedSession = session {
print(unwrappedSession.userName)
}
}
}
Twitter.sharedInstance().logIn { (session, error) in
if let unwrappedSession = session {
print("Signed in as: \(unwrappedSession.userName)")
} else {
print("ERROR: \(error)")
}
}
Fabric documentation says that the first default for login is to go through the Twitter app (that may be why your user is automatically authenticated if they're already logged in the app), otherwise it will go through the webAuth login flow.
"To force the log in flow to use the web OAuth flow pass the TWTRLoginMethodWebBased method to the relevant log in methods."
// If using the TWTRLoginButton
let logInButton = TWTRLogInButton() { session, error in
}
logInButton.loginMethods = [.webBased]
So if you want to force the user to go through the web flow, try adding to your code: twitterLoginButton.loginMethods = [.webBased]

Sign in with different google account Firebase 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

Swift: Linking Facebook users with existing PFUsers Not Working on Parse Server

I'm trying to integrate the Facebook login/signup into my app to work alongside the standard login/signup with email. I am using Swift and have migrated to Parse Server.
I've followed the Parse iOS guide on Facebook Users. However, with Parse Server, the Facebook integration isn't working normally according to all the tutorials and documentation out there.
How can I link existing PFUsers with their Facebook accounts when they log in with Facebook in the app?
I am using the Swift code from this section of the guide (https://parse.com/docs/ios/guide#users-linking) to associate an existing PFUser to a Facebook account (see code below), however, this code is not executing for some unknown reason and therefore is not linking a users existing account they created through the email sign up with their Facebook account which has the same email. Why is this code not executing?
UPDATE: The isLinkedWithUser method requires that the user be logged in. However, when a user is signed out/just downloaded the app, the PFUser.currentUser() is nil because the user is not logged in. Therefore, the linkUserInBackground method won't know which PFUser to link the Facebook account with. I believe these methods are now useless because they are to link after the fact (after they log in). But I don't want to create two users.
Is there a Cloud Code example or other Swift logic to accomplish this?
I am not sure what the problem is. Appreciate any help!
Source: https://parse.com/docs/ios/guide#users-facebook-users
func fbLoginButtonTapped() {
print("fbLoginButtonTapped")
let permissions = ["public_profile", "email"]
PFFacebookUtils.logInInBackgroundWithReadPermissions(permissions, block: {
(user: PFUser?, error: NSError?) -> Void in
if let user = user {
if user.isNew {
print("User signed up and logged in through Facebook!")
} else {
print("User logged in through Facebook!")
// This API doesn't seem to be working with Parse Server
if !PFFacebookUtils.isLinkedWithUser(user) {
PFFacebookUtils.linkUserInBackground(user, withReadPermissions: nil, block: {
(succeeded: Bool?, error: NSError?) -> Void in
if error == nil {
print("Woohoo, the user is linked with Facebook!")
} else {
print("User is not linked with Facebook.")
}
})
}
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("HomeNavigationController") as! UINavigationController
self.presentViewController(viewController, animated: true, completion: nil)
})
}
} else {
print("User cancelled the Facebook login.")
}
}
Did you configure the FBSDK correctly in your Swift app? You should need to add a few callbacks in your app delegate that handle FBSDK (https://developers.facebook.com/docs/ios/getting-started/).
Also, was that app running on parse.com or are you using it for the 1st time on parse-server?

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.

Segue does not work after Facebook Login

For some reason after the user logs in via facebook the segue that takes the user to the profile screen is never executed.
var permissions = ["public_profile", "email"]
PFFacebookUtils.logInWithPermissions(permissions, block: {
(user: PFUser!, error: NSError!) -> Void in
if user == nil {
NSLog("Uh oh. The user cancelled the Facebook login.")
} elseif user.isNew {
println("the user is new")
NSLog("User signed up and logged in through Facebook!")
// THIS WORK
println("THE NEW USER FACEBOOK HAS LOGGED IN AND IS READY TO JUMP TO THE NEXT SCREEN")
//BUT THIS DOESN'T
self.performSegueWithIdentifier("jumpToSignUp", sender: self)
} else {
NSLog("User logged in through Facebook!")
}
})
in the code above, this line never get executed:
self.performSegueWithIdentifier("jumpToSignUp", sender: self)
The user can login via fb, the record get stored in parse, but the it never jumps to the (next)sign up screen.
I have verified that the segue identifier is correct. The weird part is that there is no error in the logs.
any ideas, why this might be happening?
I figured it out - my code is correct. The problem was the next screen. I had some IBOutlets that were not properly set up. I fixed it and it worked again. Just make sure that everything in your next screen is set up properly otherwise the segue will not work.

Resources