Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. class AuthorizationBaseViewController: UIViewController {
  2.  
  3. func delay(delay: Double, closure: ()->()) {
  4. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(delay*Double(NSEC_PER_SEC))), dispatch_get_main_queue(), closure)
  5. }
  6.  
  7. func showMessage(message: String) {
  8. let alert = UIAlertController(title: nil, message: message, preferredStyle: UIAlertControllerStyle.Alert)
  9. let okayAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)
  10. alert.addAction(okayAction)
  11. self.presentViewController(alert, animated: true, completion: nil)
  12. }
  13.  
  14. func checkError(response: Response) {
  15. // Unauthorized
  16. if response.statusCode == 401 {
  17. TokenManager.deleteToken()
  18. dispatch_async(dispatch_get_main_queue(), {
  19. self.reset()
  20. })
  21. }
  22. }
  23.  
  24. func reset() {
  25.  
  26. }
  27.  
  28. // Mark: LoginButtonDelegate
  29.  
  30. func loginButton(button: LoginButton, didLogoutWithSuccess success: Bool) {
  31. if success {
  32. showMessage(NSLocalizedString("Integration with uber canceled.", comment: ""))
  33. }
  34. }
  35.  
  36. func loginButton(button: LoginButton, didCompleteLoginWithToken accessToken: AccessToken?, error: NSError?) {
  37. if let _ = accessToken {
  38.  
  39.  
  40. print(accessToken?.tokenString)
  41. print(accessToken?.expirationDate)
  42. print(accessToken?.refreshToken)
  43.  
  44. showMessage(NSLocalizedString("Uber user authenticated successfully.", comment: ""))
  45. if let url = NSURL(string: "xxxxxxxxxxxxxx") {
  46. let request = NSMutableURLRequest(URL: url)
  47. request.HTTPMethod = "POST"
  48. request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
  49. let token = AccessToken?()
  50. let jsonObject = ["token" : (token?.tokenString)!, "refresh_token" : (token?.refreshToken)!,"expires_in" : (token?.expirationDate)!, "user_id" : "uber_uuid" , "token_type" : "Bearer"] as Dictionary <String,AnyObject>
  51.  
  52. request.HTTPBody = try? NSJSONSerialization.dataWithJSONObject(jsonObject, options: [])
  53.  
  54. NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
  55. guard
  56. let data = data where
  57. error == nil &&
  58. (response as? NSHTTPURLResponse)?.statusCode == 200
  59. else {
  60. print((response as? NSHTTPURLResponse)?.statusCode ?? "no status code")
  61. print(error?.localizedDescription ?? "no error description")
  62. return
  63. }
  64. print(String(data: data, encoding: NSUTF8StringEncoding) ?? "no string from data")
  65. }.resume()
  66. }
  67. showMessage((error?.localizedDescription)!)
  68.  
  69.  
  70.  
  71. } else if let error = error {
  72. showMessage(error.localizedDescription)
  73. } else {
  74. showMessage("Error")
  75. }
  76. }
  77. }
  78.  
  79. class ImplicitGrantLoginViewController: AuthorizationBaseViewController, LoginButtonDelegate {
  80.  
  81.  
  82. /// The LoginManager to use for login
  83. let loginManager = LoginManager(loginType: .Native)
  84.  
  85. /// The RidesClient to use for endpoints
  86. let ridesClient = RidesClient()
  87.  
  88. // The Uber button to use for UI
  89.  
  90. var uberLoginButton: LoginButton?
  91.  
  92. // The Uber Scopes
  93.  
  94. var uberScopes: [RidesScope]?
  95.  
  96. @IBOutlet weak var logoutBgView: UIView!
  97.  
  98.  
  99. override func viewDidLoad() {
  100. super.viewDidLoad()
  101.  
  102. //UberScopes to get authentication
  103.  
  104. uberScopes = [.History, .Profile, .HistoryLite,.Places, .RideWidgets]
  105.  
  106. uberLoginButton = LoginButton(frame: CGRectZero,scopes:uberScopes! ,loginManager: loginManager)
  107.  
  108.  
  109. // Uber Login Button Creation
  110.  
  111. let loginButton = LoginButton(frame: CGRectZero, scopes: uberScopes!, loginManager: loginManager)
  112. loginButton.presentingViewController = self
  113. loginButton.delegate = self
  114. loginButton.frame = logoutBgView.bounds
  115. loginButton.autoresizingMask =
  116. [.FlexibleWidth, .FlexibleHeight]
  117. logoutBgView.addSubview(loginButton)
  118.  
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement