UIActivityViewController
class is define under UIKit
framework and inherits from UIViewController
. This class makes sharing feature super easy. Using UIActivityViewController
class we can post content to social media, copy content to pasteboard, send content via SMS and email, and more.
Share text message
Sharing text is one of the basic feature ofUIActivityViewController
let shareContent = ["Write your share content"]
let activityController = UIActivityViewController(activityItems: shareContent,
applicationActivities: nil)
self.present(activityController, animated: true, completion: nil)
Share image
You can easily share the image using below code// Get the image which you want to share
guard let image = UIImage(named: "myImage.png") else { return }
let shareContent = [image]
let activityController = UIActivityViewController(activityItems: shareContent,
applicationActivities: nil)
self.present(activityController, animated: true, completion: nil)
Share URLs
Share your urlsguard let url = URL(string: "http://swiftdevcenter.com/") else { return }
let shareContent = [url]
let activityController = UIActivityViewController(activityItems: shareContent,
applicationActivities: nil)
self.present(activityController, animated: true, completion: nil)
Share text and URLs
We can share both text and URLslet shareText = "This is text message to share"
guard let url = URL(string: "http://swiftdevcenter.com/") else { return }
let shareContent: [Any] = [shareText, url]
let activityController = UIActivityViewController(activityItems: shareContent,
applicationActivities: nil)
self.present(activityController, animated: true, completion: nil)
Share text, image and URLs
We cans share text, image and URLs all three togetherlet shareText = "This is text message to share"
guard let url = URL(string: "http://swiftdevcenter.com/"),
let image = UIImage(named: "myImage.png")
else { return }
let shareContent: [Any] = [shareText, url, image]
let activityController = UIActivityViewController(activityItems: shareContent,
applicationActivities: nil)
self.present(activityController, animated: true, completion: nil)
Share content with CompletionWithItemsHandler
CompletionWithItemsHandler
is a closure which execute when UIActivityViewController
dismissed. Inside this closure we can check the activity type, our sharing is success or not, returned items and error.
let shareContent = ["Write your share content"]
let activityController = UIActivityViewController(activityItems: shareContent,
applicationActivities: nil)
self.present(activityController, animated: true, completion: nil)
//Completion handler
activityController.completionWithItemsHandler = { (activityType: UIActivity.ActivityType?, completed:
Bool, arrayReturnedItems: [Any]?, error: Error?) in
if completed {
print("share completed")
return
} else {
print("cancel")
}
if let shareError = error {
print("error while sharing: \(shareError.localizedDescription)")
}
}
Remove activity type with excludedActivityTypes
We can remove any activity type usingexcludedActivityTypes
. Let’s say you want to remove the Facebook and Twitter sharing then simply exclude these activity.
let shareContent = ["Write your share content"]
let activityController = UIActivityViewController(activityItems: shareContent,
applicationActivities: nil)
activityController.excludedActivityTypes = [.postToFacebook, .postToTwitter]
self.present(activityController, animated: true, completion: nil)
Read our next article: How to detect URLs, Addresses, Phone Numbers and Dates from String