0

i need to download a mp3 file from internet through my application and i found a solution , to use NSURLConnection and connection request. here is my code (code from another faq)

 - (IBAction)downloadData:(id)sender
 {
    NSURL *myURL = [NSURL URLWithString:@"http://www.example.org/mp3song"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];

   [[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]);

    NSString *txt = [[[NSString alloc] initWithData:responseData encoding: NSASCIIStringEncoding] autorelease];
 }

how will i save this response data mp3 file to my local file of iPhone?

4

1 回答 1

0

so i found the solution after a long googling which i forget to add the code

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *fileName = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"myFile"];

   [responseData writeToFile:fileName atomically:YES];

thankyou

于 2012-05-09T04:51:06.990 回答