Skip to content

Instantly share code, notes, and snippets.

@stuartbreckenridge
Last active May 25, 2016 15:10
Show Gist options
  • Save stuartbreckenridge/300b2ba61d48f9a31454ab4131de562e to your computer and use it in GitHub Desktop.
Save stuartbreckenridge/300b2ba61d48f9a31454ab4131de562e to your computer and use it in GitHub Desktop.
SLComposable Protocol—Easy to use protocol for creation and presentation of SLComposeViewControllers
//
// SLComposable.swift
// SocialFrameworkRef
//
// Created by Stuart Breckenridge on 24/05/2016.
// Copyright © 2016 Stuart Breckenridge. All rights reserved.
//
import Foundation
import Social
/**
Social Networks available on iOS. The `rawValue` for the cases in this enum are identical to the `SLServiceType` constants.
- Facebook: Facebook.
- Twitter: Twitter.
- SinaWeibo: Sina Weibo.
- TencentWeibo: Tencent Weibo.
*/
public enum SLComposableServiceType:String, CustomStringConvertible
{
case Facebook = "com.apple.social.facebook"
case Twitter = "com.apple.social.twitter"
case SinaWeibo = "com.apple.social.sinaweibo"
case TencentWeibo = "com.apple.social.tencentweibo"
public var description: String {
switch self {
case .Twitter:
return "Twitter"
case .Facebook:
return "Facebook"
case .SinaWeibo:
return "Sina Weibo"
case .TencentWeibo:
return "Tencent Weibo"
}
}
}
/**
* The SLComposable protocol defines one method that allows for easy creation and presentation of `SLComposeViewController`s for all iOS supported Social Networks.
*/
public protocol SLComposable {
/**
This method will check that the user can post to the specified social network, and if they can it will present an `SLComposeViewController` populated with text, image, and URL if provided. If posting is not available, a UIAlertController is presented.
- parameter text: The initial text of the post. Optional.
- parameter image: An image to append. Optional.
- parameter url: An URL to append. Optional.
- parameter network: The Social Network to use. See `SLComposableServiceType`.
- seealso: `SLComposableServiceType`
*/
func composePost(text text:String?, image:UIImage?, url:NSURL?, network: SLComposableServiceType)
}
/**
* `UIViewController` implementation of the `SLComposable` protocol.
*/
extension UIViewController:SLComposable
{
public func composePost(text text:String?, image:UIImage?, url:NSURL?, network: SLComposableServiceType)
{
// If posting to the specified social network is not available, present a UIAlertController.
guard SLComposeViewController.isAvailableForServiceType(network.rawValue) == true else
{
let alert = UIAlertController(title: "Error", message: "Posting to \(network.description) is not available. Have you added an account in Settings?", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Dismiss", style: .Default, handler: nil))
presentViewController(alert, animated: true, completion: nil)
return
}
let socialController = SLComposeViewController(forServiceType: network.rawValue)
socialController.completionHandler = {(result:SLComposeViewControllerResult) in
switch result {
case .Done:
print("SLComposeViewController: Done")
case .Cancelled:
print("SLComposeViewController: Cancelled")
}
}
if text != nil{
socialController.setInitialText(text!)
}
if image != nil {
socialController.addImage(image!)
}
if url != nil {
socialController.addURL(url!)
}
// Present the SLComposeViewController.
presentViewController(socialController, animated: true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment