Created
March 18, 2016 12:56
-
-
Save Bashta/eb6482b5d8db62c1fecc to your computer and use it in GitHub Desktop.
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
// | |
// FacebookSocialMediaManager.swift | |
// | |
// | |
// Created by Erison Veshi<Bashta> on 2/25/16. | |
// | |
import FBSDKCoreKit | |
import FBSDKLoginKit | |
import Parse | |
final class FacebookSocialMediaManager { | |
static let sharedInstance = FacebookSocialMediaManager() | |
} | |
extension FacebookSocialMediaManager { | |
func logInWithFacebook(controler: UIViewController, completion:(success:Bool, error: NSError?) -> ()) { | |
let loginManager = FBSDKLoginManager() | |
loginManager.logInWithReadPermissions(["public_profile", "email"], fromViewController: controler) { results, error in | |
if let error = error { | |
completion(success: false, error: error) | |
} | |
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "name, gender,email"]).startWithCompletionHandler({ connection, result, error in | |
guard let resultDictionary = result as? Dictionary<String, AnyObject>, | |
let name = resultDictionary["name"] as? String, | |
let email = resultDictionary["email"] as? String else { | |
completion(success: false, error: error) | |
return | |
} | |
PFUser.logInWithUsernameInBackground(email, password: email, block: { user, error in | |
if let error = error { | |
//invalid login parameters | |
if error.code == 101 { | |
//Register and signup a new user | |
let newUser = PFUser() | |
newUser.username = email | |
newUser.password = email | |
newUser.signUpInBackgroundWithBlock({ success, error in | |
newUser.setValue(true, forKey: GlobalConstants.ParseKeys.isFacebookAutenticated) | |
newUser.setObject(name, forKey: GlobalConstants.ParseKeys.UserNameKey) | |
newUser.saveInBackground() | |
completion(success: success, error: error) | |
}) | |
} | |
} | |
completion(success: true, error: error) | |
}) | |
}) | |
} | |
} | |
func logOutFromFacebook() { | |
let loginManager = FBSDKLoginManager() | |
loginManager.logOut() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment