gpt4 book ai didi

ios - 使用 Post 方法为 NSURLConnection 设置进度条

转载 作者:行者123 更新时间:2023-11-29 12:19:12 24 4
gpt4 key购买 nike

我正在尝试为 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中,并没有计算在diredecievedata方法中获取的数据,而是直接导航到didfinish方法。

请帮我在我的项目中添加进度条。

最佳答案

试试这个方法

 - (void)connection:(NSURLConnection *)connection  didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
{
float prog = (totalBytesWritten / (totalBytesExpectedToWrite * 1.0f) * 100);
[self.progBar setProgress:prog];

}

为了数据的进步

关于ios - 使用 Post 方法为 NSURLConnection 设置进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31135808/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com