justdan0227 justdan0227 - 2 months ago 182
Swift Question

Facebook FBSDKLoginManager/logInWithReadPermissions Swift Example not using Parse

Where is the latest Facebook Swift Documentation. I can't get the FB Login Dialog to show up. The call to loginWithReadPermissions never returns?

import UIKit
import FBSDKCoreKit
import FBSDKLoginKit


class ViewController: UIViewController {override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

let loginManager = FBSDKLoginManager()
loginManager.logInWithReadPermissions(["basic_info", "email", "user_likes"], fromViewController: self.parentViewController, handler: { (result, error) -> Void in
if error != nil {
print(FBSDKAccessToken.currentAccessToken())
} else if result.isCancelled {
print("Cancelled")
} else {
print("LoggedIn")
}
})
}

Answer

So the answer is to use FBSDKLoginButton with the following

In the class declaration of the viewController

import FBSDKCoreKit
import FBSDKLoginKit
class ViewController: UIViewController, FBSDKLoginButtonDelegate

Then show the FBLogin Button with

        // Setup the FB Login/Logout Button  FB will take care of the
        // verbiage based on the current access token

        let loginView : FBSDKLoginButton = FBSDKLoginButton()
        self.view.addSubview(loginView)
        loginView.center = self.view.center
        loginView.readPermissions = ["public_profile", "email", "user_friends"]
        loginView.delegate = self

        // If we have an access token, then let's display some info

        if (FBSDKAccessToken.currentAccessToken() != nil)
        {
            // Display current FB premissions
            print (FBSDKAccessToken.currentAccessToken().permissions)

            // Since we already logged in we can display the user datea and taggable friend data.
            self.showUserData()
            self.showFriendData()
        }

Show the users info with

 func showUserData()
    {
        let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields" : "id, name, gender, first_name, last_name, locale, email"])
        graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in

            if ((error) != nil)
            {
                // Process error
                print("Error: \(error)")
            }
            else
            {
                let userName : NSString = result.valueForKey("name") as! NSString
                print("User Name is: \(userName)")

                if let userEmail : NSString = result.valueForKey("email") as? NSString {
                    print("User Email is: \(userEmail)")
                }
            }
        })
    }

And to display the taggable friends

func showFriendData()
    {
        let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me/taggable_friends?limit=999", parameters: ["fields" : "name"])
        graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in

            if ((error) != nil)
            {
                // Process error
                print("Error: \(error)")
            }
            else
            {
                if let friends : NSArray = result.valueForKey("data") as? NSArray{
                    var i = 1
                    for obj in friends {
                        if let name = obj["name"] as? String {
                            print("\(i) " + name)
                            i++
                        }
                    }
                }
            }
        })
    }