2014-04-29 73 views
0

在我的應用程序中,我使用了AFHTTPRequestOperationManager進行API集成及其工作。我需要顯示數據下載的進度,並且我使用setCompletionBlockWithSuccess回調進行了檢查,但未被調用。如何在AFHTTPRequestOperationManager中找到數據下載。setCompletionBlockWithSuccess在AFHTTPRequestOperationManager中未調用

請幫助我。


AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager manager] initWithBaseURL:[NSURL URLWithString:@""]]; 
[manager.requestSerializer setValue:SourceType forHTTPHeaderField:@"Source"]; 
[manager.requestSerializer setValue:sig forHTTPHeaderField:@"Sig"]; 
[manager GET:mtdName parameters:params success:^(AFHTTPRequestOperation *operation, id responseDict) 
    {} 
     failure:^(AFHTTPRequestOperation *operation, NSError *error) 
    {}]; 

回答

0

我發現: http://cocoadocs.org/docsets/AFNetworking/2.2.3/

Creating a Download Task 
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration  defaultSessionConfiguration]; 
AFURLSessionManager *manager = [[AFURLSessionManager alloc]  initWithSessionConfiguration:configuration]; 

NSURL *URL = [NSURL URLWithString:@"http://example.com/download.zip"]; 
NSURLRequest *request = [NSURLRequest requestWithURL:URL]; 

NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) { 
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; 
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]]; 
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) { 
NSLog(@"File downloaded to: %@", filePath); 
}]; 
[downloadTask resume]; 
+0

料缸嗨,我沒有下載file.I稱爲API和響應有一些JSON格式的數據。對此,我需要d在splashScreen中顯示一些進度條。如何做到這一點? – Richard

0

使用:

AFHTTPRequestOperation *operation; 
operation = 
[manager HTTPRequestOperationWithRequest:[[AFJSONRequestSerializer serializer] requestWithMethod:method URLString:url parameters:info] success:^(AFHTTPRequestOperation *operation, id responseObject) { 
}]; 


[operation setDownloadProgressBlock:^(NSUInteger bytesRead, NSInteger totalBytesRead, NSInteger totalBytesExpectedToRead) { 
    //do something here. 
}]; 

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
manager.securityPolicy.allowInvalidCertificates = YES ;//if use https 
[manager.operationQueue addOperation:operation]; 
+0

requestWithMethod:方法,「method」是@「POST」,@「GET」,@「PUT」... – liaogang

相關問題