- Integrate facebook login in your application with only two steps.
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapods
To integrate NBFacebookManager into your Xcode project using CocoaPods, specify it in your Podfile
:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!
target '<Your Target Name>' do
pod 'NBFacebookManager'
end
Then, run the following command:
$ pod install
- Please visit Facebook Developer Site to create a new application and follow the instructions to get facebook App-ID setup in plist file of your Xcode project.
- Install the NBFacebookManager pod.
- Add following code in your
Appdelegate.swift
file,
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
}
@available(iOS 9.0, *)
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(app, open: url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String, annotation: options[UIApplicationOpenURLOptionsKey.annotation])
}
- Create the FacebookManager instance in your controller to authenticate user and get its facebook profile.
fileprivate let facebookManager = FacebookManager()
- Authenticate the user using 'facebookManager' as given below
facebookManager.tryAuthenticate(fromViewController: self, success:{ facebookToken in
self.getProfileIfNeeded()
}) { error in
print("Error: \(String(describing: error?.localizedDescription))")
}
private func getProfileIfNeeded(){
facebookManager.fetchLoggedUserInfo(successBlock: { (userInfo) in
print("\(String(describing: userInfo))")
}) { (error) in
print("Error: \(String(describing: error?.localizedDescription))")
}
}
- Congratulations! You have successfully integrated facebook login in your application.
NBFacebookManager is available under the MIT license. See the LICENSE file for more info.
Fahid Attique - (https://github.com/fahidattique55)