0

i am using NSURLConnection to download mp3 data from the server , my code is here

- (IBAction)downloadData:(id)sender
 {

   NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
   NSURL *url = [[NSURL alloc] initWithString:@"http://viadj.viastreaming.net/start/psalmsmedia/ondemand/Nin%20snehamethrayo.mp3"];

   [request setURL:url];
   [url release];
   url = nil;

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


 }

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
 {
     responseData = [[NSMutableData alloc] init];
 }

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

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
 {
  [responseData release];
  [connection release];

 }

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
 {

  NSLog(@"Succeeded! Received %d bytes of data",[responseData
                                               length]);
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *fileName = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"myFile"];

  [responseData writeToFile:fileName atomically:YES];

  responseData = nil;
  self->imageConnection = nil;


  }

am little bit confused about the path given to download. when i click download button i shows "Succeeded! Received 1329 bytes of data" but nothing is downloading. need some help. how will we specify the local path of iPhone to store downloaded data?

Hector
  • 3,909
  • 2
  • 30
  • 46
Neeraj Neeru
  • 570
  • 8
  • 28
  • Follow this https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsurlconnection_Class/Reference/Reference.html – Khalid Usman May 09 '12 at 06:13
  • You're downloading the entire file to RAM then serialising it to disk. This means that large mp3 will crash the application as it runs out of memory – Hector May 10 '12 at 04:22

2 Answers2

2
- (IBAction)downloadData:(id)sender
{
  NSURL *url = [[NSURL alloc] initWithString:@"http://viadj.viastreaming.net/start/psalmsmedia/ondemand/Nin%20snehamethrayo.mp3"];
  NSMutableURLRequest   *theRequest_to = [NSMutableURLRequest requestWithURL:url];
  [url release];
  NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:theRequest_to delegate:self];

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response
{
  NSString *filepath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"snehamethrayo.mp3"]; // here you can set your filename which you can get from url 
  [[NSFileManager defaultManager] createFileAtPath:filepath contents:nil attributes:nil];
  file = [[NSFileHandle fileHandleForUpdatingAtPath:filepath] retain];// Here file is object of NSFileHandle and its declare in .h File
  [file seekToEndOfFile];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
  [file seekToEndOfFile];
  [file writeData:data];
}


- (void)connectionDidFinishLoading:(NSURLConnection*)connection 
{
 [file closeFile];
}
Hector
  • 3,909
  • 2
  • 30
  • 46
  • thank you for your feedback. but, where did this data saving. i printed the data after didReceiveData and shows some binary values. do we need to decode it or how will we see the data is downloading? – Neeraj Neeru May 09 '12 at 06:09
  • for download progress see this : http://stackoverflow.com/questions/2267950/how-to-make-an-progress-bar-for-an-nsurlconnection-when-downloading-a-file – Hector May 09 '12 at 06:36
  • when i run this code on ma device the file path displays like /var/mobile/Applications/939A18E2-0CAC-4FD4-A5A9-01BA34680256/Documents/snehamethrayo.mp3 , actually where was this file saved, iPhone local file? i can't see the downloaded file on my device – Neeraj Neeru May 09 '12 at 06:48
0

No need for any code change I think.Just put an nslog and see...

NSString *fileName = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"myFile"];
NSLog(@"%@",fileName);

That will list the file location like this /Users/me/Library/Application Support/iPhone Simulator/5.0/Applications/(your app)/Documents/myFile. ie the downloaded file is in your document folder.

note: don't forget to put the file format ie

NSString *fileName = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"myFile.mp3"];
Guru
  • 4,693
  • 3
  • 25
  • 45
  • He is downlading a mp3 file with 9.7 MB on disk – Guru May 09 '12 at 06:21
  • @Hector for exaple i put that link, usually all my mp3 files are of this size (9/8.something), so what method should i use, which is more helpful. and how will i display downloading progress? – Neeraj Neeru May 09 '12 at 06:32
  • @Akhildas when i run this code on ma device the file path displays like /var/mobile/Applications/939A18E2-0CAC-4FD4-A5A9-01BA34680256/Documents/snehamethrayo.mp3 , actually where was this file saved, iPhone local file? i can't see the downloaded file on my device – Neeraj Neeru May 09 '12 at 06:47
  • @NeerajNeeru to see your file please enable **Application supports iTunes file sharing** in your .plist file. Then you could see the files in iTunes – Guru May 09 '12 at 07:03
  • @Akhildas sorry, am just a learner.. yah i edited the plist to support iTunes file sharing. but can i add progress view to see downloading started/ended something like that. – Neeraj Neeru May 09 '12 at 07:15
  • @NeerajNeeru Simply activity indicator is the solution for your problem.In your IBAction start the activity indicator and stop it in in connection: didReceiveResponse – Guru May 09 '12 at 07:22
  • alert = [[UIAlertView alloc] initWithTitle:titleString message:messageString delegate:self cancelButtonTitle:nil otherButtonTitles: nil]; UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(xpos,ypos,width,height)]; [activityIndicator startAnimating]; [alert addSubview:activityIndicator]; [alert show]; This is to start the activity indicator And to stop the indicator [alert dismissWithClickedButtonIndex:0 animated:NO]; do this in connection: didReceiveResponse – Guru May 09 '12 at 07:35
  • You're downloading the entire file to RAM then serialising it to disk. This means that large mp3 will crash the application as it runs out of memory – Hector May 10 '12 at 04:22