1

I want to download the video from the url. I have the video url but don't know how to download it.

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    strFileName = @"MyVideo";

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:@"http://www.youtube.com/v/P9MeXRzKUuA?version=3&f=videos&app=youtube_gdata"]]; //strFileURL is url of your video/image
    NSURLConnection *conection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES] autorelease];
    [conection start];
    [request release];

    strFilePath = [[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:strFileName] retain];

    NSLog(@"file path = %@",strFilePath);
}

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// create
NSLog(@"file path = %@",strFilePath);
if (![[NSFileManager defaultManager] fileExistsAtPath:strFilePath]) {
    [[NSFileManager defaultManager] createFileAtPath:strFilePath contents:nil attributes:nil];
}

file = [[NSFileHandle fileHandleForUpdatingAtPath:strFilePath] retain];// read more about file handle
if (file)   {
    [file seekToEndOfFile];
}
NSLog(@"response received");

[responseData setLength:0];

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)receivedata
{
//write each data received
if( receivedata != nil){
    if (file)  {
        [file seekToEndOfFile];
    }
    [file writeData:receivedata];
    NSLog(@"data received");

}
[responseData appendData:receivedata];
}

- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
//close file after finish getting data;

NSLog(@"response = %@",[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);

[file closeFile];
NSLog(@"file closed");

NSURL *fileURL = [NSURL fileURLWithPath:strFilePath];

NSLog(@"fileurl = %@",fileURL);
[webView loadRequest:[NSURLRequest requestWithURL:fileURL]];

}

In the connectiondidfinishedloading method when I print the response it is null.

Please help me with this

Damon
  • 3,004
  • 7
  • 24
  • 28
The_code_cracker
  • 159
  • 2
  • 13
  • Similar que: http://stackoverflow.com/questions/10245345/how-to-download-audio-video-files-from-internet-and-store-in-iphone-app – Mrunal Sep 06 '13 at 10:43
  • you need to search on "YoutubeParser" classes that will help you for doing this simple NSMutableURLRequest will not work for youtube video downloads – D-eptdeveloper Sep 06 '13 at 10:44
  • Technically a simple `NSURLRequest` is enough to download it but you need the parser to get the actual video url. And keep in mind that downloading from youtube is a pretty fast way to get rejected from apple. – Desdenova Sep 06 '13 at 10:53
  • I tried using HCYoutubeParser classes. But it does not play video. Do you know any name or link for any youtube parser classes – The_code_cracker Sep 06 '13 at 12:07

1 Answers1

0

you forgot to initialize the responseData variable. Please do so.

Nareshkumar
  • 2,331
  • 2
  • 20
  • 30