Created
October 17, 2014 21:44
-
-
Save willrichman/563f97c396232fbf38b4 to your computer and use it in GitHub Desktop.
Tweet a photo!
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
//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