Skip to content

Instantly share code, notes, and snippets.

@kaneshin
Created November 15, 2014 11:11
Show Gist options
  • Save kaneshin/e7983be618864ad347de to your computer and use it in GitHub Desktop.
Save kaneshin/e7983be618864ad347de to your computer and use it in GitHub Desktop.
extension SLComposeViewController {
convenience init(forServiceType type: String, initialText text: String) {
self.init(forServiceType: type)
self.setInitialText(text)
}
class func shareViewController(forServiceType type: String) -> SLComposeViewController {
let text = LocalizedString("Share body")
let viewController = SLComposeViewController(forServiceType: type, initialText: text)
viewController.completionHandler = { (result) in
switch result {
case .Cancelled:
return
case .Done:
return
}
}
return viewController
}
}
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
if UIApplication.sharedApplication().canOpenURL(NSURL(string: "line://")!) {
alertController.addAction(UIAlertAction(title: "LINE", style: .Default, handler: { (action) -> Void in
let text = LocalizedString("Share body")
let encodedText = text.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
let url = NSURL(string: "line://msg/text/\(encodedText)")
UIApplication.sharedApplication().openURL(url!)
return
}))
}
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook) {
alertController.addAction(UIAlertAction(title: "Facebook", style: .Default, handler: { (action) -> Void in
let viewController = SLComposeViewController.shareViewController(forServiceType: SLServiceTypeFacebook)
self.presentViewController(viewController, animated: true, completion: nil)
return
}))
}
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter) {
alertController.addAction(UIAlertAction(title: "Twitter", style: .Default, handler: { (action) -> Void in
let viewController = SLComposeViewController.shareViewController(forServiceType: SLServiceTypeTwitter)
self.presentViewController(viewController, animated: true, completion: nil)
return
}))
}
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeSinaWeibo) {
alertController.addAction(UIAlertAction(title: "Sina Weibo", style: .Default, handler: { (action) -> Void in
let viewController = SLComposeViewController.shareViewController(forServiceType: SLServiceTypeSinaWeibo)
self.presentViewController(viewController, animated: true, completion: nil)
return
}))
}
if MFMailComposeViewController.canSendMail() {
alertController.addAction(UIAlertAction(title: "Email", style: .Default, handler: { (action) -> Void in
var viewController = MFMailComposeViewController()
viewController.mailComposeDelegate = self
viewController.setSubject(LocalizedString("Share subject"))
viewController.setMessageBody(LocalizedString("Share body"), isHTML: false)
self.presentViewController(viewController, animated: true, completion: nil)
return
}))
}
alertController.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment