Tunds
Tunds

Reputation: 1817

Can't get account info Parse & FB SDK

I'm using the ParseFacebookUtilsV4 and the fb idk seem to be having issues with retrieving the users info just to print into the console for now. The issue is that it seems that the block to start the request isn't being executed since whenever i debug it just seems to skip the start completion handler.

// View controller code
        PFFacebookUtils.facebookLoginManager().loginBehavior = .web

        var loginTask = PFFacebookUtils.logInInBackground(withReadPermissions: [])

        loginTask.continue( { (bfTask) -> Any? in

            print("I'm here")

            let request = FBSDKGraphRequest(graphPath:"me", parameters: ["fields":"id,email,name,first_name,last_name,picture"]  )
            print(request)
            request?.start {

                (connection, result, error) in

                print(result)

            }
            return ""
        })

// App delegate config

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        let configuration = ParseClientConfiguration {
            $0.applicationId = "xxxx"
            $0.clientKey = "xxxxx"
            $0.server = "https://parseapi.back4app.com"
        }
        Parse.initialize(with: configuration)

        PFFacebookUtils.initializeFacebook(applicationLaunchOptions: launchOptions)

        PFTwitterUtils.initialize(withConsumerKey: "xxxxx", consumerSecret: "xxxxx")

        // Override point for customization after application launch.
        return true
    }

Upvotes: 1

Views: 184

Answers (1)

Larry Mickie
Larry Mickie

Reputation: 511

here's the func I use for the Facebook request. I am using a block request:

func loadFacebookUserDetails() {

    // Define fields we would like to read from Facebook User object
    let requestParameters = ["fields": "id, email, first_name, last_name, name"]

    // Send Facebook Graph API Request for /me
    let userDetails = FBSDKGraphRequest(graphPath: "me", parameters: requestParameters)
    userDetails.startWithCompletionHandler({
        (connection, result, error: NSError!) -> Void in
        if error != nil {
            let userMessage = error!.localizedDescription
            let myAlert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertControllerStyle.Alert)

            let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
            myAlert.addAction(okAction)
            self.presentViewController(myAlert, animated: true, completion: nil)

            PFUser.logOut()
            return
        }

        // Extract user fields
        let userId:String = result.objectForKey("id") as! String
        let userEmail:String? = result.objectForKey("email") as? String
        let userFirstName:String?  = result.objectForKey("first_name") as? String
        let userLastName:String? = result.objectForKey("last_name") as? String



        // Get Facebook profile picture
        let userProfile = "https://graph.facebook.com/" + userId + "/picture?type=large"

        let profilePictureUrl = NSURL(string: userProfile)

        let profilePictureData = NSData(contentsOfURL: profilePictureUrl!)


        // Prepare PFUser object
        if(profilePictureData != nil)
        {
            let profileFileObject = PFFile(name:"profilePic.jpeg",data:profilePictureData!)
            PFUser.currentUser()?.setObject(profileFileObject!, forKey: "ProfilePic")
        }

        PFUser.currentUser()?.setObject(userFirstName!, forKey: "Name")
        PFUser.currentUser()?.setObject(userLastName!, forKey: "last_name")



        if let userEmail = userEmail
        {
            PFUser.currentUser()?.email = userEmail
            PFUser.currentUser()?.username = userEmail
        }



        PFUser.currentUser()?.saveInBackgroundWithBlock({ (success, error) -> Void in


            if(error != nil)
            {
                let userMessage = error!.localizedDescription
                let myAlert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertControllerStyle.Alert)
                let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
                myAlert.addAction(okAction)
                self.presentViewController(myAlert, animated: true, completion: nil)
                PFUser.logOut()
                return


            }


            if(success)
            {
                if !userId.isEmpty
                {
                    NSUserDefaults.standardUserDefaults().setObject(userId, forKey: "user_name")
                    NSUserDefaults.standardUserDefaults().synchronize()


                    dispatch_async(dispatch_get_main_queue()) {
                        self.dismissViewControllerAnimated(true, completion: nil)
                    }

                }

            }

        })


    })

}

Upvotes: 1

Related Questions