2013-11-23 18 views
5

これは、検索結果を調べるのに苦労しており、ドキュメントでは何も具体的に見つけることができません。私はちょうどファイルをダウンロードし、Documentsディレクトリに保存したいと思う。ファイルは.plistタイプですが、plistを解析する必要はありません。私が言ったように、私はちょうどディスクにそれを保存する必要があります。AFNetworking 2.0を使用してファイルをダウンロードするには

このためにAFHTTPRequestOperationManagerを使用しますか?

これは、私は、一般的にしようとしてきたものです:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
    //manager.responseSerializer = [AFPropertyListResponseSerializer serializer]; 
    manager.responseSerializer = [AFHTTPResponseSerializer serializer]; 

    [manager GET:url parameters:nil 
     success:^(AFHTTPRequestOperation *operation, id responseObject) { 
      DLog(@"downloaded plist"); 
     } 
     failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
      DLog(@"JSON DataError: %@", error); 
     }]; 

私はAFHTTPResponseSerializerを使用している場合、私はこれを取得:

Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: internal server error (500)" 

私はAFPropertyListResponseSerializerを使用している場合、私はこの取得:

Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/plain" 

しかし、正しいAPIを使用しているかどうかはわかりません。

+1

(この)[http://stackoverflow.com/questions/8372661/how-to-download-a-file-and-save-it-to-the- documents-directory-with-afnetworking]が役に立ちます。 – n00bProgrammer

+0

それはAFNetworkingの古いバージョンを使用します。まだ推奨されている方法ですか? – soleil

+0

試してみましたが、まだエラーが発生しています:エラードメイン= AFNetworkingErrorDomainコード= -1011 "要求に失敗しました:内部サーバーエラー(500) – soleil

答えて

15

この例を試してください。 願っています!

// Step 1: create NSURL with full path to downloading file 
// for example try this: NSString *fileUrl = @"https://pbs.twimg.com/profile_images/2331579964/jrqzn4q29vwy4mors75s_400x400.png"; 
// And create NSURLRequest object with our URL 
NSURL *URL = [NSURL URLWithString:fileUrl]; 
NSURLRequest *request = [NSURLRequest requestWithURL:URL]; 

// Step 2: save downloading file's name 
// For example our fileName string is equal to 'jrqzn4q29vwy4mors75s_400x400.png' 
NSString *fileName = [URL lastPathComponent]; 

// Step 3: create AFHTTPRequestOperation object with our request 
AFHTTPRequestOperation *downloadRequest = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 

// Step 4: set handling for answer from server and errors with request 
[downloadRequest setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
      // here we must create NSData object with received data... 
      NSData *data = [[NSData alloc] initWithData:responseObject]; 
      // ... and save this object as file 
      // Here 'pathToFile' must be path to directory 'Documents' on your device + filename, of course 
      [data writeToFile:pathToFile atomically:YES]; 
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
      NSLog(@"file downloading error : %@", [error localizedDescription]); 
     }]; 

// Step 5: begin asynchronous download 
[downloadRequest start]; 

何か他のもの:What's the best way to find the user's Documents directory on an iPhone?

+1

いくつかの説明を書いてください –

+1

私の悪いスタイルの回答を申し訳ありません:) 今すぐより良い? –

+1

同じリクエストが複数回実行される場合はどうすればいいですか?同じリクエストのプロセスが既に実行されている場合、ダウンロードを開始する前に確認できますか? –

関連する問題