AFNetworking - get response content length - ios

I am using a simple AFNetworking [AFHTTPRequestOperation -initWithRequest:] for downloading file, I would like to check for the response header "content length" before I can actually download the file.
How can I check for response content length in AFNetworking ?

Using expectedContentLength Property of NSURLResponse You can you can find length.
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(#"Content-lent: %lld", [operation.response expectedContentLength]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];

Related

Ios-Afnetworking multiple download using selector method

Handle single download via afnetworking is good my question is that how handle multiple click on different button then it call this method then process break previous.
it is bcoz suppose several button hit at time then it confuse to download. how handle multiple download in selector method,if in array of batch download then it's easy but through which how .
-(void)downloadimagefromserver:(UIButton *)sender
{
int index =(int) sender.tag;
historyclass *class1 = [messages objectAtIndex:index];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:arrayOfStringsfinal[1]]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(#"success");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
[operation start];
}

AFNetworking 2 POST with Authentication challenge

Im using AFNetworking 2, with AFHTTPRequestOperation I can use for my Get
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:#"GET"];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.securityPolicy = securityPolicy;
[operation setWillSendRequestForAuthenticationChallengeBlock:
^(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge) {
//the certificate
}
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
DLog(#"operation :: %#", responseObject);
NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
DLog(#"operation :: %#", result);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
DLog(#"operation error :: %#", error);
}];
[operation start];
But now i need to use POST, with parameters,
I have problems finding how to set parameters on
AFHTTPRequestOperation
or finding how to set challenge block for
AFHTTPRequestOperationManager
how to have a POST with parameters and challenge block?
cheers
I am working now on the POST request. So far I've came up with the following code while trying to send an NSDictionary with POST method:
NSDictionary*packet = [NSDictionary dictionaryWithObjectsAndKeys: ......
[manager POST:path
parameters:packet
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
if ([responseObject isKindOfClass:[NSDictionary class]])
[self parseReceivedDataPacket:responseObject];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
Actually it works for me, apart from getting an "unacceptable content-type: text/html" when sending this data. But it gets received.
Hope this was useful.

Obj-C: Pass method arguments to nested block

I am using AFNetworking to consume some JSON data. I have to do this in a number of my view controllers. I'm trying to refactor my code and remove some duplication. I would like to know how I could pass, for instance, a NSArray object to the completion block of AFHTTPRequestOperation?
I have attempted the following to no avail.
-(void)request:(NSArray *)jsonArray
{
// ...
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:
^(AFHTTPRequestOperation *operation, id responseObject) {
jsonArray = responseObject;
dispatch_async(dispatch_get_main_queue(),
^{
[self.tableView reloadData];
}
);
}
failure:
^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Request Failed: %#, %#", error, error.userInfo);
}
];
[operation start];
}
It sounds like you want jsonArray to be an out method parameter so you can set its value within your method. With an asynchronous request like you have here this is heavily discouraged, but nonetheless.
Your method definition would change to:
-(void)request:(out NSArray **)jsonArray;
Calls to this method would change to:
[object request:&anArray];
And your completion handler would set the array with:
*jsonArray = responseObject;
For what it's worth, I agree with CrimsonChris who suggested using a callback block to handle this properly.
In case I understand you, I can advice you to do like this:
-(void)request:(NSArray *)jsonArray
compltition:(void(^)(NSDIctionary *response))complition {
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:
^(AFHTTPRequestOperation *operation, id responseObject) {
jsonArray = responseObject;
dispatch_async(dispatch_get_main_queue(),
^{
//returning response
complition(response);
[self.tableView reloadData];
}
);
}
failure:
^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Request Failed: %#, %#", error, error.userInfo);
}
];
[operation start];
}
and where you are calling this function:
[self request:jsonArray complition:^(NSDictionary *response) {
NSLog(#"Response = %#, response")
//and you can parse and reload your table
}];
I solved this by defining the function in a helper class, making jsonArray an instance variable of that class, instead of a function argument.

AFNetworking 2 downloading JPEG via POST straight into a File

I am downloading jpeg images via POST request using AFNetworking 2.0:
[manager.httpManager.requestSerializer setValue:#"image/jpeg" forHTTPHeaderField:#"Accept"];
NSDictionary *parameters = #{#"filename": imageName};
manager.httpManager.responseSerializer = [AFImageResponseSerializer serializer];
AFHTTPRequestOperation *operation = [manager.httpManager POST:URL_BASE_IMG parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"Image: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
Showing the responseObject is not an issue. However, how can I specify a path so that the response object is downloaded straight into it? I have seen the use of streams but I do not know how to implement it here. Anybody? Thanks in advance

AFNetworking 2.0 - How to download uiimage async

In ios using AFNetworking 2.0 how can I easily download a remote image asynchronously and cache it for future request of the same url? I'm looking for a convenient way to receive both error callbacks and successful ones.
Thanks
You can do
AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"Response: %#", responseObject);
_imageView.image = responseObject;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Image error: %#", error);
}];
[requestOperation start];
also mentioned How to download image with AFNetworking 2.0?

Resources