0

サーバーから mp3 データをダウンロードするために NSURLConnection を使用しています。コードはこちら

- (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;


  }

ダウンロードに指定されたパスについて少し混乱しています。ダウンロードボタンをクリックすると、「Succeeded! Received 1329 bytes of data」と表示されますが、何もダウンロードされていません。助けが要る。ダウンロードしたデータを保存するための iPhone のローカル パスをどのように指定しますか?

4

2 に答える 2

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];
}
于 2012-05-09T05:48:09.450 に答える
0

コードを変更する必要はないと思います.nslogを入れて見てください...

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

/Users/me/Library/Application Support/iPhone Simulator/5.0/Applications/(your app)/Documents/myFile. つまり、ダウンロードしたファイルはドキュメント フォルダにあります。

注: ファイル形式を入力することを忘れないでください。

NSString *fileName = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"myFile.mp3"];
于 2012-05-09T06:07:07.093 に答える