Facebook SDK to get user pictures - ios

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.

Related

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?

Is it normal to ask "you have already authorize " through Facebook SDK

This is just a question, if I log in once with Facebook login using the latest SDK and then I try to log in again, its asking me "you have already authorize app name". Is it normal or Do I have to change something to avoid it.
In my scenario, I made SSO on in Facebook App Setting this does not resolve the issue!!
Whenever my login window shows, I logout the Facebook and clear the access token, first I thought because of this, this is being asked however, I omitted the code and still it is asking!!
I can post code if necessary!!!
override func viewDidLoad() {
super.viewDidLoad()
fbloginButton.delegate = self
fbloginButton.readPermissions = ["public_profile", "email", "user_friends"]
if (FBSDKAccessToken.currentAccessToken() != nil)
{
var loginM:FBSDKLoginManager = FBSDKLoginManager()
loginM.logOut()
FBSDKAccessToken.setCurrentAccessToken(nil)
}
}
#IBAction func loginFb(sender:AnyObject)
{
fbLoginManager.logInWithReadPermissions(["email"], fromViewController: self.presentingViewController, handler: { (result, error) -> Void in
if (error == nil){
var fbloginresult : FBSDKLoginManagerLoginResult = result
}
})
}
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
NSLog("didCompleteWithResult")
if ((error) != nil)
{
}
else if result.isCancelled {
}
else {
// If you ask for multiple permissions at once, you
// should check if specific permissions missing
if result.grantedPermissions.contains("email")
{
///Here I call a function to get data
}
}
It's been a while since I've worked with the Facebook SDK, but I think when you logout, you just invalidate your current accessToken and end your session, but it does not deauthorize your app.
So, when you login, you just generate a new accessToken for the FB app that you already authorized. You need to login again (to verify your identity), but you don't need to authorize again, hence it's giving you that message.
There's a Graph API call to revoke permissions, if that is what you are looking for.
And if your question is whether this is "normal". Yes, I'd say that's normal. It's a visual indication that you are at that point connecting again with a previously authorized FB app. Avoiding it might work by not sending any permissions, but you generally don't know if an already authorized user is logging in again, or a completely new user. If the latter logs in without permissions, well, your app wouldn't work as supposed too.

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.

Resources