Created
March 30, 2015 01:21
-
-
Save benaneesh/e6c66caea03bac20bdc4 to your computer and use it in GitHub Desktop.
IOS FaceBook login with Parse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK: - Login Button Pressed | |
@IBAction func loginButtonPressed(sender: AnyObject) { | |
MBProgressHUD.showHUDAddedTo(self.view, animated: true) | |
let permissions = ["user_about_me", "public_profile", "email", "user_relationships", "user_birthday", "user_location"] | |
PFFacebookUtils.logInWithPermissions(permissions, { (user: PFUser!, error: NSError!) -> Void in | |
if user == nil { | |
if (error == nil) { | |
println("User cancelled FB login") | |
}else{ | |
println("FB login error: \(error)") | |
} | |
} else if user.isNew { | |
NSLog("User signed up and logged in through Facebook!") | |
self.getUserDetails(user) | |
} else { | |
NSLog("User logged in through Facebook!") | |
self.getUserDetails(user) | |
} | |
MBProgressHUD.hideHUDForView(self.view, animated: true) | |
}) | |
} | |
func getUserDetails(user: PFUser) { | |
MBProgressHUD.showHUDAddedTo(self.view, animated: true) | |
var fbRequest = FBRequest.requestForMe() | |
fbRequest.startWithCompletionHandler { (connection: FBRequestConnection!, result:AnyObject!, error: NSError!) -> Void in | |
if error == nil { | |
var userData = result as NSDictionary | |
if let fbId = userData.objectForKey("id") as? String { | |
user["fbId"] = fbId | |
} | |
var f_name = "" | |
var l_name = "" | |
if let fname = userData.objectForKey("first_name") as? String { | |
f_name = fname | |
} | |
if let lname = userData.objectForKey("last_name") as? String { | |
l_name = lname | |
} | |
user["first_name"] = f_name | |
user["last_name"] = l_name | |
user["full_name"] = "\(f_name) \(l_name)" | |
if let email = userData.objectForKey("email") as? String { | |
user["email"] = email | |
} | |
if let gender = userData.objectForKey("gender") as? String { | |
user["gender"] = gender | |
} | |
if let verified = userData.objectForKey("verified") as? Bool { | |
user["fb_verified"] = verified | |
} | |
if let location: AnyObject = userData.objectForKey("location")? { | |
if let locationName = location.objectForKey("name") as? String { | |
user["location"] = locationName | |
} | |
} | |
// birthday | |
if let birthday = userData.objectForKey("birthday") as? String { | |
// if !birthday.isEmpty { | |
var dateFormatter = NSDateFormatter() | |
dateFormatter.dateFormat = "MM/dd/yyyy" | |
var birthdayDate = dateFormatter.dateFromString(birthday) | |
user["birthday"] = birthdayDate! | |
user["age"] = self.getAge(birthdayDate!) | |
// } | |
} | |
user.saveInBackgroundWithBlock({ (success: Bool, error: NSError!) -> Void in | |
if error == nil { | |
// continue to phone view if its empty | |
if (user["phone"] == nil) || ((user["phone"] as String).isEmpty) { | |
let phoneView: PhoneNumberViewController = PhoneNumberViewController() | |
self.navigationController?.pushViewController(phoneView, animated: true) | |
} else { | |
self.navigationController?.dismissViewControllerAnimated(true, completion: { () -> Void in | |
// | |
}) | |
} | |
} else { | |
// show error | |
} | |
MBProgressHUD.hideHUDForView(self.view, animated: true) | |
}) | |
} else { | |
println("Error: \(error.userInfo)") | |
MBProgressHUD.hideHUDForView(self.view, animated: true) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment