Facebook Login using custom button in Swift Language
Xcode Version 8
Article is Updated with the Facebook 4.16 SDK [27-September-2016].
Install Pods
1 2 3 4 5 6 7 8 |
platform :ios, '9.0' target 'FBSwiftLogin' do use_frameworks! # Pods for FBSwiftLogin pod 'FacebookCore' pod 'FacebookLogin' pod 'FacebookShare' end |
Appdelegate.swift
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import UIKit import FBSDKLoginKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions) } func applicationWillResignActive(_ application: UIApplication) { FBSDKAppEvents.activateApp() } func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool { return FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation) } } |
ViewController.swift
Create touch up inside event for custom button like as follows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
// // ViewController.swift // FBSwiftLogin // // Created by Ashish Kakkad on 05/10/16. // Copyright © 2016 Kode. All rights reserved. // import UIKit import FBSDKLoginKit class ViewController: UIViewController { var dict : [String : AnyObject]! override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } @IBAction func btnFBLoginPressed(_ sender: AnyObject) { let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager() fbLoginManager.logIn(withReadPermissions: ["email"], from: self) { (result, error) in if (error == nil){ let fbloginresult : FBSDKLoginManagerLoginResult = result! if fbloginresult.grantedPermissions != nil { if(fbloginresult.grantedPermissions.contains("email")) { self.getFBUserData() fbLoginManager.logOut() } } } } } func getFBUserData(){ if((FBSDKAccessToken.current()) != nil){ FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in if (error == nil){ self.dict = result as! [String : AnyObject] print(result!) print(self.dict) } }) } } } |
In iOS 10 don’t forget to set capabilities (Keychain Sharing) :
Go to your project targets -> Capabilities -> Keychain Sharing -> Toggle Switch ON
Detailed Output Log :
1 2 3 4 5 6 7 8 9 10 11 12 13 |
{ email = "ashishkakkad8@gmail.com"; "first_name" = Ashish; id = 1227390383984537; "last_name" = Kakkad; name = "Ashish Kakkad"; picture = { data = { "is_silhouette" = 0; url = "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xpf1/v/t1.0-1/p200x200/14192077_1193042490752660_7942165287960590731_n.jpg?oh=bf3bedc4f98190a8f588fc8c3f911328&oe=58ABC3CC&__gda__=1486868896_cfe22abf7b2d6559f3609d8c51fc0663"; }; }; } |
Happy Coding 😀