How to download and save video from web in iphone development

Forums Programming Programming – Everything else How to download and save video from web in iphone development

This topic contains 4 replies, has 2 voices, and was last updated by  bradparks 4 years, 2 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
Author Posts
Author Posts
February 9, 2010 at 1:16 pm #219754

pawangera
@pawangera

Hi All,

Can anyone know how we can download video from web and save it into the application in iphone development. Actually i have to implement this functionality very soon in one of my application but i haven’t any idea about it. Can anyone plz tell me how can i do this.

Thanks in advance.

Pawan Gera

February 9, 2010 at 1:29 pm #272982

bradparks
Participant
@bradparks

this opensource library might help you out… it helps you do http posts/gets easily and asynchronously using objective c:

http://github.com/pokeb/asi-http-request/

February 15, 2010 at 4:48 am #272983

pawangera
@pawangera

Hi,

I am trying this code to write m4v file. This code works fine if i am writing any txt file. But it’s not work for m4v. It creates 4kb blank m4v file for me. Can someone tell wht’s going wrong in this code.

- (void)viewDidLoad {

NSString *post=@”key1=val1&key2=val2″;

NSData *postData= [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];

[request setURL:[NSURL URLWithString:@"http://192.168.1.100:85/km/nano01_4_3.m4v"]];

[request setHTTPMethod:@"POST"];

[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

[request setHTTPBody:postData];

NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];

if (conn)

{

receivedData = [[NSMutableData data] retain];

//[postData writeToFile:@"/Users/sachinuttam/desktop/php.txt" atomically:YES];

NSString *msg = [[NSString alloc] initWithFormat:@”image location 0%i”,1];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”touch image value” message:msg delegate:self cancelButtonTitle:@”Ok!” otherButtonTitles:nil];

[msg release];

[alert show];

[alert release];

} else

{ // inform the user that the download could not be made

NSString *msg = [[NSString alloc] initWithFormat:@”image location 0%i”,123];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”touch image value” message:msg delegate:self cancelButtonTitle:@”Ok!” otherButtonTitles:nil];

[msg release];

[alert show];

[alert release];

}

[super viewDidLoad];

}

/*- (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning]; // Releases the view if it doesn’t have a superview.

// Release anything that’s not essential, such as cached data.

} */

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

{

[receivedData setLength:0];

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

[receivedData appendData:data];

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{

// do something with the data

// receivedData is declared as a method instance elsewhere

NSLog(@”Succeeded! Received %d bytes of data”,[receivedData length]);

NSString *aStr = [[NSString alloc] initWithData:receivedData encoding:NSASCIIStringEncoding];

NSLog(aStr);

[receivedData writeToFile:@"/Users/sachinuttam/desktop/nano01_4_3.m4v" atomically:YES];

//[[NSData dataWithContentsOfURL:myURL] writeToFile:[cachesDirectory stringByAppendingString:@"/test.mp4"] atomically:NO];

// release the connection, and the data object

[receivedData release];

}

February 15, 2010 at 1:32 pm #272984

bradparks
Participant
@bradparks

even though I linked to it, I haven’t used the above lib… that having been said, here’s some complete guesses as to what’s up:

Seems weird to me that this code is setting the content length on the request…. Do you have to set that? I’d try not setting it and see how it goes:

[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

What does the following line output in the debug console:

NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);

are you getting the correct file size there? If so, then the problem would be with purely saving the file… but that looks good to me…

February 15, 2010 at 2:00 pm #272985

bradparks
Participant
@bradparks

and for what it’s worth, here’s an article I just came across on a blog detailing how to integrate an iPhone app with PHP/other server side stuff, and one of the examples is downloading an image:

http://www.cimgf.com/2010/02/12/accessing-the-cloud-from-cocoa-touch/

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.