skip to Main Content

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.

2

Answers


  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")            
            }
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search