0

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];
Ori Lentz
  • 3,668
  • 6
  • 22
  • 28
user3162102
  • 105
  • 1
  • 10
  • Can you show us how do you upload data to the server? How much data are we talking about? 2kb? 500MB? – 4oby Dec 30 '15 at 10:54
  • 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]; – user3162102 Dec 30 '15 at 12:25
  • Continued ..... NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]]; [request setHTTPMethod:@"POST"];[request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]]; [request setTimeoutInterval:300]; [request setValue:@"3" forHTTPHeaderField:@"eda_device_type_id"][request setValue:@"10" forHTTPHeaderField:@"eda_device_id"]; [request setValue:[NSString stringWithFormat:@"%d", [body length]] forHTTPHeaderField:@"Content-Length"];NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self]; – user3162102 Dec 30 '15 at 12:25
  • could you please update your question and insert the code there, cause this is unreadable. – 4oby Dec 30 '15 at 12:26

3 Answers3

0

Here is the sample code for Header fileds: 3,4 are our custom headers which is given by client..Try this..I am not sure..but it may helps you..

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];    
[request setValue:@" " forHTTPHeaderField:@" "];
[request setValue:@" " forHTTPHeaderField:@" "];
[request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)[jsonData length]] forHTTPHeaderField:@"Content-Length"];
0

I was able to replicate the situation by sending a small dictionary.
It seems that the data you have is sent in one "chunk" this is why the function is called only once.

as stated in apple docs

- connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:
Sent as the body (message data) of a request is transmitted (such as in an http POST request).

and

The value of totalBytesExpectedToWrite may change during the upload if the request needs to be retransmitted due to a lost connection or an authentication challenge from the server.

So if it all goes well in a single "chunk" it's called only once.

Or you could always use AFNetworking

Community
  • 1
  • 1
4oby
  • 607
  • 10
  • 19
  • Thanks for reply, but there is a time gap between the buttonclicked and these NSURLConnection methods been called.How this unknow =n time can be filled with the progress bar? – user3162102 Dec 31 '15 at 11:13
  • You just fake it. :) set progres bar +1% say every 0.05 sec, or what ever time interval suits you, up until, say, 95%, at that point just wait until the `NSURLConnection` method is fired – 4oby Dec 31 '15 at 20:42
  • Thanks @4oby. will try faking it for time gap between request and NSURLConnection Methods. I am facing one more issue , iam trying the same NSURLmethods for downloading for which i am trying to find expected bytes by using the property [response expectedContentLength]in didreceiveresponse but it is showing -1. Please suggest – user3162102 Jan 01 '16 at 06:30
0

You Can Also Use 3rd Party ProgressBar

https://github.com/Marxon13/M13ProgressSuite

Community
  • 1
  • 1
chirag bhalara
  • 203
  • 1
  • 10