可以使用AFNetworking上传和MBProgressHUD的进度显示
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:urlLink]];
NSMutableURLRequest *request;
request = [httpClient multipartFormRequestWithMethod:@"POST"
path:nil
parameters:postDictionary
constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
}];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeDeterminate;
hud.labelText = @"Uploading";
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
float uploadPercentge = (float)totalBytesWritten/(float)totalBytesExpectedToWrite;
float uploadActualPercentage = uploadPercentge * 100;
hud.progress = uploadPercentge;
if (uploadActualPercentage >= 100) {
hud.mode = MBProgressHUDModeText;
hud.labelText = [NSString stringWithFormat:@"Waiting for response"];
}
}];
[httpClient enqueueHTTPRequestOperation:operation];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[hud hide:YES];
NSLog(@"Success %@", operation.responseString);
NSDictionary *message = [NSJSONSerialization JSONObjectWithData:[operation.responseString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil];
DLog(@"%@",message);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error: %@", operation.responseString);
NSLog(@"%@",error);
}];
[operation start];
通过参数你的意思是请求头? – Macondo2Seattle
你可以开始使用非常容易使用包装类的[AFNetworking](https://github.com/AFNetworking/AFNetworking)。 –