Essentially what I want is for the app, once the user has allowed access to their Twitter account, to be able to tweet whatever the user has selected in a UITableView<
The accepted answer is no longer valid due to several changes. This one works with iOS 10, Swift 3, and version 1.1 of Twitter's API.
** UPDATE **
This answer has been updated as the previous one relied upon a deprecated Twitter endpoint.
import Social
import Accounts
func postToTwitter() {
let accountStore = ACAccountStore()
let accountType = accountStore.accountType(withAccountTypeIdentifier: ACAccountTypeIdentifierTwitter)
accountStore.requestAccessToAccounts(with: accountType, options: nil) { (granted, error) in
if granted, let accounts = accountStore.accounts(with: accountType) {
// This will default to the first account if they have more than one
if let account = accounts.first as? ACAccount {
let requestURL = URL(string: "https://api.twitter.com/1.1/statuses/update.json")
let parameters = ["status" : "Tweet tweet"]
guard let request = SLRequest(forServiceType: SLServiceTypeTwitter, requestMethod: .POST, url: requestURL, parameters: parameters) else { return }
request.account = account
request.perform(handler: { (data, response, error) in
// Check to see if tweet was successful
})
} else {
// User does not have an available Twitter account
}
}
}
}
This is the API that is being used.