How do download a file with AFNetworking 2.0
Asked Answered
S

1

5

I am having a hard time sifting through search results for how to do this and I can't find anything concrete in the documentation. I just want to download a file and store in the Documents directory. The file is of type .plist, but I don't need to parse the plist. Like I said I just need to save it to disk.

Do I use AFHTTPRequestOperationManager for this?

This is what I've been trying in general:

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

If I use AFHTTPResponseSerializer I get this:

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

If I use AFPropertyListResponseSerializer I get this:

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

But I'm not even sure I'm using the correct API.

Sanitarium answered 23/11, 2013 at 6:10 Comment(5)
(This) [#8373161 might help.Forland
That uses the old version of AFNetworking though. Is that still the recommended way?Sanitarium
Tried that and I still get Error: Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: internal server error (500)Sanitarium
Recommended way for downloading files with AFNetworking 2.0 - github.com/AFNetworking/AFNetworking#creating-a-download-taskIre
#19296196Forland
D
15

Please, try this example. Hope this help!

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

Something else: What's the best way to find the user's Documents directory on an iPhone?

Decoder answered 26/6, 2014 at 7:4 Comment(6)
Write some explanation tooo.Gradate
Sorry for my bad style in answers :) Now this better?Decoder
can u please tell me what if same request will execute multiple time?can we check before starting the download that if there is already running the process for same request?Carolecarolee
Good question! But, sorry, I don't know answer for this question. But I think you can check exists or not exists file at 'pathToFile'Decoder
@BhavikKama, why can't we add next request to a queue and set maxConcurrentOperationCount ?Diacritic
how to call Get type method and pass some header data to download a fileDemonetize

© 2022 - 2024 — McMap. All rights reserved.