In iOS we can share the content of an app to the social world(Facebook,twitter, gmail and many more ) using UIActivityViewController. UIActivityViewController shows the list of activities when presented on a UIViewController. Each activity in the UIActivityViewController represents the media type which allow to share the content of our app.
We don't need to integrate the designated SDK to share the content. All tasks are handled by UIActivityViewController itself.
To share the content(like photo or text) , follow these steps:
1) Create an instance of UIActivityViewController by passing required arguments.
let textItem = "Welcome here"
let imageItem = UIImage(named:"hello")
The initializer method of UIActivityViewController require to pass two arguments named activityItems: and applicationActivities: . In first parameter we must pass the array of items to share. In our case it is an image and a text. In second parameter we pass the array of activities which we add custom . These activities are not provided by the UIActivityViewController . We will talk later about custom activity and see how to create a custom activity and show in UIActivityViewController . Here we pass nil for applicationActivites: .
2) Present the activityViewContoller from your view controller:
We can exclude the activities which we don't want to be listed in UIActivityController by setting the value of excludedActivityTypes of UIActivityViewController.
To check which activity the user has interacted with , pass a closure to completionWithItemsHandler property of UIActivityViewController:
activityC.completionWithItemsHandler = {
Here we see how to share the content with UIActivityViewController and a brief introduction of important properties of UIActivityViewController.
We can customize the UIActityViewController such that we can define the custom action for each activity , assign different data for a UIActivity and many more . To accomplish these kinds of tasks we have to subclassed either UIActivityItemSource or UIActivityItemProvider and then we pass the array of objects of UIActivityItemSource or UIActivityItemProvider to the activityItems: parameter of initializer method of UIActivityViewController . Here we won't go in to the details , these require an another blog.
}
To use this in UIActivityViewController pass the instance of CustomActivity to the applicationActivities parameter of initializer method of UIActivityViewController .
let activityC = UIActivityViewController(activityItems: [imageItem,textItem], applicationActivities:CustomActivity())
References:
https://developer.apple.com/reference/uikit/uiactivityviewcontroller
http://nshipster.com/uiactivityviewcontroller/
We don't need to integrate the designated SDK to share the content. All tasks are handled by UIActivityViewController itself.
To share the content(like photo or text) , follow these steps:
1) Create an instance of UIActivityViewController by passing required arguments.
let textItem = "Welcome here"
let imageItem = UIImage(named:"hello")
let activityC = UIActivityViewController(activityItems: [imageItem,textItem], applicationActivities:nil)
The initializer method of UIActivityViewController require to pass two arguments named activityItems: and applicationActivities: . In first parameter we must pass the array of items to share. In our case it is an image and a text. In second parameter we pass the array of activities which we add custom . These activities are not provided by the UIActivityViewController . We will talk later about custom activity and see how to create a custom activity and show in UIActivityViewController . Here we pass nil for applicationActivites: .
2) Present the activityViewContoller from your view controller:
self.present(activityC, animated: true, completion: nil)
We can exclude the activities which we don't want to be listed in UIActivityController by setting the value of excludedActivityTypes of UIActivityViewController.
activityC.excludedActivityTypes = [.airDrop,.assignToContact,.postToVimeo,.postToWeibo,.postToFlickr,.postToTencentWeibo,.print,.copyToPasteboard,.openInIBooks,.addToReadingList]
activityC.completionWithItemsHandler = {
(type, success,items,error)->Void in
if success{
print("item shared of type \(type)")
}
}Here we see how to share the content with UIActivityViewController and a brief introduction of important properties of UIActivityViewController.
We can customize the UIActityViewController such that we can define the custom action for each activity , assign different data for a UIActivity and many more . To accomplish these kinds of tasks we have to subclassed either UIActivityItemSource or UIActivityItemProvider and then we pass the array of objects of UIActivityItemSource or UIActivityItemProvider to the activityItems: parameter of initializer method of UIActivityViewController . Here we won't go in to the details , these require an another blog.
CustomActivity:
To create an application activity we subclass the UIActivity and override the methods and properties of UIActivity class. In our subclass we return the activity title, activity image and activity type and UIViewController for the activity, from the designated properties.
class CustomActivity: UIActivity {
// returns activity title
override var activityTitle: String?{
return "MyActivity"
}
//thumbnail image for the activity
override var activityImage: UIImage?{
return UIImage(named: "")
}
//activiyt type
override var activityType: UIActivityType{
return UIActivityType.postToFacebook
}
//view controller for the activity
override var activityViewController: UIViewController?{
print("user did tap on my activity")
return nil
}
//here check whether this activity can perfor with given list of items
override func canPerform(withActivityItems activityItems: [Any]) -> Bool {
return true
}
//prepare the data to perform with
override func prepare(withActivityItems activityItems: [Any]) {
}
To use this in UIActivityViewController pass the instance of CustomActivity to the applicationActivities parameter of initializer method of UIActivityViewController .
let activityC = UIActivityViewController(activityItems: [imageItem,textItem], applicationActivities:CustomActivity())
References:
https://developer.apple.com/reference/uikit/uiactivityviewcontroller
http://nshipster.com/uiactivityviewcontroller/