From 098d9c3ea6ad9fdaa0600d15b9a42b58bccdbdc7 Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Sat, 20 Jan 2018 18:42:55 -0800 Subject: [PATCH] Add a little error handling for invalid auth When changing my api keys, realized I couldn't reauth. Now on a bad auth, we clear the keychain so that we can reauth the next time --- FitKit/FitbitClient.swift | 1 + FitKit/ViewController.swift | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/FitKit/FitbitClient.swift b/FitKit/FitbitClient.swift index abfd64a..e088e9c 100644 --- a/FitKit/FitbitClient.swift +++ b/FitKit/FitbitClient.swift @@ -168,6 +168,7 @@ class FitbitClient { Alamofire.request(urlString) + .validate() .validate(contentType: ["application/json"]) .responseJSON(completionHandler: handler) } diff --git a/FitKit/ViewController.swift b/FitKit/ViewController.swift index 67d166f..ebcd5fa 100644 --- a/FitKit/ViewController.swift +++ b/FitKit/ViewController.swift @@ -11,7 +11,7 @@ import HealthKit import UIKit class ViewController: UIViewController { - + @IBOutlet var loggedIn: UILabel! @IBOutlet var outputText: UITextView! let client = FitbitClient() @@ -88,8 +88,18 @@ class ViewController: UIViewController { response in FitbitClient.weightResponseHandler(response: response) { fbWeights, error in if let error = error { - NSLog("Unable to get weights: \(error)") + NSLog("Unable to get weights: \(error.localizedDescription)") + if let error = error as? AFError { + if let suggestion = error.recoverySuggestion { + NSLog("Suggestion to resolve: \(suggestion)") + } + if error.responseCode == 401 { + NSLog("Unauthorized. Clearing credentials") + self.client.clearCredential() + } + } self.outputText.text = "Error getting weights" + return }