SNos
SNos

Reputation: 3470

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)

Upvotes: 12

Views: 12002

Answers (3)

SNos
SNos

Reputation: 3470

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)

Upvotes: 23

Noel C. Perez
Noel C. Perez

Reputation: 103

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)
    }
}

Upvotes: 10

Eric Mentele
Eric Mentele

Reputation: 1060

It is not working because this class does not allow NSData to be passed to the Facebook activity type.

"UIActivityTypePostToFacebook The object posts the provided content to the user’s wall on Facebook.

When using this service, you can provide NSString, NSAttributedString, UIImage, ALAsset, and NSURL objects as data for the activity items. You may also specify NSURL objects whose contents use the assets-library scheme."

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIActivity_Class/index.html#//apple_ref/doc/constant_group/Built_in_Activity_Types

I have been trying to get this to work as well. Haven't figured out how to get it to work; however, this is why your code is not working.

Upvotes: 2

Related Questions