Reputation: 1116
Since I am new to IOS and AFNetworking 3,0 is new, I don't know how to retrieve data from AFHTTPSessionManager. I have to following message and I want to return the result
- (NSString *) makeServiceCall;
{
NSString *response = @"";
@try {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager
POST:self.url.absoluteString
parameters:self.parameters
progress:nil
success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"Success: %@", responseObject);}
failure:^(NSURLSessionDataTask * task, NSError * error) {
NSLog(@"Error: %@", error);
}];
[AFHTTPSessionManager manager].securityPolicy.allowInvalidCertificates = YES;
}
@catch (NSException *exception) {
NSLog(@"%@", exception.reason);
}
}
Upvotes: 1
Views: 1713
Reputation: 77641
The method AFHTTPSessionManager POST:parameters:progress:success:failure:
is an asynchronous method.
What you are trying to do is return a string from the method calling it. This will not work as the method will finish before the download has started.
You need to call this with a completion block something like this...
- (void)getStringWithCompletionHandler:(void (^)(id))completion {
NSLog(@"Method started");
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager
POST:self.url.absoluteString
parameters:self.parameters
progress:^(NSProgress * _Nonnull uploadProgress) {
NSLog(@"Download underway");
}
success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"Download successful");
completion(responseObject);
}
failure:^(NSURLSessionDataTask * task, NSError * error) {
NSLog(@"Error");
}];
// trying to return a string here won't work because the download hasn't finished yet.
// You can see the order of things happening by adding logs...
NSLog(@"Method finished");
}
The order of the logs in this code will be...
As you can see, trying to return at the end of the method won't work because the download won't have completed yet.
Upvotes: 1