Minkle Garg
Minkle Garg

Reputation: 751

method to download mp3 file from url

If someone want to download a file instead of playing it, then what functionality or method will be used for it.

I want to download the file. Provide me some example if u can, or give me some idea about this concept.

Thankyou very much.

Upvotes: 0

Views: 763

Answers (1)

Mohammad
Mohammad

Reputation: 1666

Send request following way.

NSURL *url = [NSURL URLWithString:[fileUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setValue:[NSString stringWithFormat:@"bytes=%ld-",0] forHTTPHeaderField:@"Range"];
[theRequest addValue: @"pdf" forHTTPHeaderField:@"Content-Type"];
[theRequest setHTTPMethod:@"POST"];
webData = [[NSMutableData alloc] init];
theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];

Implement following methods...

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

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [webData appendData:data]; }

-(void)connectionDidFinishLoading:(NSURLConnection )connection {
/
******CODE FOR WRITING FILE*************/ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentPath = [paths objectAtIndex:0];

NSString *directoryPath = [documentPath stringByAppendingPathComponent:[MAIN_DIRECTORY stringByAppendingPathComponent:fileDate]];

[[NSFileManager defaultManager] createDirectoryAtPath:directoryPath withIntermediateDirectories:NO attributes:nil error:nil];

[webData writeToFile:fileName atomically:YES];

}

Upvotes: 1

Related Questions