Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class AuthorizationBaseViewController: UIViewController {
- func delay(delay: Double, closure: ()->()) {
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(delay*Double(NSEC_PER_SEC))), dispatch_get_main_queue(), closure)
- }
- func showMessage(message: String) {
- let alert = UIAlertController(title: nil, message: message, preferredStyle: UIAlertControllerStyle.Alert)
- let okayAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)
- alert.addAction(okayAction)
- self.presentViewController(alert, animated: true, completion: nil)
- }
- func checkError(response: Response) {
- // Unauthorized
- if response.statusCode == 401 {
- TokenManager.deleteToken()
- dispatch_async(dispatch_get_main_queue(), {
- self.reset()
- })
- }
- }
- func reset() {
- }
- // Mark: LoginButtonDelegate
- func loginButton(button: LoginButton, didLogoutWithSuccess success: Bool) {
- if success {
- showMessage(NSLocalizedString("Integration with uber canceled.", comment: ""))
- }
- }
- func loginButton(button: LoginButton, didCompleteLoginWithToken accessToken: AccessToken?, error: NSError?) {
- if let _ = accessToken {
- print(accessToken?.tokenString)
- print(accessToken?.expirationDate)
- print(accessToken?.refreshToken)
- showMessage(NSLocalizedString("Uber user authenticated successfully.", comment: ""))
- if let url = NSURL(string: "xxxxxxxxxxxxxx") {
- let request = NSMutableURLRequest(URL: url)
- request.HTTPMethod = "POST"
- request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
- let token = AccessToken?()
- let jsonObject = ["token" : (token?.tokenString)!, "refresh_token" : (token?.refreshToken)!,"expires_in" : (token?.expirationDate)!, "user_id" : "uber_uuid" , "token_type" : "Bearer"] as Dictionary <String,AnyObject>
- request.HTTPBody = try? NSJSONSerialization.dataWithJSONObject(jsonObject, options: [])
- NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
- guard
- let data = data where
- error == nil &&
- (response as? NSHTTPURLResponse)?.statusCode == 200
- else {
- print((response as? NSHTTPURLResponse)?.statusCode ?? "no status code")
- print(error?.localizedDescription ?? "no error description")
- return
- }
- print(String(data: data, encoding: NSUTF8StringEncoding) ?? "no string from data")
- }.resume()
- }
- showMessage((error?.localizedDescription)!)
- } else if let error = error {
- showMessage(error.localizedDescription)
- } else {
- showMessage("Error")
- }
- }
- }
- class ImplicitGrantLoginViewController: AuthorizationBaseViewController, LoginButtonDelegate {
- /// The LoginManager to use for login
- let loginManager = LoginManager(loginType: .Native)
- /// The RidesClient to use for endpoints
- let ridesClient = RidesClient()
- // The Uber button to use for UI
- var uberLoginButton: LoginButton?
- // The Uber Scopes
- var uberScopes: [RidesScope]?
- @IBOutlet weak var logoutBgView: UIView!
- override func viewDidLoad() {
- super.viewDidLoad()
- //UberScopes to get authentication
- uberScopes = [.History, .Profile, .HistoryLite,.Places, .RideWidgets]
- uberLoginButton = LoginButton(frame: CGRectZero,scopes:uberScopes! ,loginManager: loginManager)
- // Uber Login Button Creation
- let loginButton = LoginButton(frame: CGRectZero, scopes: uberScopes!, loginManager: loginManager)
- loginButton.presentingViewController = self
- loginButton.delegate = self
- loginButton.frame = logoutBgView.bounds
- loginButton.autoresizingMask =
- [.FlexibleWidth, .FlexibleHeight]
- logoutBgView.addSubview(loginButton)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement