user3255746
user3255746

Reputation: 1437

Facebook SDK in Swift

I am trying to integrate Facebook login and more into my app but am having a lot of difficulty because there is not much documentation in Swift. I am trying to use this code:

FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
                initWithGraphPath:@"/me"
            parameters:@{ @"fields": @"id,name,friends,birthday,gender,first_name,last_name,friendlists,picture",}
            HTTPMethod:@"GET"];
            [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
            // Insert your code here
            }];

Any idea what this would be in Swift?

Upvotes: 0

Views: 169

Answers (2)

jose920405
jose920405

Reputation: 8049

//MARK: - Functions Fb Logic
    func signInWithFacebook()
    {
        if (FBSDKAccessToken.currentAccessToken() != nil) {
            // User is already logged in, do work such as go to next view controller.
            print("already logged in ")
            returnUserData()
            return
        }

        let faceBookLoginManger = FBSDKLoginManager()
        faceBookLoginManger.logInWithReadPermissions(["public_profile", "email"], fromViewController:nil, handler: { (result, error)-> Void in
            //result is FBSDKLoginManagerLoginResult
            if (error != nil) {
                print("error is \(error)")
            } else if (result.isCancelled) {
                //handle cancelations
            } else if result.grantedPermissions.contains("email") {
                self.returnUserData()
            }
        })
    }

    func returnUserData() {

        if((FBSDKAccessToken.currentAccessToken()) != nil){
            FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
                if ((error) != nil) {
                    // Process error
                    print("Error: \(error)")
                } else {
                    let accessToken = FBSDKAccessToken.currentAccessToken().tokenString
                    print("the access token is \(accessToken)")

                    let userID = result.valueForKey("id") as! NSString
                    let facebookProfileUrl = "http://graph.facebook.com/\(userID)/picture?type=large"

                    print("facebookProfileUrl: \(facebookProfileUrl)")
                    print("fetched user: \(result)")
                }
            })
        }
    }

Upvotes: 0

Shubhank
Shubhank

Reputation: 21805

 FBSDKGraphRequest(graphPath: "me",
   parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email, gender"])
    .startWithCompletionHandler({ (connection, result, error) -> Void in

 })

Upvotes: 1

Related Questions