First make sure to connect the IBAction to your view controller code.
URL sharing:
@IBAction func shareURL(_ sender: Any) {
let URLstring = String(format:"https://itunes.apple.com/in/app/facebook/id284882215?mt=8")
let urlToShare = URL(string:URLstring)
let title = "title to be shared"
let activityViewController = UIActivityViewController(
activityItems: [title,urlToShare!],
applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
//so that ipads won't crash
present(activityViewController,animated: true,completion: nil)
}
Text sharing:
@IBAction func shareText(_ sender: Any) {
let text = "Text to be shared"
let activityViewController = UIActivityViewController(activityItems:[text],applicationActivities:nil)
activityViewController.popoverPresentationController?.sourceView = self.view
present(activityViewController,animated: true,completion: nil)
}
Image sharing:
@IBAction func shareImage(_ sender: Any) {
let image = #imageLiteral(resourceName: "myImage")
let activityViewController = UIActivityViewController(activityItems:[image],applicationActivities:nil)
activityViewController.popoverPresentationController?.sourceView = self.view
present(activityViewController,animated: true,completion: nil)
}
When user click on Facebook button to share it will directly connect them either to App-store(for those who don't have it) or to the Facebook for sharing.