Skip to content

Commit

Permalink
Stored OAuthToken and OAuthTokenSecret
Browse files Browse the repository at this point in the history
Once authenticate!

refs #13
  • Loading branch information
ykws committed Jun 23, 2017
1 parent bb8bfa9 commit 0ce8bc4
Showing 1 changed file with 61 additions and 34 deletions.
95 changes: 61 additions & 34 deletions yomblr/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))")
Expand Down Expand Up @@ -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
}
}
}

1 comment on commit 0ce8bc4

@ykws
Copy link
Owner Author

@ykws ykws commented on 0ce8bc4 Jun 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.