Social Framework iOS 8 swift

2019-04-22 19:10发布

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)

2条回答
闹够了就滚
2楼-- · 2019-04-22 19:48

Try the following code, not yet tested

 socialVC.completionHandler = {
    (result:SLComposeViewControllerResult) in
        // Your code
    }
查看更多
闹够了就滚
3楼-- · 2019-04-22 20:04
    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)
    }
}
查看更多
登录 后发表回答