How to get download using AFHTTPSessionManager in AFNetworking 3.0

When I use AFNetworking 2, I could make progress with AFHTTPRequestOperation as follows:

NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:aURL cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:_timeoutInterval];
AFHTTPRequestOperation *imageRequestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
imageRequestOperation.responseSerializer = [AFImageResponseSerializer serializer];

__weak AFHTTPRequestOperation *imageRequestOperationForBlock = imageRequestOperation;

[imageRequestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    // ...
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    // ...
}];

[imageRequestOperation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
    if (progress) {
        progress((float)totalBytesRead / totalBytesExpectedToRead);
    }
}];

I use AFHTTPSessionManager GET:parameters:success:failure:to retrieve data, but I do not know how to get the download in AFNetworking 3.0.

+4
source share
1 answer

The link in the comments was skipped ( NSURLSessionDownloadTask). Sorry.

The code below should work.

Assumption: this code is placed in a subclass AFHTTPSessionManagerwith declared NSURLSessionDataTask *testTaskivar. It should be simple enough if necessary.

The important part of the code taken from this answer

- (void)test
{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://eoimages.gsfc.nasa.gov/images/imagerecords/78000/78797/nwpassage_tmo_2012199_lrg.jpg"]];

    testTask = [self dataTaskWithRequest:request
                       completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) {

                           if (error)
                           {
                               //...
                               NSLog(@"error");
                           }
                           else
                           {
                               //...

                               UIImage *result = responseObject;

                               NSLog(@"Success: %@",NSStringFromCGSize(result.size));
                           }
                       }];

    [self setDataTaskDidReceiveDataBlock:^(NSURLSession *session,
                                                      NSURLSessionDataTask *dataTask,
                                                      NSData *data)
     {
         if (dataTask.countOfBytesExpectedToReceive == NSURLSessionTransferSizeUnknown)
           return;

         if (dataTask != testTask)
           return;

         NSUInteger code = [(NSHTTPURLResponse *)dataTask.response statusCode];

         if (!(code> 199 && code < 400))
           return;

         long long  bytesReceived = [dataTask countOfBytesReceived];
         long long  bytesTotal = [dataTask countOfBytesExpectedToReceive];

         NSLog(@"... %lld/%lld",
               bytesReceived,
               bytesTotal);
     }];

    [testTask resume];
}
+3
source

Source: https://habr.com/ru/post/1616251/


All Articles