Swift system share video (uiactivityviewcontroller)


Uiactivityviewcontroller introduction

The system sharing function is implemented by uiactivityviewcontroller. This class can share the content to be shared to social media, SMS, e-mail, etc. the shared content can be text, picture, link, video, etc.


UIActivityViewController(activityItems: <#T##[Any]#>, applicationActivities: <#T##[UIActivity]?#>)

Uiactivityviewcontroller initialization needs to pass two parameters. The first parameter activityitems is the content to be shared. The object type in the array can be arbitrary.
As follows:

Var activityitems = ["what to share", UIImage.init (named: "name of the picture to share")] as [any]
UIActivityViewController(activityItems: activityItems, applicationActivities: nil)

If the shared content is of the same type, you can not add as [any], otherwise, you have to add

The second parameter, applicationactivities, represents an array of objects for custom services supported by the application. One is usually written as nil.
If we need to customize the service type, we need to create a customuiactivity and inherit uiactivity

class CustomUIActicity: UIActivity {

At the same time, we need to copy the following methods

 1. activityType
 2. activityTitle
 3. activityImage
 4. canPerform(withActivityItems:)
 5. prepare(withActivityItems:)
 6. activityCategory

activityTypeIs the identifier of the provided service type, and generally returns a custom class name.activityTitleactivityImageSelf defined service title and logo.canPerform(withActivityItems:)Make a pre judgment on the content to be shared to see if it needs to be shared.prepare(withActivityItems:)Click the share button to share.activityCategoryThe types of sharing can be divided into share and action

Swift system share video (uiactivityviewcontroller)

Share text, pictures, links

First define what you want to share

Let textshare = the title of the share. "
 let imageShare = #imageLiteral(resourceName: "icon_tab1_select")
 let urlShare = URL(string: "http://www.baidu.com")
 let activityItems = [textShare,imageShare,urlShare] as [Any]

According to the above initialization process, initialize

let toVC = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
present(toVC, animated: true, completion: nil) 

Share video

First define the URL of a video

let videoUrl = URL(fileURLWithPath: Bundle.main.path(forResource: "test", ofType: "mp4") ?? "")


let activityVC = UIActivityViewController(activityItems: [videoUrl] , applicationActivities: nil)
present(toVC, animated: true, completion: nil)

Share custom uiactivity

The first choice is to customize a custom uiactivity and copy the relevant methods

class CustomUIActicity: UIActivity {
    //Shared title
    override var activityTitle: String? {
        Return "first class commander"
    //Shared logo
    override var activityImage: UIImage? {
        return UIImage.init(named: "icon_tab1_select")
    //Identifier of the type of service provided
    override var activityType: UIActivityType? {
        return UIActivityType.init(CustomUIActicity.self.description())
    //Sharing type
    override class var activityCategory: UIActivityCategory {
        return .action
    ///Here, do the operation on the content to be shared
    override func prepare(withActivityItems activityItems: [Any]) {
    ///Whether sharing is allowed under the pre judgment here
    override func canPerform(withActivityItems activityItems: [Any]) -> Bool {
        return true

Create content to share

Let tovc = uiactivityviewcontroller (activityitems: [shared title], applicationactivities: [customuiactivity()])
present(toVC, animated: true, completion: nil)

Second parameter of uiactivityviewcontrollerapplicationActivitiesThe input is our custom UI activity.

If we need to monitor the shared results, write the following

toVC.completionWithItemsHandler = {(_ activityType: UIActivityType?, _ completed: Bool, _ returnedItems: [Any]?, _ activityError: Error?) -> Void in
            Print (completed? "Success": "failed")

Git address: https://github.com/LSnumber1/…