2013-11-23 20 views
5

Ho difficoltà a setacciare i risultati della ricerca su come eseguire questa operazione e non riesco a trovare nulla di concreto nella documentazione. Voglio solo scaricare un file e archiviarlo nella directory Documenti. Il file è di tipo .plist, ma non ho bisogno di analizzare il plist. Come ho detto, devo solo salvarlo sul disco.Come scaricare un file con AFNetworking 2.0

Per questo utilizzo AFHTTPRequestOperationManager?

Questo è quello che ho cercato in generale:

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

Se uso AFHTTPResponseSerializer ottengo questo:

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

Se uso AFPropertyListResponseSerializer ottengo questo:

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

Ma non sono nemmeno sicuro che sto usando l'API corretta.

+1

(Questo) [http://stackoverflow.com/questions/8372661/how-to-download-a-file-and-save-it-to-the- document-directory-with-afnetworking] potrebbe aiutare. – n00bProgrammer

+0

Tuttavia, utilizza la versione precedente di AFNetworking. È ancora il modo consigliato? – soleil

+0

Ho provato e ricevo ancora Errore: Errore Dominio = AFNetworkingErrorDomain Code = -1011 "Richiesta non riuscita: errore interno del server (500) – soleil

risposta

15

Per favore, prova questo esempio. Spero che questo aiuto!

// 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]; 

Un'altra cosa: What's the best way to find the user's Documents directory on an iPhone?

+1

Scrivi anche qualche spiegazione –

+1

Scusa per il mio cattivo stile nelle risposte :) Ora meglio così? –

+1

puoi dirmi cosa succede se la stessa richiesta verrà eseguita più volte? possiamo controllare prima di iniziare il download che se c'è già in esecuzione il processo per la stessa richiesta? –

Problemi correlati