AFNetworking - call function inside - ios

I'm using AFNetworking and i have a question about it, i want call one different function when return value correct
- (AFHTTPRequestOperation *)GET:(NSString *)URLString
parameters:(id)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithHTTPMethod:#"GET" URLString:URLString parameters:parameters success:^(AFHTTPRequestOperation
*requestOperation, __unused id responseObject) {
if (success) {
int status = [[responseObject objectForKey:#"Status"] intValue];
else if(status == -1)
{
[self HTTPRequestOperationWithHTTPMethod:#"GET" URLString:URLString parameters:parameters success:^(AFHTTPRequestOperation *requestOperation, __unused id responseObject) {
if (success) {
NSLog(#"adfasdfasdfasdf");
success(requestOperation,responseObject);
}
} failure:^(AFHTTPRequestOperation * operation, NSError *error) {
NSLog(#"ERROR: %#", error);
failure(nil, error);
}];
}
else
success(requestOperation,responseObject);
}
} failure:failure];
operation.securityPolicy.allowInvalidCertificates = YES;
operation.securityPolicy.validatesDomainName = NO;
[self.operationQueue addOperation:operation];
return operation;
}
when status = -1. I'll call more function with param above, but dosen't work.
else if(status == -1)
{
[self HTTPRequestOperationWithHTTPMethod:#"GET" URLString:URLString parameters:parameters success:^(AFHTTPRequestOperation *requestOperation, __unused id responseObject) {
if (success) {
NSLog(#"adfasdfasdfasdf");
success(requestOperation,responseObject);
}
} failure:^(AFHTTPRequestOperation * operation, NSError *error) {
NSLog(#"ERROR: %#", error);
failure(nil, error);
}];
}
run app and I bug function not call to success or failure.

You don't need to check success of failure with condition in AFNetworking. AFNetworking provide block.
Follow below code :
- (AFHTTPRequestOperation *)GET:(NSString *)URLString
parameters:(id)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithHTTPMethod:#"GET" URLString:URLString parameters:parameters success:^(AFHTTPRequestOperation
*requestOperation, __unused id responseObject) {
int status = [[responseObject objectForKey:#"Status"] intValue];
if(status == -1)
{
[self HTTPRequestOperationWithHTTPMethod:#"GET" URLString:URLString parameters:parameters success:^(AFHTTPRequestOperation *requestOperation, __unused id responseObject) {
NSLog(#"adfasdfasdfasdf");
success(requestOperation,responseObject);
} failure:^(AFHTTPRequestOperation * operation, NSError *error) {
NSLog(#"ERROR: %#", error);
failure(nil, error);
}];
}
else
success(requestOperation,responseObject);
}
} failure:failure];
operation.securityPolicy.allowInvalidCertificates = YES;
operation.securityPolicy.validatesDomainName = NO;
[self.operationQueue addOperation:operation];
return operation;
}

if (success) {
int status = [[responseObject objectForKey:#"Status"] intValue];
else if(status == -1)
{
are you sure this if and else if are a pair?

Related

Timed Out while using AFHTTPSessionOperation

Can anybody tell me why my call "timed out"? My app just hangs there, the success:^(NSURLSessionTask* operation, id response) section of the folowing code was never executed.
return [self beginRequestController:#"myController" action:#"myAction" parameters:parameters
success:^(NSURLSessionTask* operation, id response)
{
NSLog(#"This is NOT being called --->>>: %#", response);
} failure:^(NSURLSessionTask* operation, NSError* error)
{
//Handle the error
}];
- (NSOperation*) beginRequestController:(NSString*)controller action: (NSString*)action parameters:(NSDictionary*)parameters success: (RequestSuccess)success failure:(RequestFailure)failure
{
NSOperation *operation = [AFHTTPSessionOperation operationWithManager:manager HTTPMethod:#"POST" URLString:urlString parameters:parameters uploadProgress:nil downloadProgress: nil success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(#"Reponse --->>>: %#", responseObject );
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(#"Error --->>>: %#", error);
}];
[self.operationQueue addOperation:operation];
return operation;
}
You're passing blocks to beginRequestController, but that method doesn't do anything with them. You want to call those blocks. E.g.
- (NSOperation *)beginRequestController:(NSString *)controller action:(NSString *)action parameters:(NSDictionary *)parameters success:(RequestSuccess)success failure:(RequestFailure)failure {
NSOperation *operation = [AFHTTPSessionOperation operationWithManager:manager HTTPMethod:#"POST" URLString:urlString parameters:parameters uploadProgress:nil downloadProgress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(#"Response --->>>: %#", responseObject);
if (success)
success(task, responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(#"Error --->>>: %#", error);
if (failure)
failure(task, error);
}];
[self.operationQueue addOperation:operation];
return operation;
}
Or, even simpler:
- (NSOperation *)beginRequestController:(NSString *)controller action:(NSString *)action parameters:(NSDictionary *)parameters success:(RequestSuccess)success failure:(RequestFailure)failure {
NSOperation *operation = [AFHTTPSessionOperation operationWithManager:manager HTTPMethod:#"POST" URLString:urlString parameters:parameters uploadProgress:nil downloadProgress: nil success:success failure:failure];
[self.operationQueue addOperation:operation];
return operation;
}

AFNetworking timeout is not working

I am using AFNetworking V2.5.4.
The default timeout for AFN is 60s, this is too long for my app. I want to set it as 20s.
So I tried follow code manager.requestSerializer.timeoutInterval = 20;
But it's not working. I searched some post saying I need add [manager.requestSerializer willChangeValueForKey:#"timeoutInterval"];
manager.requestSerializer.timeoutInterval = 3;
[manager.requestSerializer didChangeValueForKey:#"timeoutInterval"];
Still, it's not working.
Follow is my code piece
+ (AFHTTPRequestOperation *)sendRequestWithRequest:(NSMutableURLRequest*) request
manager:(AFHTTPRequestOperationManager *)manager
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure{
[manager.requestSerializer willChangeValueForKey:#"timeoutInterval"];
manager.requestSerializer.timeoutInterval = 3;
[manager.requestSerializer didChangeValueForKey:#"timeoutInterval"];
NSLog(#"manager %#, start time %lf",manager, [[NSDate date] timeIntervalSince1970]);
AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (operation.response.statusCode) {
success(operation, responseObject);
}else{
failure(operation, nil);
}
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failure(operation, error);
}];
[manager.operationQueue addOperation:operation];
return operation;}

How can we pass the parameters along with block methods in objecitve-c

I want to pass the 'NSString' as a parameter for as URLString in below method, how can we do that
-(void)makeServiceCallSuccess:(void (^)(NSDictionary *response))success
failure:(void (^)(NSError *error))failure {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:URLString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
response = (NSDictionary *)responseObject;
success(response);
NSLog(#"JSON: %#", response);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failure(error);
NSLog(#"Error: %#", error);
}];
}
simply add parameter in method, as usually we are doing like
-(void)makeServiceCallSuccessWithURLString:(NSString*)urlString withResponseHandler:(void (^)(NSDictionary *response))success
failure:(void (^)(NSError *error))failure
There is no wat to pass a paramether in that method as it, you can edit it and add a parameter or read a value from aproperty
-(void)makeServiceCallSuccess:(void (^)(NSDictionary *response))success
failure:(void (^)(NSError *error))failure {
NSString* myString = self.myProperty;
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:myString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
response = (NSDictionary *)responseObject;
success(response);
NSLog(#"JSON: %#", response);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failure(error);
NSLog(#"Error: %#", error);
}];
}

iOS Block issue with AFNetworking

I'm currently learning blocks, and I want to use them with AFNetworking. I made method in Singletone:
- (void)getAllPlayers:(void (^)(NSArray *playerz, bool succeed))blockPlayers {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:#"https://api.myjson.com/bins/530re" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
}
I called this method in viewDidLoad:
[[Manager sharedInstance]getAllPlayers:^(NSArray *playerz, bool succeed) {
if (succeed == YES) {
self.allClubs = playerz;
[self.tableView reloadData];
}
}];
But nothing is downloaded.
Yo forgot to call the blockPlayers Completion handler.
- (void)getAllPlayers:(void (^)(NSArray *playerz, bool succeed))blockPlayers {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:#"https://api.myjson.com/bins/530re" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSarray *returnedPLayerz = [NSArray array];
//Here treat operation and response Object to extract playerz and assing it to returnedPlayerz
blockPLayers(returnedPlayerz, YES);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
blockPlayers(nil, NO);
NSLog(#"Error: %#", error);
}];
}
You got data from server in response object. After that you need to parse it and return in block:
- (void)getAllPlayers:(void (^)(NSArray *playerz, bool succeed))blockPlayers {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:#"https://api.myjson.com/bins/530re" parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSArray* players = [self getPlayersFromJson:responseObject];
blockPlayers(players, YES);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
blockPlayers(nil, NO);
}];
}
So you need to create parser method getPlayersFromJson

AFNetworking 2.0 : AFHTTPRequestOperationManager asynchronous methode invoke

I am using AFNetworking 2.0 for networking of my application, I have implemented a method to check the login of a user
-(NSString *) loginWith : (NSString *) email andPassword :(NSString *) password
{
__block NSString * result ;
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *params = #{#"email": email, #"password": password};
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:[kROOT_URL stringByAppendingString:#"auth/"] parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(#"JSON: %#", (NSDictionary*)responseObject);
[[NSUserDefaults standardUserDefaults] setObject:responseObject forKey:USER_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
result = [responseObject objectForKey:#"id"];
NSLog(#"result :: %#",result);
} failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(#"Error: %#", error);
result = nil;
}];
return result;
}
and from other view controller I am invoking this method :
NSString * result = [self.user loginWith:emailCell.textField.text andPassword:passCell.textField.text];
NSLog(#"Result: %#",result);
the problem is that I get "null" as the object of result, This is because that when the NSlog is executed the network process was doing in background thread, so how I correct the implementation, so then I can get the result in the right moment, after fetching
Thank you
You should add a “callback“ block parameter to your method signature, and call that block when the request is done.
- (void)loginWith:(NSString *)email andPassword:(NSString *)password complete:(void(^)(id result, NSError *error))block {
// ...
[manager POST:[kROOT_URL stringByAppendingString:#"auth/"] parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject)
{
// ...
result = [responseObject objectForKey:#"id"];
if (block) block(result, nil)
} failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
if (block) block(nil, error)
}];
}
When calling the method, pass the callback block where you can read the result value:
[self.user loginWith:emailCell.textField.text andPassword:passCell.textField.text complete:^(id result, NSError *error) {
if (error)
NSLog(#"Error: %#", error);
else
NSLog(#"Result: %#", result);
}];

Resources