2011-11-04 28 views
16

Adam, bu konuda güldü. AFNetworking ile bir dosya indirmeye çalışıyorum ve yapabileceğim en iyi şey downloadProgressBlock içindeki ilerleme baytlarını yakalamaktır. AFNetworking'in birçok farklı çatalını denedim ve şimdi en son yapıya geri döndüm. Bir kez AFHTTPRequestOperation da NSURLResponse içerecek şekilde değiştirilmiş gibi görünüyor ama bu en son sürümde gitti. Ve aşağıdaki kod uyarınca "başarı" bloğu asla çağrılmamaktadır. İndirilen baytların bir günlüğünü alıyorum ve sonra^tamamlandı denir. başarı ve hata asla denir.AFNetworking - Dosya indirmek için AFHTTPRequestOperation'ı mı kullanıyorsunuz?

Bu konuda herhangi bir rehberlik harika olurdu. Verilerin nerede döndüğünü ve NSFileManager'ı kullanmak için nasıl kullanacağımı anlayamıyorum? Dosyaları indirmem gerekiyor, resimler için akış yazmam gerekiyor, vb.

Düzenleme: Ayrıca verileri geçersiz kılmayı denedim - (void) bağlantı: belgede önerilen şekilde didReceiveData ama hiçbir şey yapmıyor.

// url http://mydomain.com/somezip.zip

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", url]]]; 

AFHTTPRequestOperation *operation = [AFHTTPRequestOperation HTTPRequestOperationWithRequest:request success:^(id object) { 

    NSLog(@"hey, some success!"); 

} failure:^(NSHTTPURLResponse *response, NSError *error) { 

    NSLog(@"%@", error); 

}]; 


[operation setDownloadProgressBlock:^(NSInteger bytesRead, NSInteger totalBytesRead, NSInteger totalBytesExpectedToRead) { 

    // callback is sent in and works great. Just outputting to the console at the moment 
    SEL callme = NSSelectorFromString(callback); 
    [sender performSelectorOnMainThread:callme withObject:[NSNumber numberWithInt:bytesRead] waitUntilDone:NO]; 

}]; 

[operation setCompletionBlock:^{ 
    NSLog(@"operation complete"); 
}]; 

NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease]; 
[queue addOperation:operation]; 

cevap

64

Sadece AFNetworking bulunmalı ve diske kaydetmek bile bu özelliği kullanabilirsiniz olduğu - Bildirimi operation.outputStream:

NSMutableURLRequest* rq = [api requestWithMethod:@"GET" path:@"YOUR/URL/TO/FILE" parameters:nil]; 
AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:rq] autorelease]; 

NSString* path=[@"/PATH/TO/APP" stringByAppendingPathComponent: imageNameToDisk]; 
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO]; 

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 

    NSLog(@"SUCCCESSFULL IMG RETRIEVE to %@!",path) 

} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 

    // Deal with failure 
}]; 

DÜZENLEME: missed

[operation start]; 

DÜZENLEME: ...or if you use an AFHTTPClient * apiClient :

// [apiClient enqueueHTTPRequestOperation:operation]; 
+3

Biri bu adama bir upvote ver! Ve OP cevabı seçmeli! – radj

İlgili konular