0

Hi so basically I want to share to Facebook using their Facebook API but how do I add the link that if they click they can go back to the app or they will go to the app store if they don't have the app installed?

Edit ^

So I want to clear up my question. So for example in the app I have a product page that displays a product. And then I share that product to facebook and when you post it in facebook (I already know how to add the image and text) I want it to display the app and when you click the app it goes straight TO that product page IF the app is installed. If not then it will go to the app store to download that product.

Jov
  • 31
  • 1
  • 9
  • Hi Jov, I have implemented these functionality before one year. Please refer to the link and let me know https://stackoverflow.com/questions/42083226/how-to-implement-ios-app-link-facebook-functionality-in-swift-3 – Sailendra Jul 09 '18 at 06:21

2 Answers2

1

If i understood your question correctly then following could fix your problem. It would check if Facebook is installed in device , if FB is installed then it would open Facebook otherwise it would open iTunes link to download the app. This is swift 4. Also you need to add URL scheme for FB in info.plist

    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>fb</string>
    </array>

        var url = URL(string: "fb://")!

        if (UIApplication.shared.canOpenURL(url)) {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
            print("Opened FB App")
        }
        else{
            url = URL(string: "https://itunes.apple.com/in/app/facebook/id284882215?mt=8")!
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
                print("Opened FB in iTunes")            
        }
Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81
SThakur
  • 262
  • 4
  • 16
  • The back button will be visible on the top left corner with your app name. You can go back to your app. – SThakur Jul 09 '18 at 06:26
  • Hi @user160611990 not the fb app but the app that i'm making as a link when you share to facebook and then go to the app store to install the app that I made if it's not installed. – Jov Jul 09 '18 at 06:32
  • @user16011990 it's a deeplink feature. What u have posted, it's totally different – Sailendra Jul 09 '18 at 06:43
1

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.

Rihab D
  • 11
  • 1