Search code examples
swift3uiactivityviewcontroller

sharing video using uiactivityViewcontroller


I am developing an application and want to share images videos and other files saved into my document directory using uiactivityviewcontroller. images and files sharing is working properly. Code for image sharing

var textToShare : Any!
textToShare = UIImage(data:data)//data of an image

Code for other files which are stored into document directory.

var textToShare : Any!
textToShare = url//url of file saved into document directory

But i don't know how to share video. After this all i am using following code to use activityviewcontroller.

let activityViewController = UIActivityViewController(activityItems: [textToShare], applicationActivities: nil)
    activityViewController.popoverPresentationController?.sourceView = self.view // so that iPads won't crash

    // exclude some activity types from the list (optional)
    activityViewController.excludedActivityTypes = [ UIActivityType.airDrop, UIActivityType.postToFacebook ]

    // present the view controller
    self.present(activityViewController, animated: true, completion: nil)

Solution

  • First get video file path

     let videoURL = NSURL(fileURLWithPath:localVideoPath)
    

    And then pass this path to UIActivityViewController like below

    let activityItems = [videoURL, "Check this out!" ]
    let activityController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
    
    activityController.popoverPresentationController?.sourceView = self.view
    activityController.popoverPresentationController?.sourceRect = self.view.frame
    
    self.presentViewController(activityController, animated: true, completion: nil)
    

    Updated Code for Swift 4

        let localVideoPath = "your_video_path_here..."
        let videoURL = URL(fileURLWithPath: localVideoPath)
    
        let activityItems: [Any] = [videoURL, "Check this out!"]
        let activityController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
    
        activityController.popoverPresentationController?.sourceView = view
        activityController.popoverPresentationController?.sourceRect = view.frame
    
        self.present(activityController, animated: true, completion: nil)