WepsTech
The world of development
So let’s start with the coding part! With a explanation of integrate Facebook login for iOS.
Here you can find very simple way to integrate Facebook login for iOS on your Xcode project. So that, with the help of this you can integrate Facebook login for iOS using swift language easily. When people do Facebook login into your app, they can grant permissions to your app so you can retrieve information from their Facebook account and you can authorised them for using the application or perform actions as you want.
On different – 2 website you get different different answer of the question, How to do Facebook login for iOS using swift language. Therefore, you will get it in very easy and simple process.
Open your terminal and run below command at your project location.
$ pod init
$ pod install
Close your FB-Login.xcodeproj and open FB-Login.xcworkspace. After that open your Podfile and write the below code!
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'FB-Login' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# Pods for FB-Login
pod 'FacebookLogin'
pod 'FacebookSDK'
target 'FB-LoginTests' do
inherit! :search_paths
# Pods for testing
end
target 'FB-LoginUITests' do
# Pods for testing
end
end
Now open your AppDelegate.swift and write the below code!
import UIKit
import FacebookCore
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
ApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
}
After that, finally code for your ViewController.swift. Or it may be any file where ever you want to create your login! In addition, this file just contains the necessary code.
import UIKit
import FacebookLogin
import FacebookCore
import FBSDKLoginKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let loginButton = FBLoginButton(permissions: [ .publicProfile, .email ])
loginButton.center = view.center
view.addSubview(loginButton)
loginButton.addTarget(self, action: #selector(facebookManager), for: .touchUpInside)
if let accessTocken = AccessToken.current {
print("ACCESS_TOKEN - " + accessTocken.tokenString)
fetchUserProfile()
}
}
func fetchUserProfile()
{
let graphRequest : GraphRequest = GraphRequest(graphPath: "me", parameters: ["fields":"id, email, name, picture.width(480).height(480)"])
graphRequest.start(completionHandler: { (connection, result, error) -> Void in
if ((error) != nil)
{
print("Error took place: \(String(describing: error))")
}
else
{
print("Print entire fetched result: \(String(describing: result))")
let alertController = UIAlertController(
title: "Login Success",
message: "Login succeeded with granted permissions: \(String(describing: result!))",
preferredStyle: UIAlertController.Style.alert
)
self.present(alertController, animated: true, completion: nil)
}
})
}
@objc func facebookManager() {
let loginManager = LoginManager()
loginManager.logIn(
permissions: [.publicProfile, .userFriends, .email],
viewController: self
) { result in
self.loginManagerDidComplete(result)
}
}
func loginManagerDidComplete(_ result: LoginResult) {
let alertController: UIAlertController
switch result {
case .cancelled:
alertController = UIAlertController(
title: "Login Cancelled",
message: "User cancelled login.",
preferredStyle: UIAlertController.Style.alert
)
case .failed(let error):
alertController = UIAlertController(
title: "Login Fail",
message: "Login failed with error \(error)",
preferredStyle: UIAlertController.Style.alert
)
case .success(let grantedPermissions, _, _):
alertController = UIAlertController(
title: "Login Success",
message: "Login succeeded with granted permissions: \(grantedPermissions)",
preferredStyle: UIAlertController.Style.alert
)
}
self.present(alertController, animated: true, completion: nil)
}
}
In Conclusion, I hope no this question is clear how to do Facebook login for iOS with swift? And I am sure that, this will help you for your problem. Here you can find more tutorial which may help you for this problem.
iOS Architect
let loginManager = LoginManager() should be declared outside the function
I would not consider creating a loginManager object everytime user taps the button.
Pushpendra Kumar
Yes Great, You can take let loginManager = LoginManager() out side the function! Or you can declare this inside viewDidLoad() function. If you do this then you do not need to call it on button click event! Thanks for this valuable suggestion!
N95 Respirator
What a fantastic post! This is so chock full of useful
information I can’t wait to dig deep and start using the
resources you have given me.
King regards,
Abildgaard Cannon
ปั้มไลค์
Like!! Really appreciate you sharing this blog post.Really thank you! Keep writing.
3M 9551V
What a post! That is so chock full of useful information I can’t wait
to dig deep and get started using the resources you’ve given me.
King regards,
Abildgaard Schneider