Social Framework iOS 8 swift

2019-04-22 19:56发布

问题:

So I've been writing an app that needs to use Social framework to share text via twitter and facebook.

I got it to work but it did not dismiss (?), then I remembered the completion handler, but whatever I do this handler keeps on giving me errors.

var okFacebook :Bool = SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook)
var okTwitter :Bool = SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter)
var okLinkedIn : Bool = SLComposeViewController.isAvailableForServiceType(SLServiceTypeLinkedIn)
var socialVC :SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTwitter)

socialVC.completionHandler = SLComposeViewControllerCompletionHandler(SLComposeViewControllerResult) -> Void
self.presentViewController(socialVC, animated: true, completion: nil)

回答1:

Try the following code, not yet tested

 socialVC.completionHandler = {
    (result:SLComposeViewControllerResult) in
        // Your code
    }


回答2:

    override func viewDidLoad() {
    super.viewDidLoad()

    if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook){
        var facebookSheet:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)


        facebookSheet.setInitialText("Hiya, I have just discovered this great app called Dwingle, which I think you're going to love.")
        let url = NSURL(string: "")
        facebookSheet.addURL(url)
        self.presentViewController(facebookSheet, animated: true, completion: nil)


        facebookSheet.completionHandler = {

            result -> Void in
            self.dismissViewControllerAnimated(true, completion: { () -> Void in
                self.navigationController?.popToRootViewControllerAnimated(true)
            })
        }


    } else {
        var alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
        self.presentViewController(alert, animated: true, completion: nil)
    }
}