Last active
February 24, 2018 20:09
-
-
Save fedejordan/bb403fc281b75926d8cb988515de9188 to your computer and use it in GitHub Desktop.
Blog03 - Retweet with tweetId
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import TwitterKit | |
class RetweetViewController: UIViewController { | |
@IBOutlet private weak var tweetIdTextField: UITextField! | |
let client = TWTRAPIClient().withCurrentUser() | |
@IBAction private func didSelectRetweet(sender: UIButton) { | |
guard let tweetId = tweetIdTextField.text else { return } | |
client.loadTweet(withID: tweetId) { (tweet, error) -> Void in | |
if let text = tweet?.text { | |
self.showRetweetAlert(withText: text) | |
} else { | |
self.showSimpleAlert(withText: "Data not found") | |
} | |
} | |
} | |
private func showSimpleAlert(withText text: String) { | |
let alert = UIAlertController(title: "SimpleRTApp", message: text, preferredStyle: .alert) | |
let okAction = UIAlertAction(title: "OK", style: .cancel, handler: nil) | |
alert.addAction(okAction) | |
present(alert, animated: true, completion: nil) | |
} | |
private func showRetweetAlert(withText text: String) { | |
let alert = UIAlertController(title: "Tweet example", message: text, preferredStyle: .alert) | |
let okAction = UIAlertAction(title: "OK", style: .cancel, handler: nil) | |
let retweetAction = UIAlertAction(title: "Retweet", style: .default) { (action) in | |
self.retweet() | |
} | |
alert.addAction(okAction) | |
alert.addAction(retweetAction) | |
present(alert, animated: true, completion: nil) | |
} | |
private func retweet() { | |
guard let tweetId = tweetIdTextField.text else { return } | |
let statusesShowEndpoint = "https://api.twitter.com/1.1/statuses/retweet/" + tweetId + ".json" | |
let params = ["id": tweetId] | |
var clientError : NSError? | |
let request = client.urlRequest(withMethod: "POST", urlString: statusesShowEndpoint, parameters: params, error: &clientError) | |
client.sendTwitterRequest(request) { (response, data, connectionError) -> Void in | |
if let connectionError = connectionError { | |
print("Error: \(connectionError)") | |
self.showSimpleAlert(withText: "Retweet error") | |
} else { | |
self.showSimpleAlert(withText: "Retweet done!") | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment