iOS sharing with UIActivityViewController in Swift

Shrikar Archak / December 12, 2014

3 min read

iOS Swift Tutorial : UICollectionView Pinterest Layout

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.

iOS Swift Tutorial : UICollectionView Pinterest Layout

//
//  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)
}
iOS Swift Tutorial : UICollectionView Pinterest Layout

Let me know if you have any question / feedback.

Shrikar Archak

@shrikar84

IndieHacker and Software Developer. Getting started to build in public.Tweet me @shrikar84 for any collaboration opportunity or brainstorming.

#developer#ml-engineer#seo

Subscribe to the newsletter

Get notified when new content or topic is released.

You won't receive any spam! ✌️