Skip to content

Instantly share code, notes, and snippets.

@willrichman
Created October 17, 2014 21:44
Show Gist options
  • Save willrichman/563f97c396232fbf38b4 to your computer and use it in GitHub Desktop.
Save willrichman/563f97c396232fbf38b4 to your computer and use it in GitHub Desktop.
Tweet a photo!
//MARK: - Social Methods
func tweet(message: String?, image: UIImage?, url: NSURL?) {
let accountStore = ACAccountStore()
let accountType = accountStore.accountTypeWithAccountTypeIdentifier(ACAccountTypeIdentifierTwitter)
accountStore.requestAccessToAccountsWithType(accountType, options: nil) { (granted: Bool, error: NSError!) -> Void in
if granted {
let accounts = accountStore.accountsWithAccountType(accountType)
self.twitterAccount = accounts.first as? ACAccount
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter) {
let controller = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
controller.setInitialText(message)
controller.addImage(image)
controller.addURL(url)
controller.completionHandler = { (result: SLComposeViewControllerResult) -> Void in
switch result {
case SLComposeViewControllerResult.Cancelled:
println("tweet: cancelled")
case SLComposeViewControllerResult.Done:
println("tweet: success")
}
}
self.presentViewController(controller, animated: true, completion: { () -> Void in
// Controller is presented
})
}
} else {
println("Twitter is not available")
}
}
}
@IBAction func tweetPhoto(sender: AnyObject) {
tweet(nil, image: self.imageView.image, url: nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment