iOS sharing with UIActivityViewController in Swift

December 12, 2014
Shrikar Archak

<Image alt="iOS Swift Tutorial : UICollectionView Pinterest Layout" objectFit="contain" src="/static/images/share2.png" height={350} width={1000} placeholder="blur" quality={100} />

iOS Sharing with UIActivityViewController in Swift

Do you want to make your app viral? If so one of the key ingredient is adding ios sharing functionality in your app. You can share to Social Media, or traditional means of message and mail.

iOS 7 and iOS 8 provide UIActivityViewController for making the social sharing a lot easier. With UIActivityViewController we can add ios sharing capabilities with a few lines of code.

UIActivityViewController
- init(activityItems activityItems: [AnyObject],
applicationActivities applicationActivities: [AnyObject]?)

activityItems are the data object on which to perform the activity. Activity Items can be simple type like String, image , url or custom object which conform to UIActivityItemSource protocol.

If you look at the documentation for UIActivityItemSource protocol there are two function which are required.

func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> AnyObject
func activityViewController(_ activityViewController: UIActivityViewController,
   itemForActivityType activityType: String) -> AnyObject?

Lets take a look at a example . We have a custom News reading application where the news appear as a tinder style flash cards and we want to share that card.

<Image alt="iOS Swift Tutorial : UICollectionView Pinterest Layout" objectFit="contain" src="/static/images/share2.png" height={350} width={1000} placeholder="blur" quality={100} />


//
//  FlashCard.swift
//  FlashCard
//
//  Created by Shrikar Archak on 12/1/14.
//  Copyright (c) 2014 Shrikar Archak. All rights reserved.
//

import UIKit

class FlashCard: NSObject, UIActivityItemSource {
    var news: String = ""
    var url : String = ""

    func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject {
        NSLog("Place holder")
        return news;
    }

    func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
        NSLog("Place holder itemForActivity")
        if(activityType == UIActivityTypeMail){
            return news
        } else if(activityType == UIActivityTypePostToTwitter){
            return news + " via @iOSgetstarted " + url
        } else {
            return news + " url"
        }
    }

    func activityViewController(activityViewController: UIActivityViewController, subjectForActivityType activityType: String?) -> String {
        NSLog("Place holder subjectForActivity")
        if(activityType == UIActivityTypeMail){
            return "Hey check this out!!"
        } else if(activityType == UIActivityTypePostToTwitter){
            return news + " via @iOSgetstarted " + url
        } else {
            return news + " via @iOSgetstarted " + url
        }
    }
}

And this is how you would present the UIActivityViewController

func share(card : FlashCard) {
    let vc = UIActivityViewController(activityItems: [card, NSURL(string: card.url)!], applicationActivities: nil)
    /* If you want to exclude certain types from sharing
    options you could add them to the excludedActivityTypes */
//        vc.excludedActivityTypes = [UIActivityTypeMail]
    self.presentViewController(vc, animated: true, completion: nil)
}

<Image alt="iOS Swift Tutorial : UICollectionView Pinterest Layout" objectFit="contain" src="/static/images/share1.png" height={350} width={1000} placeholder="blur" quality={100} />

<iframe src="//www.youtube.com/embed/abGn8-vFlTQ" width="420" height="315" frameborder="0" ></iframe>

Let me know if you have any question / feedback.

Subscribe to the newsletter

Get notified when new content or topic is released.

You won't receive any spam! ✌️