Skip to content

Instantly share code, notes, and snippets.

@randhirraj3130
Last active February 10, 2017 10:43
Show Gist options
  • Save randhirraj3130/5e5c6dbd94a220f232894c82902692e3 to your computer and use it in GitHub Desktop.
Save randhirraj3130/5e5c6dbd94a220f232894c82902692e3 to your computer and use it in GitHub Desktop.
Facebook Login in swift 3.0
func FbLoginButton() {
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()
}
}
}
}
}
}
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")
}
}
func sendFbData(id:String , name:String ,email:String,user_image:String){
let prs = [
"id" : id,
"login_type" : "2" as String,
"name" : name,
"mobile" : "",
"email" : email,
"user_image" : user_image,
"house_number": "",
"locality" : "",
"city" : "",
"pincode" : ""
] as [String:String]
Service.Start(VC: self,dic: prs, url: "\(GlobalVariable.serverName)user_insert.php", onCompletion: { result in
DispatchQueue.main.async(execute: {
if let dic = result as? NSDictionary{
if let city = dic.value(forKey: "city") as? String{
print("city:\(city)")
UserDefaults.standard.setValue(city, forKey: "city")
}
if let house_number = dic.value(forKey: "house_number") as? String{
print("house_number:\(house_number)")
UserDefaults.standard.setValue(house_number, forKey: "house_number")
}
if let id = dic.value(forKey: "id") as? String{
print("id\(id)")
UserDefaults.standard.setValue(id, forKey: "id")
}
if let locality = dic.value(forKey: "locality") as? String{
print("locality\(locality)")
UserDefaults.standard.setValue(locality, forKey: "locality")
}
if let login_type = dic.value(forKey: "login_type") as? String{
print("login type\(login_type)")
}
if let mobile = dic.value(forKey: "mobile") as? String{
print("mobile\(mobile)")
UserDefaults.standard.setValue(mobile, forKey: "mobile")
}
if let pincode = dic.value(forKey: "pincode") as? String{
print("pincode\(pincode)")
UserDefaults.standard.setValue(pincode, forKey: "pincode")
}
if let user_image = dic.value(forKey: "user_image") as? String{
print("user_image\(user_image)")
UserDefaults.standard.setValue(user_image, forKey: "user_image")
}
if let useremail = dic.value(forKey: "useremail") as? String{
print("useremail\(useremail)")
UserDefaults.standard.setValue(useremail, forKey: "email")
}
if let username = dic.value(forKey: "username") as? String{
print("username\(username)")
UserDefaults.standard.setValue(name, forKey: "name")
}
CurrentUser.getData()
}
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment