Hi i want to show the received bytes in the console.
(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[receivedData appendData:data];
NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[receivedData length]];
NSLog(@"resourceData length: %d ", [resourceLength intValue]);
if (file) {
[file seekToEndOfFile];
}
[file writeData:data];
}
the file successfully downloads and safed it into docoument dir but the nslog show always
resourceData length: 0
receivedata is NSMutableData *receivedData; in the .h file
the strange thing is that the exceptedbytes work well. here the code for that:
- (void)connection:(NSURLConnection )connection didReceiveResponse:(NSURLResponse)response { localFilename = [[[url2 absoluteString] lastPathComponent] copy]; NSLog(localFilename); NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0] ; NSString *appFile = [documentsDirectory stringByAppendingPathComponent:localFilename]; [[NSFileManager defaultManager] createFileAtPath:appFile contents:nil attributes:nil];expectedBytes = [response expectedContentLength]; NSLog(@"content-length: %lli Bytes", expectedBytes); file = [[NSFileHandle fileHandleForUpdatingAtPath:appFile] retain];
if (file) {
[file seekToEndOfFile]; }}
can somebody help me. i really dont know what i am doing wrong
kind regards
tammo
Have you allocated and initialised
receivedData? One usually doesright after creating an
NSURLConnectioninstance, and thenupon receiving a response via
-connection:didReceiveResponse:, andupon receiving data via
-connection:didReceiveData:. This is exemplified in the URL Loading System Programming Guide. If you haven’t instantiatedreceivedData, it is probablyniland[receivedData length]returns 0 in that case.