I thought I had it figured out but I just can't get it to work. I have a method that is called on every URL in an array. This method have a URL of a picture that should be downloaded in specific path in a Application Support folder for offline use. But maybe I'm misinterpreting the methods in the AFNetwork library. My method looks like this:
- (void) downloadImageInBackground:(NSDictionary *)args{
@autoreleasepool {
NSString *photourl = [args objectForKey:@"photoUrl"];
NSString *articleID = [args objectForKey:@"articleID"];
NSString *guideName = [args objectForKey:@"guideName"];
NSNumber *totalNumberOfImages = [args objectForKey:@"totalNumberOfImages"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:photourl]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.inputStream = [NSInputStream inputStreamWithURL:[NSURL URLWithString:photourl]];
[operation setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{
DLog(@"PROBLEMS_AF");
}];
DLog(@"URL_PHOTOURL: %@", photourl);
DLog(@"indexSet: %@", operation.hasAcceptableStatusCode);
[operation response];
NSData *data = [args objectForKey:@"data"];
NSString *path;
path = [NSMutableString stringWithFormat:@"%@/Library/Application Support/Guides", NSHomeDirectory()];
path = [path stringByAppendingPathComponent:guideName];
NSString *guidePath = path;
path = [path stringByAppendingPathComponent:photourl];
if ([[NSFileManager defaultManager] fileExistsAtPath:guidePath]){
[[NSFileManager defaultManager] createFileAtPath:path
contents:data
attributes:nil];
}
DLog(@"path: %@", path);
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
[operation start];
DLog(@"isExecuting: %d",[operation isExecuting]);
DLog(@"IS_FINISHED: %d",[operation isFinished]);
}
}
PhotoURL is the direct link to the image that I want to download.
Since this method is called for all the images, all logs is called several times, and seems to be correct.