Skip to content

Instantly share code, notes, and snippets.

@soggybag
Last active August 10, 2016 22:11
Show Gist options
  • Save soggybag/c83ed5cc91702b5e4c9c to your computer and use it in GitHub Desktop.
Save soggybag/c83ed5cc91702b5e4c9c to your computer and use it in GitHub Desktop.
Swift, post to FaceBook and Twitter
import UIKit
import Social
import MessageUI
class ViewController: UIViewController, MFMessageComposeViewControllerDelegate {
// MARK: Social
func postToFaceBook() {
let vc = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
vc.setInitialText("Posting to FaceBook")
// vc.addImage(UIImage!) // Add an image
// vc.addURL(NSURL!) // Add a URL
presentViewController(vc, animated: true, completion: nil)
}
func postToTwitter() {
let vc = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
vc.setInitialText("Posting to Twitter")
// vc.addImage(UIImage!) // Add an image
// vc.addURL(NSURL!) // Add a URL
presentViewController(vc, animated: true, completion: nil)
}
func sendMessage() {
let messageVC = MFMessageComposeViewController()
messageVC.body = "Your message string"
messageVC.recipients = []
messageVC.messageComposeDelegate = self
presentViewController(messageVC, animated: true, completion: nil)
}
func messageComposeViewController(_ controller: MFMessageComposeViewController,
didFinishWithResult result: MessageComposeResult) {
// Do something after sending a message...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment