3

How to download video from url and save it in to document directory in iOS

Praksha
  • 265
  • 1
  • 3
  • 10

4 Answers4

26

use this code it is working in my current project

-(void)DownloadVideo
{
//download the file in a seperate thread.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"Downloading Started");
NSString *urlToDownload = @"http://www.somewhere.com/thefile.mp4";
NSURL  *url = [NSURL URLWithString:urlToDownload];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
    {
    NSArray       *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString  *documentsDirectory = [paths objectAtIndex:0];

    NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"thefile.mp4"];

    //saving is done on main thread
    dispatch_async(dispatch_get_main_queue(), ^{
        [urlData writeToFile:filePath atomically:YES];
        NSLog(@"File Saved !");
    });
    }

});
}
Kalpit Gajera
  • 2,475
  • 1
  • 14
  • 24
5

You can download it using GCD.

-(void)downloadVideoAndSave :(NSString*)videoUrl
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        NSData *yourVideoData=[NSData dataWithContentsOfURL:[NSURL URLWithString:videoUrl]];

        if (yourVideoData) {
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];

            NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"video.mp4"];

            if([yourVideoData writeToFile:videpPath atomically:YES])
            {
                NSLog(@"write successfull");
            }
            else{
                NSLog(@"write failed");
            }
        }
    });
}
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
1

You can use the NSURLConnection method sendAsynchronousRequest:queue:completionHandler:.

That one method will download an entire file into an NSData object and call your completion handler method once it's done.

If you can write a app that requires iOS 7 or later, you could also use the new NSURLSession API. That offers a lot more features.

If you search using those terms you should be able to find tutorials and sample apps illustrating both APIs.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
-1

You can also implement same in Swift 2.0

class func downloadVideo(videoImageUrl:String)
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
        //All stuff here

        let url=NSURL(string: videoImageUrl)
        let urlData=NSData(contentsOfURL: url!)

        if((urlData) != nil)
        {
            let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]

            let fileName = videoImageUrl.lastPathComponent //.stringByDeletingPathExtension

            let filePath="\(documentsPath)/\(fileName)"

            //saving is done on main thread

            dispatch_async(dispatch_get_main_queue(), { () -> Void in

                 urlData?.writeToFile(filePath, atomically: true)
            })

        }
    })

}
Arvind Kumar
  • 2,371
  • 1
  • 18
  • 25