Skip to content

Instantly share code, notes, and snippets.

@randhirraj3130
Last active April 5, 2017 06:08
Show Gist options
  • Save randhirraj3130/49398f8c225905cf18756ee694eec573 to your computer and use it in GitHub Desktop.
Save randhirraj3130/49398f8c225905cf18756ee694eec573 to your computer and use it in GitHub Desktop.
facebook login in swift
// facebook login in swift 3.0
// the following code into info.plist
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fb1801381186770425</string>
</array>
</dict>
</array>
<key>FacebookAppID</key>
<string>AddYourID</string>
<key>FacebookDisplayName</key>
<string>YourAppName</string>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fb-messenger-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
<string>instagram</string>
</array>
<key>NSLocationWhenInUseUsageDescription</key>
<string>To access your current location</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>To share </string>
// add the following code in appdelegate.swift
import FBSDKCoreKit
import FBSDKLoginKit
import FBSDKCoreKit
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
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)
}
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(app, open: url, options: options)
return false
}
// add the function where you want
import FBSDKLoginKit
func FbLoginButton() {
cheak = "facebook"
UserDefaults.standard.set(cheak, forKey: "cheak")
print("fb login workin")
if (FBSDKAccessToken.current() != nil)
{
// User is logged in, do work such as go to next view controller.
print("already login")
self.getFBUserData()
}else{
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"))
{
print("fb result data::\(result)")
self.getFBUserData()
//fbLoginManager.logOut()
}
}
}else{
self.InvalidAlert()
}
}
}
}
func getFBUserData()
{
print("getFBUserData already login")
if((FBSDKAccessToken.current()) != nil)
{
print("getFBUserData ")
FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in
if (error == nil)
{
print("\(result)")
var name = ""
var email = ""
var id = ""
if let dic = result as? NSDictionary{
if let email1 = dic.value(forKey: "email") as? String{
print("email is ::\(email)")
email = email1
}
if let name1 = dic.value(forKey: "name") as? String{
print("name is::\(name)")
name = name1
}
if let id1 = dic.value(forKey: "id") as? String{
print("id is::\(id)")
id = id1
}
if let picture = dic.value(forKey: "picture") as? NSDictionary{
if let data = picture.value(forKey: "data") as? NSDictionary{
if let url = data.value(forKey: "url") as? String{
print("url is \(url)")
self.sendFbData(id: id, name: name, email: email, user_image: url)
}
}
}
}
}
})
}else{
print("fb already login")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment