AFNetworking AFHTTPClient Class

I am new to iOS programming, especially when it comes to web services. I am developing an application for academic purposes, and I need to communicate with my server currently using AFNetworking2 and Restler / php, everything works when it comes to GET methods. But I can’t download anything.

I read for hours on the github support site, stackoverflow, almost all examples / questions for uploading images (and there are LOTs) use this line:

AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://server"]]; 

I have a Client class, a subclass of AFHTTPSessionManager, with my sharedClient. But all examples use this AFHTTPClient with initWithURL and other classes such as AFJSONRequestOperation, which I can no longer find.

This basically suggests that I should create a singleton subclass of AFHTTPClient, but I cannot find it anywhere. Some links even send me to the official github repository, but it is no longer available. So my question is: where can I get more information about AFHTTPClient, should I use it, can someone tell me a tutorial on how to create or at least understand its functionality.

Greetings

+5
source share
3 answers

In AFNetworking 2.0, AFHTTPClient has been replaced by AFHTTPRequestOperationManager / AFHTTPSessionManager. I would suggest you refer to an example in git them . git clone and open in Xcode. This should help you. This is the most updated example.

If you want to use the code AFHTTPClient ie 1.x. Here is the git link to the branch . The pod specification for this would be

pod 'AFNetworking', '~> 1.3.3'

In 2.0 AFNetworking, you can create a single-user client like this.

Interface

 @interface AFAppDotNetAPIClient : AFHTTPSessionManager + (instancetype)sharedClient; @end 

Implementation

 #import "AFAppDotNetAPIClient.h" static NSString * const AFAppDotNetAPIBaseURLString = @"https://alpha-api.app.net/"; @implementation AFAppDotNetAPIClient + (instancetype)sharedClient { static AFAppDotNetAPIClient *_sharedClient = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _sharedClient = [[AFAppDotNetAPIClient alloc] initWithBaseURL:[NSURL URLWithString:AFAppDotNetAPIBaseURLString]]; [_sharedClient setSecurityPolicy:[AFSecurityPolicy policyWithPinningMode:AFSSLPinningModePublicKey]]; }); return _sharedClient; } @end 
+23
source

AFHTTPClient is a class from AFNetworking 1.x - https://github.com/AFNetworking/AFNetworking/tree/1.x

AFNetworking 2.0 is a fairly new library, so there are not too many tutorials on this subject, as long as you can still use the first version, until you feel that there is time to learn 2.x))

Hope helps

+2
source

Here is the solution modified for the latest version of AFNetworking.

 //sample PNG NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"700k_image.png"]); AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager POST:WEBSERVICE_IMAGEM_UPLOAD parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { [formData appendPartWithFileData:imageData name:@"image" fileName:@"image_name" mimeType:@"image/png"]; } success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Success: %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }]; 
+2
source

Source: https://habr.com/ru/post/958451/


All Articles