Aasim Khan Aasim Khan - 4 years ago 85
iOS Question

Sharing text on Facebook programatically IOS is not possible now

I cannot able to share text on Facebook programatically in swift.
See my code please.
Here is it,

let facebookPostAction = UIAlertAction(title: "Share on Facebook", style: UIAlertActionStyle.Default)

{ (action) -> Void in

if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook) {
let facebookComposeVC = SLComposeViewController(forServiceType: SLServiceTypeFacebook)

facebookComposeVC.setInitialText("\(self.noteTextview.text)")

self.presentViewController(facebookComposeVC, animated: true, completion: nil)
}
else {
self.showAlertMessage("You are not connected to your Facebook account.")
}

}


This function at the end says to me on run


facebook share is invalidated


What should I do with this?

Answer Source

Facebook has changed policy of sharing, so you need to use their SDK for sharing. Now developers need to install FBSDKShareKit.framework, and simple add button with context information.

https://developers.facebook.com/docs/sharing/ios

Simple says, you need to add FBSDKShareButton, by the way it extends from UIButton, and set context data to it:

_fbButton = FBSDKShareButton()
...
// setup visual part
// don't need to add target, facebook's sdk decides action
...
// set context data to shareContent button's property
let fbContent = FBSDKShareLinkContent()
fbContent.contentURL = value.contentURL
fbContent.contentTitle = value.contentTitle
fbContent.contentDescription = value.contentDescription
fbContent.imageURL = value.imageURL
_fbButton.shareContent = fbContent

That's all, facebook's share panel will show with included data after touch up inside this button.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download