Share using activity view controller in swift


This tutorial has been updated for X code 7.3 & swift 2.2

Sharing on social media can really ramp up downloads for your app. But you can also share with text, email, what’s app & more. If you just want to share on social media then see my tutorial here. In this tutorial we explore how to share via all options available in iPhone.

  1. First lets set up the x code project as usual. Create new single view application X code project. Set the values as follows and save it on your desktop.

new x code project


2. Go to main storyboard and select view controller. Set its size iPhone 4-inch in attribute section.

iPhone 4 inch


3. Add button to main storyboard & rename it to Share

add share button


4. Connect button to view controller swift file

share button as action


5. Add following code in share button

@IBAction func shareButtonPressed(sender: UIButton) {
    let shareText = "Checkout my latest app #coolapp"
    let activityViewController = UIActivityViewController(activityItems: [shareText], applicationActivities: nil)
    presentViewController(activityViewController, animated: true, completion: nil)

Now build and run app in iPhone 5s. Click on share button. You can see more sharing options when you use this app in real device.

iOS Simulator Screen Shot 10-Mar-2015 3.52.39 pm
Download X code file
Challenge 1: share url & image along with text in activity view controller

Challenge 2: Exclude add to reading list & assign to contact from activity view controller

iOS Simulator Screen Shot 10-Mar-2015 3.53.18 pm
Download Challenge Solution

Total 1 Votes

Tell us how can I improve this tutorial?

+ = Verify Human or Spambot ?