2

Get some troubles with Parse and Swift. I'm trying to use Facebook Login with Parse class PFFacebookUtils. That's the code:

// LOG IN or SIGN UP with FACEBOOK


@IBAction func FacebookLogin() {
    PFFacebookUtils.logInWithPermissions(permissions, {(user: PFUser!, error: NSError!) -> Void in
        if error == nil {
            if user == nil {
                NSLog("Uh oh. The user cancelled the Facebook login.")
                let userCancelledAlert = UIAlertController(title: "Error", message: "You cancelled the Facebook Login", preferredStyle: .Alert)
                let okButton = UIAlertAction(title: "OK", style: .Default, handler: nil)
                userCancelledAlert.addAction(okButton)
                let cancelButton = UIAlertAction(title: "Cancel", style: .Default, handler: nil)
                userCancelledAlert.addAction(cancelButton)
                self.presentViewController(userCancelledAlert, animated: true, completion: nil)            
            } else if user.isNew {
                NSLog("User signed up and logged in through Facebook!")
                FBRequestConnection.startWithGraphPath("me?fields=email", completionHandler: { (connection, result, error) -> Void in
                    if error == nil {
                        let fbGraphicObject = result as FBGraphObject
                        println(fbGraphicObject)
                    }
                })
            } else {
                NSLog("User logged in through Facebook!")            
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    var homeViewController = self.storyboard?.instantiateViewControllerWithIdentifier("homePage") as HomeViewController
                    self.presentViewController(homeViewController, animated: true, completion: nil)
                })
            // end login (success)
            }
        } else {  // if login doesnt work
            println(error!)
            /*
            let facebookError = UIAlertController(title: "Error", message: "Cant connect to Facebook, check your connectivity", preferredStyle: .Alert)
            let okButton = UIAlertAction(title: "OK", style: .Default, handler: nil)
            facebookError.addAction(okButton)
            let cancelButton = UIAlertAction(title: "Cancel", style: .Default, handler: nil)
            facebookError.addAction(cancelButton)
            self.presentViewController(facebookError, animated: true, completion: nil)
            */
        }
    })
}

So now my question is: why can't i see what are the permissions when i sign up? I mean, permissions is an array.

let permissions = ["email","user_birthday"]

But when i sign up the message is something like "[APP-NAME] wants to access to your basic info and list of friend. Why not the email or the user_birthday. Then, how can i store those infos in my Parse database? I mean, I see id, username, password (obviously, i have not), authData etc. I have the email field, but it's empty. I guess there's something wrong in the code. Anyone can find? Finally, don't know why, but xCode autocompletion for Parse class (I'm using Swift) doesn't work. I imported the framework (i added the bridging header), and it worked some days ago.

rici
  • 234,347
  • 28
  • 237
  • 341
  • Just before entering the logInWithPermissions, can you `println(permissions)` to see what's inside ? As for XCode, it might be related to http://stackoverflow.com/questions/24006206/sourcekitservice-terminated – Kalzem Jan 10 '15 at 13:00
  • @BabyAzerty , permissions contain "[email, user_birthday]", as I set. Yeah, i read that post before but that's not my problem. I don't have any message or black window. Simply, xCode editor doesn't suggests me anything about Parse framework (classes, methods or properties) – Giuseppe Capoluongo Jan 10 '15 at 14:16

1 Answers1

1

I was working on this same thing, trying to get Parse to store the Facebook email as the username instead of the auto generated id. Since Facebook updated the Graph API to v2.0, it seems like there isn't much out there to tell you how to do it. This is what I did:

First, I set up the proper permissions to retrieve the email from Facebook. This is done within the "didLoginUser" method.

func logInViewController(logInController: PFLogInViewController, didLogInUser user: PFUser) {
    if !PFFacebookUtils.isLinkedWithUser(user) {
        let facebookLinkSuccess : (succeeded: Bool, error: NSError?) -> Void = { succeeded, error in
            if(succeeded){
                println("PF User linked with facebook")
            }
        }
        PFFacebookUtils.linkUserInBackground(user, withReadPermissions: ["public_profile", "email", "user_friends","user_birthday","user_location"], block: facebookLinkSuccess)
    } ...

Second, I found this stackoverflow, which helps you make the FB Graph API call, and then update the Parse PFUser object with the desired values: Facebook iOS SDK & Swift - How do I create dependent batch requests?

I set up the call as follows:

        if (FBSDKAccessToken.currentAccessToken() != nil){
            var userProfileRequestParams = [ "fields" : "id, name, email"]
            let userProfileRequest = FBSDKGraphRequest(graphPath: "me", parameters: userProfileRequestParams)
            let graphConnection = FBSDKGraphRequestConnection()
            graphConnection.addRequest(userProfileRequest, completionHandler: { (connection: FBSDKGraphRequestConnection!, result: AnyObject!, error: NSError!) -> Void in
                if(error != nil){
                    println(error)
                }
                else {
                    let fbEmail = result.objectForKey("email") as! String
                    let fbUserId = result.objectForKey("id") as! String
                    if(fbEmail != "") {
                        PFUser.currentUser()?.username = fbEmail
                        PFUser.currentUser()?.saveEventually(nil)
                    }
                    println("Email: \(fbEmail)")
                    println("FBUserId: \(fbUserId)")
                }
            })
            graphConnection.start()
        }

I put this code in func logInViewController(logInController: PFLogInViewController, didLogInUser user: PFUser) { ...

If you have questions about that part, check out this video, it has a similar idea, but with Twitter. https://www.youtube.com/watch?v=lnf7KHHeiO0

Hope this helps somebody. I just spent almost 5 hours on this.

Community
  • 1
  • 1
ShadowMinhja
  • 293
  • 2
  • 10