Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 2 - Share video with UIActivityViewController

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)
like image 977
SNos Avatar asked Nov 09 '15 21:11

SNos


2 Answers

Remember that a video is usually a big file, so it'll take some time for it to be downloaded and saved. Here I used part of the first answer but adding some GCD. You'll get better results.

This is how I did it to avoid the App to stay frozen until the video is downloaded and saved:

let url = NSURL(string: "https://example.com/video.mov")
    
//Show activity indicator
    
DispatchQueue.global(qos: .background).async {
    guard let urlData = NSData(contentsOf: url) else { return }
    let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    let filePath="\(documentsPath)/tempFile.mov"
    DispatchQueue.main.async {
        urlData.write(toFile: filePath, atomically: true)
                                        
        //Hide activity indicator
    
        let activityVC = UIActivityViewController(activityItems: [NSURL(fileURLWithPath: filePath)], applicationActivities: nil)
        activityVC.excludedActivityTypes = [.addToReadingList, .assignToContact]
        self.present(activityVC, animated: true, completion: nil)
    }
}
like image 78
Noel C. Perez Avatar answered Nov 15 '22 20:11

Noel C. Perez


THIS IS HOW YOU DO IT ...

guard let urlData = NSData(contentsOfURL: NSURL(string:"https://example.com/video.mov)")!) else { 
    return 
}

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)
like image 37
SNos Avatar answered Nov 15 '22 19:11

SNos