Before the release of iOS 6.0, the sharing of content was quite tiresome. There were numerous third party libraries that provided the content sharing feature but they weren’t that effective and efficient.
Fortunately, Apple then added a class UIActivityViewController that made the sharing of content to any service very simple. You just have to tell the class what kind of content you have and the class will do that work for you.
The most interesting thing about UIActivityViewController is that it automatically detects the apps that user has installed in his device. If they have Twitter, then they can post tweets with the content; if they have Facebook, then they can post content to their Facebook timeline; if they have configured a printer with their device, then they can print documents, images and more. You just have to tell the type of content you have and the class will do the rest of the work.
Below is an example to share an image:if let image = UIImage(named: "myImage") { let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: [image], applicationActivities: nil) self.present(activityViewController, animated: true, completion: nil) }
You can also remove the apps that you don’t want to be shown in the controller and don’t want to share the content with.
Here is an example to share text as well as image with the excluded apps:
let shareText = "Hello, world!" if let image = UIImage(named: "myImage") { let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: [shareText, image], applicationActivities: nil) let excludedTypes: [UIActivityType] = [.postToVimeo, .postToTwitter, .assignToContact, .saveToCameraRoll, .markupAsPDF] activityViewController.excludedActivityTypes = excludedTypes self.present(activityViewController, animated: true, completion: nil) }