Govind Rakholiya
Govind Rakholiya

Reputation: 295

FBSDKAccessToken.currentAccessToken() found nil every time

I integrate facebook in my app. but after login FBSDKAccessToken.currentAccessToken() found nil .

Here is my code:

override func viewDidLoad() {

    if (FBSDKAccessToken.currentAccessToken() != nil)
    {
        print("Not loggedin...")
    }
    else
    {
        print("logged in...")
    }

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


func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {

    if(error == nil)
    {
        print("login complete")
        print(result)
        print(result.grantedPermissions)
    }
    else{
        print(error.localizedDescription)
    }

    if let userToken = result.token
    {
        let token:FBSDKAccessToken = result.token
        print("TOKEN = \(FBSDKAccessToken.currentAccessToken().tokenString)")
        print("User ID = \(FBSDKAccessToken.currentAccessToken().userID)")
    }

    let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"name, email"])

    graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in

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

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

After login, I am getting a blank screen. It prints the login successful message, but it is not going into this:

if let userToken = result.token
{
    let token:FBSDKAccessToken = result.token
    print("TOKEN = \(FBSDKAccessToken.currentAccessToken().tokenString)")
    print("User ID = \(FBSDKAccessToken.currentAccessToken().userID)")
}

So, I am not getting any token. And while its goes to print email its showing this error:

this is my code in appdelegate

 func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool
{
    return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}


func applicationDidBecomeActive(application: UIApplication) {

    FBSDKAppEvents.activateApp()
}

func application(application: UIApplication,
    openURL url: NSURL, options options: [String: AnyObject]) -> Bool {
        return GIDSignIn.sharedInstance().handleURL(url,
            sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey]! as! String,
            annotation: options[UIApplicationOpenURLOptionsAnnotationKey])
}

actually everytime its called openurl method of gmail integration so how can i put the condition to call method of facebook integration

Upvotes: 0

Views: 1635

Answers (1)

Rohit Pradhan
Rohit Pradhan

Reputation: 3875

Do following changes in your code, It will work

 func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
            println("User Logged In")

            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")
                {
                     // Do work
        self.returnUserData()

                }
            }   
        }

call this method to get user email id

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

                if ((error) != nil)
                {
                    // Process error
                    println("Error: \(error)")
                }
                else
                {
                    println("fetched user: \(result)")
                    let userName : NSString = result.valueForKey("name") as! NSString
                    println("User Name is: \(userName)")
                    let userEmail : NSString = result.valueForKey("email") as! NSString
                    println("User Email is: \(userEmail)")
                }
            })
        }

please remove this method

func application(application: UIApplication,
    openURL url: NSURL, options options: [String: AnyObject]) -> Bool {
        return GIDSignIn.sharedInstance().handleURL(url,
            sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey]! as! String,
            annotation: options[UIApplicationOpenURLOptionsAnnotationKey])
}

change this method implementation

func application(application: UIApplication,
        openURL url: NSURL,
        sourceApplication: String?,
        annotation: AnyObject?) -> Bool {
if url.scheme == "fbxxxxx"
{
//xxxxx is your APP ID
return FBSDKApplicationDelegate.sharedInstance().application(
                application,
                openURL: url,
                sourceApplication: sourceApplication,
                annotation: annotation)
}else if url.scheme == "com.googleusercontent.apps.xxxxxx"
  {
// string is reverse client ID
 return GIDSignIn.sharedInstance().handleURL(url,
                sourceApplication:  sourceApplication,
                annotation: annotation)
   }
 }

Upvotes: 3

Related Questions