0

I have the exact same situation from this topic PFFacebookUtils.logInInBackgroundWithReadPermissions, swift 1.2, user returned is nil

I get user nil and error nil, the author said code started working but didn't post his solution, any ideas?

(I'm new, so I don't have the reputation to post in that topic)

Community
  • 1
  • 1

1 Answers1

1

Try this:

Librarys in podfile:

pod 'FBSDKCoreKit', '~> 4.2'

pod 'FBSDKLoginKit', '~> 4.2'

pod 'FBSDKShareKit', '~> 4.2'

AppDelegate.swif class:

import UIKit

import FBSDKCoreKit

import FBSDKShareKit

import FBSDKLoginKit


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate{

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)

    }


    // Added to handle the Authorization code returned from sign-in.
    func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
        return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
    }
}

MainViewController class or other view controller:

import UIKit
import FBSDKCoreKit
import FBSDKShareKit
import FBSDKLoginKit

class MainViewController: UIViewController, FBSDKLoginButtonDelegate{

    override func viewDidLoad() {
        super.viewDidLoad()

        var fbLoginButton : FBSDKLoginButton = FBSDKLoginButton()
        fbLoginButton.readPermissions = ["public_profile", "email", "user_friends"]
        fbLoginButton.delegate = self
        self.view.addSubview(fbLoginButton)
        fbLoginButton.center = self.view.center
    }

    override func viewDidAppear(animated: Bool) {
        if (FBSDKAccessToken.currentAccessToken() != nil){
            self.returnUserData()
        }
    }

    func goToTabController(){
        Util.showViewController(viewControllerSource: self, viewControllerToShow: "TabBarController")
    }

    //////////////////////////////////////
    //     Facebook Methods Delegate    ///
    //////////////////////////////////////


    func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
        if ((error) != nil){
            // Process error
        }else if result.isCancelled {
            // Handle cancellations
        }else {
            // If you ask for multiple permissions at once, you should check if specific permissions missing
            if result.grantedPermissions.contains("email"){
                self.returnUserData()
            }
        }
    }

    func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
        //println("User Logged Out")
    }

    func returnUserData(){
        FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
            if (error == nil){
                println(result.valueForKey("email"))
                println(result.valueForKey("name"))                
                self.goToTabController()
            }
        })
    }    

    //////////////////////////////////////
    //      Facebook Methods Delegate  ///
    //////////////////////////////////////    
}

I hope it helps you

Landaida
  • 134
  • 2
  • 8