Roberto C Dl Garza Roberto C Dl Garza - 1 month ago 7
iOS Question

don't recognize a facebook user already logged in

on my viewDidLoad I have this

var currentUser = PFUser.currentUser()
if currentUser != nil || FBSDKAccessToken.currentAccessToken() != nil {
performSegueWithIdentifier("principal1", sender: self)

println("parse user or facebook is loged in...")
} else {

// Show the signup or login screen
}


it works fine when I login with parse but not when I login with facebook

my code for a login custom button is (and is outside of any override Function ):

@IBAction func fbBtn(sender: AnyObject) {
var fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager .logInWithReadPermissions(["email"], handler: { (result, error) -> Void in
if (error == nil){
var fbloginresult : FBSDKLoginManagerLoginResult = result
if(fbloginresult.grantedPermissions.contains("email"))
{
self.getFBUserData()
fbLoginManager.logOut()
}

}
})
}

func getFBUserData(){
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){
println(result)
}
})
}
performSegueWithIdentifier("principal1", sender: self)

}


it seams that is not saving the accesstoken for facebook so the segue on the top is not working

Answer

In your appdelegate :

import UIKit

import FBSDKCoreKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
    }
    func applicationWillResignActive(application: UIApplication) {    
        FBSDKAppEvents.activateApp()
    }
    func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
        return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
    }
}

In your view controller :

import UIKit
import FBSDKLoginKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @IBAction func btnFBLoginPressed(sender: AnyObject) {
        var fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
        fbLoginManager .logInWithReadPermissions(["email"], handler: { (result, error) -> Void in
            if (error == nil){
                var fbloginresult : FBSDKLoginManagerLoginResult = result
                if(fbloginresult.grantedPermissions.contains("email"))
                {
                    self.getFBUserData()
                    fbLoginManager.logOut()
                }
            }
        })
    }

    func getFBUserData(){
        if((FBSDKAccessToken.currentAccessToken()) != nil){
            FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
                if (error == nil){
                    println(result) // Do your stuff here
                }
            })
        }
    }
}

Add properties in your plist.info file.

enter image description here

Add URL Types, FacebookAppID, and FacebookDisplayName Properties

For more you can go through the steps of https://developers.facebook.com/docs/ios/getting-started