Search code examples
iosswiftsharinguiactivityviewcontroller

Download video from URL and share it on Facebook, Messages, Twitter using UIActivityViewController


I am using Alamofire for networking and working with Swift 4.

I need to download a video from URL and store it in a particular location and then share it using UIActivityViewController on Facebook, Messages, Twitter etc.

Can someone provide some sample code or guide me on this?

Regards!!


Solution

  • try to download video using Alamofire

    func downloadVideo() {
        let destination = DownloadRequest.suggestedDownloadDestination()
        let url = "https://lynda_files2-a.akamaihd.net/secure/courses/391599/VBR_MP4h264_main_SD/391599_00_01_WX30_welcome.mp4?c3.ri=3770828196132302975&hashval=1522966719_0c357049da0562665ab3f99ccd3e8bca"
    
        Alamofire.download(url, to: destination).downloadProgress(queue: DispatchQueue.global(qos: .utility)) { (progress) in
            print("Progress: \(progress.fractionCompleted)")
        } .validate().responseData { [weak self] (response)  in
            self?.shareVideo(from: response.destinationURL!)
        }
    }
    

    and share using UIActivityViewController

    func shareVideo(from url: URL) {
        let activityViewController = UIActivityViewController(activityItems: [url], applicationActivities: nil)
        present(activityViewController, animated: true, completion: nil)
    }