Guest User

Untitled

a guest
May 21st, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. import UIKit
  2.  
  3. class testViewController: UIViewController, FBSDKLoginButtonDelegate {
  4.  
  5. override func viewDidLoad() {
  6. super.viewDidLoad()
  7. // Do any additional setup after loading the view, typically from a nib.
  8.  
  9. if (FBSDKAccessToken.currentAccessToken() != nil)
  10. {
  11. // User is already logged in, do work such as go to next view controller.
  12.  
  13. // Or Show Logout Button
  14. let loginView : FBSDKLoginButton = FBSDKLoginButton()
  15. self.view.addSubview(loginView)
  16. loginView.center = self.view.center
  17. loginView.readPermissions = ["public_profile", "email", "user_friends"]
  18. loginView.delegate = self
  19. self.returnUserData()
  20. }
  21. else
  22. {
  23. let loginView : FBSDKLoginButton = FBSDKLoginButton()
  24. self.view.addSubview(loginView)
  25. loginView.center = self.view.center
  26. loginView.readPermissions = ["public_profile", "email", "user_friends"]
  27. loginView.delegate = self
  28. }
  29.  
  30. }
  31.  
  32. // Facebook Delegate Methods
  33.  
  34. func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
  35. println("User Logged In")
  36.  
  37. if ((error) != nil)
  38. {
  39. // Process error
  40. }
  41. else if result.isCancelled {
  42. // Handle cancellations
  43. }
  44. else {
  45. // If you ask for multiple permissions at once, you
  46. // should check if specific permissions missing
  47. if result.grantedPermissions.contains("email")
  48. {
  49. // Do work
  50. }
  51.  
  52. self.returnUserData()
  53. }
  54.  
  55. }
  56.  
  57. func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
  58. println("User Logged Out")
  59. }
  60.  
  61. func returnUserData() {
  62. let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
  63. graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
  64.  
  65. if ((error) != nil)
  66. {
  67. // Process error
  68. println("Error: (error)")
  69. }
  70. else
  71. {
  72. println("fetched user: (result)")
  73. let userName : NSString = result.valueForKey("name") as! NSString
  74. println("User Name is: (userName)")
  75. let userEmail : NSString = result.valueForKey("email") as! NSString
  76. println("User Email is: (userEmail)")
  77. }
  78. })
  79. }
  80.  
  81. override func didReceiveMemoryWarning() {
  82. super.didReceiveMemoryWarning()
  83. // Dispose of any resources that can be recreated.
  84. }
  85.  
  86. }
  87.  
  88. fetched user: {
  89. id = 808101565906152;
  90. name = "Marcelo Pontes Machado";
  91. }
  92.  
  93. FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"id,email,name,picture.width(480).height(480)"]).startWithCompletionHandler({
Add Comment
Please, Sign In to add comment