SOLVED: the filename was an autoreleased string no longer available when called at createFileAtPath:
I’m trying to track the progress of the download of a file and the code I’m trying to implement is this (edited):
connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
if (connection)
receivedData = [[NSMutableData data] retain];
-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receivedData setLength:0];
totalBytes = [[NSNumber numberWithLongLong:[response expectedContentLength]] intValue];
NSLog(@"content-length: %i bytes", totalBytes);
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
int resourceLength = [[NSNumber numberWithUnsignedInteger:[receivedData length]] intValue];
NSLog(@"receivedData length: %i", resourceLength);
}
-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
[fileMgr createFileAtPath:filename contents:receivedData attributes:nil];
//if instead i write only the Apple example:
//NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
//there's no SIGABRT
[receivedData release];
[connection release];
}
but the createFileAtPath:contents:attributes: just gives SIGABRT:
-[__NSCFData
getFileSystemRepresentation:maxLength:]:
unrecognized selector sent to instance
0x2ba510* Terminating app due to uncaught exception
‘NSInvalidArgumentException’, reason:
‘-[__NSCFData
getFileSystemRepresentation:maxLength:]:
unrecognized selector sent to instance
0x2ba510’
what am i doing wrong? isn’t this the way to download a file asynchronously ?
2 more things: a) the content-length NSLog is correct. b) if I don’t initWithCapacity:content-lenght in didReceiveResponse and just init, the receivedData length only grows about two times the content-length…
SOLVED: the filename was an autoreleased string no longer available when called at createFileAtPath: