Share iOS texts

Asked

Viewed 132 times

0

How do I share a text on social media on an iOS App on Swift 3? I have several Random Phrases in an array and if the person likes one I would like to let them share that phrase. How would the one button code to open options between share the current text on Facebook, Whatsapp, etc...

Like this example on Android in the image below: App de frases em Android

  • I don’t have time to translate, but see: https://stackoverflow.com/a/35931947/2570426

1 answer

2


An example of how to do this with Swift 3

import UIKit
class ViewController: UIViewController {

    // share text
    @IBAction func shareTextButton(_ sender: UIButton) {

        // text to share
        let text = "This is some text that I want to share."

        // set up activity view controller
        let textToShare = [ text ]
        let activityViewController = UIActivityViewController(activityItems: textToShare, applicationActivities: nil)
        activityViewController.popoverPresentationController?.sourceView = self.view // so that iPads won't crash

        // exclude some activity types from the list (optional)
        activityViewController.excludedActivityTypes = [ UIActivityType.airDrop, UIActivityType.postToFacebook ]

        // present the view controller
        self.present(activityViewController, animated: true, completion: nil)

    }

    // share image
    @IBAction func shareImageButton(_ sender: UIButton) {

        // image to share
        let image = UIImage(named: "Image")

        // set up activity view controller
        let imageToShare = [ image! ]
        let activityViewController = UIActivityViewController(activityItems: imageToShare, applicationActivities: nil)
        activityViewController.popoverPresentationController?.sourceView = self.view // so that iPads won't crash

        // exclude some activity types from the list (optional)
        activityViewController.excludedActivityTypes = [ UIActivityType.airDrop, UIActivityType.postToFacebook ]

        // present the view controller
        self.present(activityViewController, animated: true, completion: nil)
    }

Source

  • That’s just what I needed, thank you very much! Just one more thing, as it would be to share on other networks, email, etc., in theory I must replace this "Uiactivitytype.postToFacebook", there are specific functions like the postToFacebook?

  • Exactly, the system will give you the options available on your iPhone. It’s just not having that, because it’s optional. If the answer helped you, could dial with solved.

  • add Uiactivitytype to all elements is redundant [ .airDrop, .postToFacebook ]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.