Search code examples
xcodeswiftfacebooksharefacebook-share

Swift share on facebook


I am having trouble sharing a link on facebook from my iOS app. I was using the below code and it was working a few months ago. The only setup I used to do was "import Social"

@IBAction func facebookShareButtonTouched(sender: AnyObject) {
    if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook) {
        let fbShare:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)

        fbShare.addURL(NSURL(fileURLWithPath: "https://www.xxx.ro/info/article/\(newsObject.id!)"))
        self.presentViewController(fbShare, animated: true, completion: nil)


    } else {
        let 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)
    }
}

Now it opens the facebook share dialog, but it does not contain any link. Anybody knows what it changed?


Solution

  • Replace this line

    fbShare.addURL(NSURL(fileURLWithPath: "https://www.xxx.ro/info/article/\(newsObject.id!)"))
    

    with this

    fbShare.addURL(NSURL("https://www.xxx.ro/info/article/\(newsObject.id!)"))
    

    Answered considering you want share URL - "https://www.xxx.ro/info/article/xid"