How can I log in to Facebook using Swift 2.0 and iOS 9.1?

I added the latest Facebook SDK to the XCode 7.1 project written for iOS 9.1 . Unfortunately, all I know is Swift , not Target C. The documentation for the site developer contains only the documentation for Goal C. Each search on Google shows documentation that is too old because everything has changed the same way as in the last 6 months. So what I lost.

I did all the easy things with the plist file.

I was able to use:

 import FBSDKCoreKit import FBSDKLoginKit 

I also added:

 return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation) 

and

 return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions) 

In my AppDelegate.swift file. All this works and is being built successfully. Obviously, I have the right framework. Other than that, I stopped because I don’t know the syntax for adding the login button, to capture what I suppose, json strings with tokens and other profile data that I can use to store in the user account, etc. will be returned. d ..

+5
source share
2 answers

You are on the right track.

Have you installed your pList?

You need to add your VC, add the login button, discuss with the delegate, and then process the FB information. Something like (but not quite):

 class yourVC: UIViewController, FBSDKLoginButtonDelegate, UITextFieldDelegate { var loginView : FBSDKLoginButton = FBSDKLoginButton() viewDidLoad() { loginView.frame = CGRectMake(20, 20, theWidth-40, 40) self.view.addSubview(loginView) loginView.readPermissions = ["public_profile", "email", "user_friends","user_birthday"] loginView.delegate = self } func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) { if ((error) != nil) { //handle error } else { returnUserData() } } func returnUserData() { let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"id,interested_in,gender,birthday,email,age_range,name,picture.width(480).height(480)"]) graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in if ((error) != nil) { // Process error print("Error: \(error)") } else { print("fetched user: \(result)") let id : NSString = result.valueForKey("id") as! String print("User ID is: \(id)") //etc... } }) } 

You will need to play returnData() with the returnData() part so that everything is correct, but this code should get you 90% of how it is.

I have included a wide range of permissions (user age, interest, etc.), but you will have to configure them yourself. Honestly, this part (the difficult part) is really similar to it with the C object, which is much better documented. Just download some of the projects in which he works, and try to parse them together into something that matches your imagination.

It may be hard to get all this to work, but let it go and go on!

+8
source

If you want to log in to Facebook using the "User" button, this will help:

 @IBAction func onClickFacebookButton(sender: UIButton){ let login = FBSDKLoginManager() login.loginBehavior = FBSDKLoginBehavior.SystemAccount login.logInWithReadPermissions(["public_profile", "email"], fromViewController: self, handler: {(result, error) in if error != nil { print("Error : \(error.description)") } else if result.isCancelled { } else { FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "first_name, last_name, picture.type(large), email, name, id, gender"]).startWithCompletionHandler({(connection, result, error) -> Void in if error != nil{ print("Error : \(error.description)") }else{ print("userInfo is \(result))") } }) } }) } 
+5
source

Source: https://habr.com/ru/post/1235028/


All Articles