2012-05-08 10 views
0

私のアプリケーションを介してインターネットからmp3ファイルをダウンロードする必要があり、NSURLConnectionと接続要求を使用するための解決策が見つかりました。ここに私のコード(他のよくある質問からコード)NSURLConnection非同期ダウンロードデータがiPhoneのローカルファイルに保存されていますか?

は、私はiPhoneの私のローカルファイルには、この応答データmp3ファイルを保存する方法
- (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]; 
} 

のですか?

答えて

0

はので、私は、コード

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

    [responseData writeToFile:fileName atomically:YES]; 

ありがとう

を追加するのを忘れ、長いグーグル後に解決策を見つけました
関連する問題