How to download video from url and save it in to document directory in iOS
Asked
Active
Viewed 1.8k times
4 Answers
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
-
it works.... thank u so much.... but the problem is its not showing me the status of download..... – Praksha Apr 02 '14 at 11:22
-
Nop it wont because youtube has its own policy for it – Kalpit Gajera Aug 17 '17 at 13:11
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