Add Sharing to your iOS App

Just a quick post this time, for those of you looking to add sharing into your App.

Apple have done a great drop adding sharing into their OS. It couldn’t be easier to add it in to your app with just three lines of code. I’m using Swift here.

let objectsToShare = ["some text you wish to share","http://somewebli.nk"]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
self.presentViewController(activityVC, animated: true, completion: nil)

This is of course just the simplest form to get a basic share menu working, providing links to any apps that can share the content you have chosen (Messages, Mail, WhatsApp, Facebook, Twitter, etc). The menu automatically displays the installed Apps that are compatible with this feature, unless you specifically choose to prevent some of them. In this case I’ve added two objects (strings, in fact) which will be passed on to whichever app you choose from the share menu.

And that’s it, a fairly major addition to anyone’s app with just three lines of code. Great job Apple, if only it was all this easy.

More details on this controller can be found in Apple’s documentation –¬†Apple Documentation – UIActivityViewController