Emil David Emil David - 2 months ago 10
Swift Question

Facebook Permissions iOS

So I was fiddling around with Facebook's iOS SDK and with it I managed to allow the user to login into my app. I had a

UIView
setup on the storyboard that was of
FBSDKLoginButton
class which according to this, its simply a subclass of
UIButton
. I then created an
@IBOutlet weak var facebookLogInBtn: FBSDKLoginButton!
. Then I went forth to setup my permissions as such

override func viewDidLoad()
{
super.viewDidLoad()

facebookLogInBtn.delegate = self
facebookLogInBtn.readPermissions = ["public_profile", "email", "user_friends"]
}


This works just fine and accomplishes my desired task and any extra permissions I require, I simply append them to the
facebookLoginBtn.readPermissions
array. But after some Googling, I found a bunch of sites which use the
FBSDKGraphRequest
along with the
FBSDKLoginManager
instead. Take for instance this blog. Now my questions are:


  1. Is my way deemed acceptable or hacky? It works but I just want to be certain that I am following the standard procedures.

  2. How does my way somehow tie into getting data through the Graph API? Because I am explicitly not declaring any
    FBSDKRequest
    or
    FBSDKLoginManager
    objects. I simply added my button then implemented its
    delegates
    and Voila! I am getting access. No need to parse any JSON data or anything.

  3. I also noticed that most of these sites never actually made any of their
    UIView
    a class of
    FBSDKLoginButton
    . Could this be the reason why they have to handle everything 'manually'?



Any assistance will be greatly appreciated.
Cheers!

Answer

The way that I was doing is correct but I wasn't able to retrieve any user data with it. With FBSDKLoginManager object, Facebook handled everything for me and it would simply return the user's final response (agreed or not) but without any actual data I requested through my readPermissions array. For any data retrieval; and possibly even writes, I had to create a FBSDKGraphRequest object for it. In summary, I ended up letting Facebook handle the log in and log out with FBSDKLoginManager and upon success, I retrieved user data with FBSDKGraphRequest as shown below

func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!)
{
    if error != nil
    {
       print("Facebook Log In Failed")
       print(error.localizedDescription)
    }

    let graphRequest: FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields" : "\(FacebookPermission.NAME), \(FacebookPermission.FIRST_NAME), \(FacebookPermission.LAST_NAME), \(FacebookPermission.EMAIL)"])

    graphRequest.startWithCompletionHandler { (connection: FBSDKGraphRequestConnection!, result:AnyObject!, error: NSError!) in

        if error == nil
        {
            print("Data retrieval was successful")
        }
        else
        {
            print("Facebook Graph Request Error")
            print(error)
        }
    }
}
Comments