Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the login in Facebook SDK 4.6 for iOS?

I was using this code to FB login:

@IBAction func fbloginbtn(sender: AnyObject) {

    FBSDKLoginManager().logInWithReadPermissions(["public_profile", "email","user_location","user_about_me", "user_photos", "user_website"], handler: { (result:FBSDKLoginManagerLoginResult!, error:NSError!) -> Void in
        if (error == nil){
            let fbloginresult : FBSDKLoginManagerLoginResult = result
            if(fbloginresult.grantedPermissions.contains("email"))
            {
                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){
                            //do sth
                        }
                    })
                }
            } 
        }
    })
}

But loginwithreadpermissions is deprecated in SDK 4.6

How should I change this code?

like image 547
Utku Dalmaz Avatar asked Sep 15 '15 02:09

Utku Dalmaz


1 Answers

If you look at the documentation, you'll see the mention of alternative api as well. From the documentation of FBSDKLoginManager, it says:

- (void)logInWithReadPermissions:(NSArray *)permissions
                         handler:(FBSDKLoginManagerRequestTokenHandler)handler
__attribute__((deprecated("use logInWithReadPermissions: fromViewController:handler: instead")));

So there is a new method, that takes an additional parameter of UIViewController as well, to determine from where the login sequence was initiated. As documentation says:

- (void)logInWithReadPermissions:(NSArray *)permissions
              fromViewController:(UIViewController *)fromViewController
              handler:(FBSDKLoginManagerRequestTokenHandler)handler;

and the explanation for parameters says:

fromViewController - The view controller to present from. If nil, the topmost view controller will be automatically determined as best as possible.

Since, there's only one additional parameter, you could add it to existing implementation like this:

FBSDKLoginManager().logInWithReadPermissions(["public_profile", "others"],
                           fromViewController:self //<=== new addition, self is the view controller where you're calling this method.
                           handler: { (result:FBSDKLoginManagerLoginResult!, error:NSError!) -> Void in
})

The latest xcode should also suggest you when you write, logInWithReadPermissions with all available options.

like image 140
Adil Soomro Avatar answered Sep 21 '22 19:09

Adil Soomro