我正在尝试为post方法设置nsurlconection的进度条, 请查看代码和指南以设置进度条,我正在尝试本网站提供的所有可能方式。
-(void)getCustomerDetails:(NSMutableDictionary *)tempDir
{
self.downloadedMutableData = [[NSMutableData alloc] init];
NSURL *url = [NSURL URLWithString:[CommonFunctions getPListFileValue:@"wsgetcustomers"]];
NSError *error = nil;
[tempDir setObject:[self md5:[CommonFunctions getPListFileValue:@"udid"]] forKey:@"encryptedvenderid"];
[tempDir setObject:[CommonFunctions getPListFileValue:@"username"] forKey:@"username"];
[tempDir setObject:[CommonFunctions getPListFileValue:@"password"] forKey:@"password"];
[tempDir setObject:[CommonFunctions getPListFileValue:@"version"] forKey:@"version"];
NSData *postData = [NSJSONSerialization dataWithJSONObject:tempDir
options:kNilOptions
error:&error];
// Create the request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"json" forHTTPHeaderField:@"Data-Type"];
[request setValue:@"no-cache" forHTTPHeaderField:@"cache-control"];
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[postData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
self.connectionManager = [[NSURLConnection alloc] initWithRequest:request
delegate:self];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"%lld", response.expectedContentLength);
self.urlResponse = response;
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.downloadedMutableData appendData:data];
self.progressBar.progress = ((100.0/self.urlResponse.expectedContentLength)*self.downloadedMutableData.length)/100;
NSLog(@"%.0f%%", ((100.0/self.urlResponse.expectedContentLength)*self.downloadedMutableData.length));
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
self.progressBar.hidden = YES;
NSLog(@"Succeeded! Received %lu bytes of data", (unsigned long)[self.downloadedMutableData length]);
NSDictionary *Response = [[[NSString alloc]initWithData:self.downloadedMutableData encoding:NSUTF8StringEncoding] JSONValue];
NSDictionary *FinalDict = [[NSString stringWithFormat:@"%@",[Response objectForKey:@"d"]] JSONValue];
NSLog(@"%@",FinalDict);
}
在此代码中,在getCustomerDetails中,它不计算数据进入didrecievedata方法并直接导航到didfinish方法。
请帮我在项目中添加进度条。
答案 0 :(得分:1)
试试这个方法
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
{
float prog = (totalBytesWritten / (totalBytesExpectedToWrite * 1.0f) * 100);
[self.progBar setProgress:prog];
}
进展数据