From 0ce8bc41b4a703faea28f101fe6e0aa0ef9ce70c Mon Sep 17 00:00:00 2001 From: Yoshiyuki Kawashima Date: Fri, 23 Jun 2017 23:12:20 +0900 Subject: [PATCH] Stored OAuthToken and OAuthTokenSecret Once authenticate! refs #13 --- yomblr/ViewController.swift | 95 ++++++++++++++++++++++++------------- 1 file changed, 61 insertions(+), 34 deletions(-) diff --git a/yomblr/ViewController.swift b/yomblr/ViewController.swift index b630f55..bd3a870 100644 --- a/yomblr/ViewController.swift +++ b/yomblr/ViewController.swift @@ -26,31 +26,73 @@ class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() + guard let oAuthToken = UserDefaults.standard.string(forKey: "OAuthToken"), + let oAuthTokenSecret = UserDefaults.standard.string(forKey: "OAuthTokenSecret") else { + authenticate() + return + } + + TMAPIClient.sharedInstance().oAuthToken = oAuthToken + TMAPIClient.sharedInstance().oAuthTokenSecret = oAuthTokenSecret + showDashboard() + } + + override func didReceiveMemoryWarning() { + super.didReceiveMemoryWarning() + // Dispose of any resources that can be recreated. + } + + // MARK: - Navigation + + override func prepare(for segue: UIStoryboardSegue, sender: Any?) { + if let cropViewController: CropViewController = segue.destination as? CropViewController { + cropViewController.blogName = blogName + cropViewController.image = photo.image + cropViewController.postUrl = postUrl + } + } + + // MARK: - Tumblr + + func authenticate() { TMAPIClient.sharedInstance().authenticate("yomblr", from: self, callback: { error in if (error != nil) { print("\(String(describing: error?.localizedDescription))") return } - - TMAPIClient.sharedInstance().userInfo({ result, error in - if (error != nil) { - print("\(String(describing: error?.localizedDescription))") - return - } - - guard let dictionary = result as? [String: Any] else { - print("result was not JSON.") - return - } - - guard let user = dictionary["user"] as? [String: Any] else { - print("result was not found \"user\".") + + guard let oAuthToken = TMAPIClient.sharedInstance().oAuthToken, + let oAuthTokenSecret = TMAPIClient.sharedInstance().oAuthTokenSecret else { + print("can't retrieve OAuthToken or OAuthTokenSecret") return - } - - self.blogName = user["name"] as? String - }) - + } + + UserDefaults.standard.set(oAuthToken, forKey: "OAuthToken") + UserDefaults.standard.set(oAuthTokenSecret, forKey: "OAuthTokenSecret") + + self.showDashboard() + }) + } + + func showDashboard() { + TMAPIClient.sharedInstance().userInfo({ result, error in + if (error != nil) { + print("\(String(describing: error?.localizedDescription))") + return + } + + guard let dictionary = result as? [String: Any] else { + print("result was not JSON.") + return + } + + guard let user = dictionary["user"] as? [String: Any] else { + print("result was not found \"user\".") + return + } + + self.blogName = user["name"] as? String + TMAPIClient.sharedInstance().dashboard(["type": "photo", "limit": 1], callback: { result, error in if (error != nil) { print("\(String(describing: error?.localizedDescription))") @@ -103,19 +145,4 @@ class ViewController: UIViewController { }) }) } - - override func didReceiveMemoryWarning() { - super.didReceiveMemoryWarning() - // Dispose of any resources that can be recreated. - } - - // MARK: - Navigation - - override func prepare(for segue: UIStoryboardSegue, sender: Any?) { - if let cropViewController: CropViewController = segue.destination as? CropViewController { - cropViewController.blogName = blogName - cropViewController.image = photo.image - cropViewController.postUrl = postUrl - } - } }