How to login with Facebook in Swift
Posted By : Manu Gupta | 30-Jun-2015
In this blog I will tell you how to login with facebook in Swift which is very important nowadays for quick signup as users don't have time to fill the forms.
Facebook is providing is SDK which is written in Obj-C and we will implement this in our swift project using the Bridging Header file. Below will be the step by step guide to implement the login .
- Download the FACEBOOK SDK and plaese check the requirements mentioned on the link.
- Go to this link Create a new iOS App and fill the necessary details of your app.
- Now go to your Xcode project manually drag the "FacebookSDKCoreKit.Framework" to your project.
- Now Create the Bridging-Header.h file and import the following files into that header.
#ifndef Project_Bridging_Header_h #define Project_Bridging_Header_h #import
#endif’ - Now add the following method into your app delegate file so that on facebook login click the app should redirect to your facebook App.
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool { return FBSDKApplicationDelegate.sharedInstance().application( application, openURL: url, sourceApplication: sourceApplication, annotation: annotation) }
- Go to your info.plist file and do the following shown in the image
- Add this into your viewControllers viewDidLoad() method
if (FBSDKAccessToken.currentAccessToken() != nil){ // TODO IF THE USER IS ALREADY LOGIN }else{ let facebookLoginButton: FBSDKLoginButton = FBSDKLoginButton() self.view.addSubview(facebookLoginButton) facebookLoginButton.center = self.view.center facebookLoginButton.readPermissions = ["public_profile", "email"] facebookLoginButton.delegate = self }
- Extend the "FBSDKLoginButtonDelegate"
class ViewController: UIViewController,FBSDKLoginButtonDelegate {
- Finally, add the delegate methods of FBSDKLoginButtonDelegate
// Facebook Delegate Methods func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) { if ((error) != nil){ println("Error: \(error)") }else if result.isCancelled { // Handle cancellations }else { ActivityIndicator.shared.showProgressView(self.view) self.returnUserData() } } // Facebook logout button func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) { println("User Logged Out") } // Getting the user data from FaceBook func returnUserData(){ let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil) graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in if (error) != nil { println("Error: \(error)") }else{ self.storingFacebookData(result) } }) }
- Now you are ready to use login with facebook functionality in your project .
Thanks
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Manu Gupta
Manu is a Native iOS (Swift) and Titanium lead developer . Manu has good experience working with Swift and Alloy framework as well. He loves watching movies and travelling.