Created
March 1, 2016 03:53
-
-
Save makthrow/9115fb38d11a219144e0 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
// | |
// ViewController.swift | |
// | |
// Created by Alan Jaw on 2/29/16. | |
// Copyright © 2016 Alan Jaw. All rights reserved. | |
// | |
import UIKit | |
import FBSDKLoginKit | |
import Firebase | |
let ref = Firebase(url: "https://xxxxxxxxxxxxx.firebaseio.com/") | |
class ViewController: UIViewController { | |
@IBOutlet weak var facebookLoginButton: UIButton! | |
@IBOutlet weak var facebookLogoutButton: UIButton! | |
let facebookLoginManager = FBSDKLoginManager() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
hideShowLoginLogoutButtons() | |
if (FBSDKAccessToken.currentAccessToken() != nil) | |
{ | |
} | |
else { | |
} | |
} | |
override func viewWillAppear(animated: Bool) { | |
super.viewWillAppear(animated) | |
hideShowLoginLogoutButtons() | |
} | |
@IBAction func facebookLogoutButtonPressed(sender: UIButton) { | |
facebookLoginManager.logOut() | |
hideShowLoginLogoutButtons() | |
} | |
func hideShowLoginLogoutButtons() { | |
if (FBSDKAccessToken.currentAccessToken() != nil) { | |
// user logged in to facebook already | |
facebookLoginButton.hidden = true | |
facebookLogoutButton.hidden = false | |
print ("already FBlogged in") | |
} | |
else { | |
// user not logged in | |
facebookLoginButton.hidden = false | |
facebookLogoutButton.hidden = true | |
print ("FB logged out") | |
} | |
} | |
@IBAction func facebookLoginButtonPressed(sender: UIButton) { | |
let facebookPermissionsBasic = ["public_profile", "email", "user_friends"] | |
let facebookPermissionsExtensive = ["public_profile", "email", "user_friends", "user_education_history", "user_birthday"] | |
facebookLoginManager.logInWithReadPermissions(facebookPermissionsExtensive, fromViewController: self) { (facebookResult, facebookError) -> Void in | |
if facebookError != nil { | |
print("Facebook login failed. Error \(facebookError)") | |
} | |
else if facebookResult.isCancelled { | |
print("Facebook login was cancelled.") | |
} | |
else { | |
let accessToken = FBSDKAccessToken.currentAccessToken().tokenString | |
ref.authWithOAuthProvider("facebook", token: accessToken, | |
withCompletionBlock: { error, authData in | |
if error != nil { | |
print("Login failed. \(error)") | |
} else { | |
print("Logged in! \(authData)") | |
self.getFBUserInfo() | |
} | |
}) | |
} | |
} | |
} | |
func getFBUserInfo() { | |
let facebookRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "name, gender, picture, birthday, first_name, last_name"]) | |
facebookRequest.startWithCompletionHandler { (connection: FBSDKGraphRequestConnection!, result, error) -> Void in | |
if error == nil { | |
print ("result: \(result)") | |
var r = result as! NSDictionary | |
let firstName = r["first_name"] | |
let gender = r["gender"] | |
var dateFormatter = NSDateFormatter() | |
dateFormatter.dateFormat = "MM/dd/yyyy" | |
let birthday = dateFormatter.dateFromString(r["birthday"]! as! String) | |
let facebookID = r["id"]! | |
print ("facebookID: \(facebookID)") | |
let urlString = "https://graph.facebook.com/\(facebookID)/picture?type=large" | |
// get the URL of a larger picture | |
let largerPicURL = NSURL(string: urlString) | |
print (largerPicURL) | |
let request = NSURLRequest(URL: largerPicURL!) | |
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: { | |
response, data, error in | |
// TODO: save largerPic to DB | |
}) | |
} | |
else { // facebookRequest.startWithCompletionHandler | |
print (error) | |
} | |
} | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment