Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you initialize and present a custom SLComposeServiceViewController?

Tags:

ios

I can't seem to understand how to get a custom share view controller fired up. My understanding is that the SLComposeServiceViewController class provides the developer with a barebones compose view, much like the already-provided service types Facebook and Twitter, and the developer has to implement his or her own features. If that's the case, shouldn't I just init the SLComposeServiceViewController like any other VC and present it?

like image 782
Stephen Paul Avatar asked Oct 07 '14 00:10

Stephen Paul


1 Answers

From my understanding, it seems that the class is meant to be subclassed for functionality.

You could make a very simple subclass if you want to make it dynamic

enum ComposeServiceResult {
    case Cancel
    case Post
}
class ComposeServiceViewController: SLComposeServiceViewController {
    var completionHandler : ((result:ComposeServiceResult, text:String) -> Void)!

    override func didSelectCancel() {
        completionHandler(result: .Cancel, text: self.contentText)
        self.navigationController?.dismissViewControllerAnimated(true, completion: nil)
    }

    override func didSelectPost() {
        completionHandler(result: .Post, text: "")
        self.navigationController?.dismissViewControllerAnimated(true, completion: nil)
    }
}

and then present it

var composeVC = ComposeServiceViewController()
composeVC.title = "Facebook"
composeVC.placeholder = "Add a caption!"
composeVC.completionHandler = completionHandler
composeVC.modalPresentationStyle = .OverCurrentContext;
presentViewController(composeVC, animated: true, completion: nil)

I also think you can dig into SLComposeSheetConfigurationItem for better customization but I have not done that myself.

like image 162
Gustavo Barcena Avatar answered Oct 21 '22 04:10

Gustavo Barcena