M
M
mechanicnikodimich2016-04-12 14:06:36
Facebook
mechanicnikodimich, 2016-04-12 14:06:36

Doesn't get to see the client's token when logging in via facebook?

Good day, gentlemen.
There was a problem of the following nature:
I want to add a custom button to get a Facebook user token.
Here is the AppDelegate:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        NSURLProtocol.registerClass(TrainStarURLProtocol)

        return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
    }
    
    func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
        return GIDSignIn.sharedInstance().handleURL(url,
                                                    sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as! String,
                                                    annotation: options[UIApplicationOpenURLOptionsAnnotationKey])
        
    }
    
    func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
        var flag: Bool = false
        // handle Facebook url scheme
        if let wasHandled:Bool = FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
            {
            flag = wasHandled }
        if let googlePlusFlag: Bool = GIDSignIn.sharedInstance().handleURL(url, sourceApplication: sourceApplication!, annotation: annotation) {
            flag = googlePlusFlag
        }
        return flag
    }

The button is placed on the View, here is the ViewController's code:
@IBOutlet weak var facebookbutton: UIButton!

class LoginViewController: UIViewController,GIDSignInUIDelegate,GIDSignInDelegate {
   override func viewDidLoad() {
facebookbutton.addTarget(self, action: #selector(LoginViewController.loginFacebookAction(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        super.viewDidLoad()
}

func loginFacebookAction(sender: FBSDKLoginButton!) {
        let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
  self.getFBUserData()
        fbLoginManager.logInWithReadPermissions(["email"], fromViewController: self) { (result, error) -> Void in
              self.getFBUserData() 
              print("Done")}

    }
    
    func getFBUserData(){
        if((FBSDKAccessToken.currentAccessToken()) != nil){
            FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, email, access_token"]).startWithCompletionHandler({ (connection, result, error) -> Void in
                if (error == nil){
                    //everything works print the user data
                   // let json = try! NSJSONSerialization.JSONObjectWithData (result! as! NSData, options: .MutableContainers) as! NSDictionary
                   let token = FBSDKAccessToken.currentAccessToken()
                    print(token)
                }
                else {
                    print(error.description)
                }
            })
        }
    }

}

When the application starts, the button is active, it turns out to log in. When confirming that the application is available, a blank page opens.
When you click on the Done button in Safari, print is triggered, but since apparently
FBSDKAccessToken.currentAccessToken()) == nil
nothing else happens.
Help correct the natural curvature of the hands, what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question