Download file using AFNetworking on iOS 6
Asked Answered
H

3

2

I've recently updated to AFNetworking 2.0. The documentation said it is compatible with iOS6.0+. I am building a iOS 6.0 app, when I am trying to implement a download method (both images and videos). The example use

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

However, I got an "Use of undeclared identifier 'AFURLSessionManager'" error. And I found out that AFURLSessionManager use a class that is only available from iOS7. I'm just wondering, who could I download in iOS6 using AFNetworking?

Also, is there anyway to see download progress?

Thank you

Hershelhershell answered 10/10, 2013 at 12:25 Comment(0)
P
2

As you say AFURLSessionManager is only available in iOS 7(is backed by NSURLSession), so you should use the NSURLConnection based classes in AFNetworking 2.0 (AFHTTPRequestOperationManager, AFHTTPRequestOperation, etc).

Physical answered 10/10, 2013 at 12:31 Comment(3)
OK, found this... So to recap: in order to support the new NSURLSession APIs as well as the old-but-not-deprecated-and-still-useful NSURLConnection, the core components of AFNetworking 2.0 are split between request operation and session tasks. AFHTTPRequestOperationManager and AFHTTPSessionManager provide similar functionality, with nearly interchangeable interfaces that can be swapped out rather easily, should the need arise (such as porting between iOS 6 and 7).Hershelhershell
Do you have an example or tutorial for downloading files using AFHTTPRequestOperationManager?Hershelhershell
I don't but it should be straightforward, you just need to create a AFHTTPRequestOperation(you can use the manager's method HTTPRequestOperationWithRequest:success:failure:) and set its outputStream to one pointing to a file.Physical
B
4

You can use the AFHTTPRequestOperation class to perform a file download on iOS 6. You basically just need to set the operation's outputStream property to store the file and the downloadProgressBlock property to monitor the progress.

The bare bones method below is declared in a class that is a subclass of AFHTTPRequestOperationManager. When I initialized an instance of this class I set up the baseURL property.

- (AFHTTPRequestOperation *)downloadFileWithContentId:(NSString *)contentId destination:(NSString*)destinationPath {

    NSString *relativeURLString = [NSString stringWithFormat:@"api/library/zipped/%@.zip", contentId];
    NSString *absoluteURLString = [[NSURL URLWithString:relativeURLString relativeToURL:self.baseURL] absoluteString];

    NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"GET" URLString:absoluteURLString parameters:nil];

    void (^successBlock)(AFHTTPRequestOperation *operation, id responseObject) = ^void(AFHTTPRequestOperation *operation, id responseObject) {

    };

    void (^failureBlock)(AFHTTPRequestOperation *operation,  NSError *error) = ^void(AFHTTPRequestOperation *operation,  NSError *error) {

    };

    AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:successBlock failure:failureBlock];

    NSOutputStream *outputStream = [NSOutputStream outputStreamToFileAtPath:destinationPath append:NO];
    operation.outputStream = outputStream;

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

    }];

    [self.operationQueue addOperation:operation];

    return operation;
}
Bumbling answered 17/10, 2013 at 18:21 Comment(0)
Q
3

try this...

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

AFHTTPRequestOperation *operation = [manager GET:urlString
                                      parameters:nil
                                         success:^(AFHTTPRequestOperation *operation, NSData *responseData)
                                     {
                                         [responseData writeToURL:someLocalURL atomically:YES];
                                     }
                                         failure:^(AFHTTPRequestOperation *operation, NSError *error)
                                     {
                                         NSLog(@"Downloading error: %@", error);
                                     }];

[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead)
 {
     float downloadPercentage = (float)totalBytesRead/(float)(totalBytesExpectedToRead);

     [someProgressView setProgress:downloadPercentage animated:YES];
 }];
Quelpart answered 8/7, 2014 at 12:42 Comment(0)
P
2

As you say AFURLSessionManager is only available in iOS 7(is backed by NSURLSession), so you should use the NSURLConnection based classes in AFNetworking 2.0 (AFHTTPRequestOperationManager, AFHTTPRequestOperation, etc).

Physical answered 10/10, 2013 at 12:31 Comment(3)
OK, found this... So to recap: in order to support the new NSURLSession APIs as well as the old-but-not-deprecated-and-still-useful NSURLConnection, the core components of AFNetworking 2.0 are split between request operation and session tasks. AFHTTPRequestOperationManager and AFHTTPSessionManager provide similar functionality, with nearly interchangeable interfaces that can be swapped out rather easily, should the need arise (such as porting between iOS 6 and 7).Hershelhershell
Do you have an example or tutorial for downloading files using AFHTTPRequestOperationManager?Hershelhershell
I don't but it should be straightforward, you just need to create a AFHTTPRequestOperation(you can use the manager's method HTTPRequestOperationWithRequest:success:failure:) and set its outputStream to one pointing to a file.Physical

© 2022 - 2024 — McMap. All rights reserved.