I have two classes, one with(Class:JSONClass) NSURLConnection delegate
methods and the other with progress bar(Class :ProgressbarClass)
. When googled "-(void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite"
method should be used for updating the progress bar. For that the delegate method should be called regularly , but this method is called only once and displays the bytesWritten,totalBytesExpectedToWrite,totalBytesWritten same,
-(void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite{
NSLog(@"bytesWritten %ld",(long)bytesWritten);
NSLog(@"totalBytesWritten %ld",(long)totalBytesWritten);
NSLog(@"totalBytesExpectedToWrite %ld",(long)totalBytesExpectedToWrite);
progressedIndicator = ((float)totalBytesWritten / (float)totalBytesExpectedToWrite);
NSLog(@"DELEGATE PROGRESS INDICATOR %f",progressedIndicator);
[objUpdatesController updateProgressBar:progressedIndicator];
}
The above method is called only once. The variable "progressedIndicator" always shows "1" as totalBytesWritten and totalBytesExpectedToWrite will contain the same value!
Any help is appreciated.
This is how I send the data
NSDictionary *bodyDictionary = [NSDictionary dictionaryWithObjects:values forKeys:keys];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:bodyDictionary options:0 error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
body = [NSString stringWithFormat:@"message=%@",jsonString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
[request setTimeoutInterval:300];
[request setValue:[NSString stringWithFormat:@"%d", [body length]] forHTTPHeaderField:@"Content-Length"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[connection start];