Melanie Journe Melanie Journe - 1 year ago 91
Swift Question

Swift - Downloading video with downloadTaskWithURL

I'm downloading a video thanks to downloadTaskWithURL and I'm saving it to my gallery with this code :

func saveVideoBis(fileStringURL:String){


let url = NSURL(string: fileStringURL);
(NSURLSession.sharedSession().downloadTaskWithURL(url!) { (location:NSURL?, r:NSURLResponse?, e:NSError?) -> Void in

let mgr = NSFileManager.defaultManager()

let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0];


let destination = NSURL(string: NSString(format: "%@/%@", documentsPath, url!.lastPathComponent!) as String);


try? mgr.moveItemAtPath(location!.path!, toPath: destination!.path!)

PHPhotoLibrary.requestAuthorization({ (a:PHAuthorizationStatus) -> Void in
}) { completed, error in
if completed {
print("Video is saved!");

It works perfectly fine on my simulator but on my iPad the video isn't saved even if the
print("Video is saved!");
Do you have any idea why ?

I also have that message appearing in my console

Unable to create data from file (null)

Answer Source

Please check comments through the code:

Xcode 8 GM • Swift 3

import UIKit
import Photos

class ViewController: UIViewController {
    func downloadVideoLinkAndCreateAsset(_ videoLink: String) {
        // use guard to make sure you have a valid url
        guard let videoURL = URL(string: videoLink) else { return }
        let documentsDirectoryURL =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        // check if the file already exist at the destination folder if you don't want to download it twice
        if !FileManager.default.fileExists(atPath: documentsDirectoryURL.appendingPathComponent(videoURL.lastPathComponent).path) {
            // set up your download task

            URLSession.shared.downloadTask(with: videoURL) { (location, response, error) -> Void in
                // use guard to unwrap your optional url
                guard let location = location else { return }
                // create a deatination url with the server response suggested file name
                let destinationURL = documentsDirectoryURL.appendingPathComponent(response?.suggestedFilename ?? videoURL.lastPathComponent)

                do {
                    try FileManager.default.moveItem(at: location, to: destinationURL)
                    PHPhotoLibrary.requestAuthorization({ (authorizationStatus: PHAuthorizationStatus) -> Void in
                        // check if user authorized access photos for your app
                        if authorizationStatus == .authorized {
                                PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: destinationURL)}) { completed, error in
                                    if completed {
                                        print("Video asset created")
                                    } else {
                }  catch let error as NSError { print(error.localizedDescription)}

        } else {
            print("file already exists at destination url")
    override func viewDidLoad() {
        // Do any additional setup after loading the view, typically from a nib.
    override func didReceiveMemoryWarning() {
        // Dispose of any resources that can be recreated.