Last active
March 19, 2016 20:18
-
-
Save reterVision/091200424e122ef14c8c to your computer and use it in GitHub Desktop.
Facebook Login with SDK4.0 and Swift
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
import UIKit | |
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
var window: UIWindow? | |
func application(application: UIApplication, | |
didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { | |
return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions) | |
} | |
func application(application: UIApplication, | |
openURL url: NSURL, | |
sourceApplication: String?, | |
annotation: AnyObject?) -> Bool { | |
return FBSDKApplicationDelegate.sharedInstance().application( | |
application, | |
openURL: url, | |
sourceApplication: sourceApplication, | |
annotation: annotation) | |
} | |
func applicationWillResignActive(application: UIApplication) { | |
} | |
func applicationDidEnterBackground(application: UIApplication) { | |
} | |
func applicationWillEnterForeground(application: UIApplication) { | |
} | |
func applicationDidBecomeActive(application: UIApplication) { | |
FBSDKAppEvents.activateApp() | |
} | |
func applicationWillTerminate(application: UIApplication) { | |
} | |
} |
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
import UIKit | |
class ViewController: UIViewController, FBSDKLoginButtonDelegate { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
if (FBSDKAccessToken.currentAccessToken() != nil) { | |
// User is already logged in, do work such as go to next view controller. | |
} else { | |
let loginView : FBSDKLoginButton = FBSDKLoginButton() | |
self.view.addSubview(loginView) | |
loginView.center = self.view.center | |
loginView.readPermissions = ["public_profile", "email", "user_friends"] | |
loginView.delegate = self | |
} | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
// Facebook Delegate Methods | |
func loginButton(loginButton: FBSDKLoginButton!, | |
didCompleteWithResult result: FBSDKLoginManagerLoginResult!, | |
error: NSError!) { | |
println("User Logged In") | |
if ((error) != nil) | |
{ | |
// Process error | |
} else if result.isCancelled { | |
// Handle cancellations | |
} else { | |
// If you ask for multiple permissions at once, you | |
// should check if specific permissions missing | |
if result.grantedPermissions.contains("email") | |
{ | |
// Do work | |
} | |
} | |
} | |
func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) { | |
println("User Logged Out") | |
} | |
func returnUserData() | |
{ | |
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil) | |
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in | |
if ((error) != nil) | |
{ | |
// Process error | |
println("Error: \(error)") | |
} | |
else | |
{ | |
println("fetched user: \(result)") | |
let userName : NSString = result.valueForKey("name") as! NSString | |
println("User Name is: \(userName)") | |
let userEmail : NSString = result.valueForKey("email") as! NSString | |
println("User Email is: \(userEmail)") | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did you get the name of the user when you run it ? I didn't get it ! I could only view the user's image