I am trying to download multiple images from a URL stored in an XML feed. Getting the image urls from the XML is working correctly. However, the NSURLConnection is creating empty files, but the data is received as noted in NSLog. In connectionDidFinishLoading:(NSURLConnection *)connection
, the data and correct bytes are received, the problem is how do I make the receivedData write to the correct file.
Semi-working code:
-(void)parsingComplete:(XMLDataSource*)theParser
{
/* iterate through the Categories and create the
sub-directory if it does not exist
*/
for (int i = 0; i < [categories count]; i++) {
NSString *cat = [NSString stringWithFormat:@"%@/%@",BASE_DIR,[[categories objectAtIndex:i] objectForKey:@"name"]];
NSString *catName = [[categories objectAtIndex:i] objectForKey:@"name"];
NSArray *catArray = [[categories objectAtIndex:i] objectForKey:@"images"];
/* create the sub-direcotry naming it the #category# key */
if (![FILEMANAGER fileExistsAtPath:cat]) {
[FILEMANAGER createDirectoryAtPath:cat withIntermediateDirectories:NO attributes:nil error:nil];
}
//NSLog(@"\n\nCategory: %@",cat);
for (int x = 0; x < [catArray count]; x++) {
//NSLog(@"Image: %@",[[catArray objectAtIndex:x] objectForKey:@"imageUrl"]);
/* download each file to the corresponding category sub-directory */
fileOut = [NSString stringWithFormat:@"%@/%@_0%i.jpg",cat,catName,x];
NSURLRequest *imageRequest =
[NSURLRequest requestWithURL:[NSURL URLWithString:[[catArray objectAtIndex:x] objectForKey:@"imageUrl"]]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30.0];
NSURLConnection *imageConnection = [[NSURLConnection alloc] initWithRequest:imageRequest delegate:self];
int counter = 0;
//BOOL result = NO;
if(imageConnection)
{
NSLog(@"Counter: %i",counter++);
receivedData = [[NSMutableData data] retain];
/*result = */[receivedData writeToFile:fileOut atomically:YES];
}
/*
if (!result) NSLog(@"Failed"); else NSLog(@"Successful");
*/
}
}
}
#pragma mark NSURLConenction
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// release the connection, and the data object
[connection release];
// receivedData is declared as a method instance elsewhere
[receivedData release];
// inform the user
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// do something with the data
// receivedData is declared as a method instance elsewhere
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
NSString *aStr = [[NSString alloc] initWithData:receivedData encoding:NSASCIIStringEncoding];
// release the connection, and the data object
//[receivedData release];
}