How to use Facebook login SDK 4.6 for iOS?

I used this code to enter FB:

@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 deprecated in SDK 4.6

How do I change this code?

+5
source share
1 answer

If you look at the documentation, you will also see a mention of an alternative api. The FBSDKLoginManager documentation says:

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

So, there is a new method that also takes an optional UIViewController parameter to determine where the logical sequence started from. As the documentation says:

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

and an explanation of the parameters says:

fromViewController - the view controller for the view. If nil, the topmost controller will be automatically detected as well as possible.

Since there is only one additional parameter, you can add it to an existing implementation as follows:

 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 offer you, when you write, logInWithReadPermissions with all the options available.

+13
source

All Articles