Swift 2 - Share video with UIActivityViewController

SNos picture SNos · Nov 9, 2015 · Viewed 8.7k times · Source

I am trying to share a video from a URL using UIActivityViewController. If I try with an Image I have not problems.

Share Image Works:

let imageThump = NSURL(string: "https://example.com/image.png")
        let imageData = NSData(contentsOfURL: imageThump!) 
let objectsToShare = [comment!, imageData!]
            let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)

Share Video is not working. Why?

let videoShare = NSURL(string: "https://example.com/video.mp4")
        let videoData = NSData(contentsOfURL: videoShare!) 
let objectsToShare = [comment!, videoData!]
            let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)

Answer

SNos picture SNos · Jan 22, 2016

THIS IS HOW YOU DO IT....

  let urlData = NSData(contentsOfURL: NSURL(string:"https://example.com/video.mov)")!)

            if ((urlData) != nil){

                print(urlData)


                let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
                let docDirectory = paths[0]
                let filePath = "\(docDirectory)/tmpVideo.mov"
                urlData?.writeToFile(filePath, atomically: true)
                // file saved

                let videoLink = NSURL(fileURLWithPath: filePath)


                let objectsToShare = [videoLink] //comment!, imageData!, myWebsite!]
                let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)

                activityVC.setValue("Video", forKey: "subject")


                //New Excluded Activities Code
                if #available(iOS 9.0, *) {
                    activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList, UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard, UIActivityTypeMail, UIActivityTypeMessage, UIActivityTypeOpenInIBooks, UIActivityTypePostToTencentWeibo, UIActivityTypePostToVimeo, UIActivityTypePostToWeibo, UIActivityTypePrint]
                } else {
                    // Fallback on earlier versions
                    activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList, UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard, UIActivityTypeMail, UIActivityTypeMessage, UIActivityTypePostToTencentWeibo, UIActivityTypePostToVimeo, UIActivityTypePostToWeibo, UIActivityTypePrint ]
                }


                self.presentViewController(activityVC, animated: true, completion: nil)
            }