IOS uses Block secondary encapsulation AFNetworking 3.0 details

  • 2021-11-10 11:04:08
  • OfStack

IOS uses Block2 to encapsulate AFNetworking 3.0

At present, most of our network requests use the third-party tool-AFNetworking;

Among them, AFNetworking 3.0 encapsulates NSURLSession, which simplifies many steps. However, in real development, we can encapsulate AFNetworking into a class again, so that we can make network requests by passing URL and Parameters.

Specific implementation steps:

Pre-preparation: Import the AFNetworking third-party framework

1. Create a new tool class that inherits from NSObject;;

2. Macro define two block in. h, and request the result with callback;


// Macro definition succeeded block  Information after successful callback 
typedef void (^HttpSuccess)(id data);
// Macro definition failed block  Callback failure information 
typedef void (^HttpFailure)(NSError *error);

3. Create two GET and POST request functions using class methods;


//get Request 
+(void)getWithUrlString:(NSString *)urlString success:(HttpSuccess)success failure:(HttpFailure)failure;


//post Request 
+(void)postWithUrlString:(NSString *)urlString parameters:(NSDictionary *)parameters success:(HttpSuccess)success failure:(HttpFailure)failure;

4. # import "AFNetworking/AFNetworking. h" in. m, and implement two functions defined in. h;


//GET Request 
+(void)getWithUrlString:(NSString *)urlString success:(HttpSuccess)success failure:(HttpFailure)failure{
  // Create a request manager 
  AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

  manager.responseSerializer = [AFHTTPResponseSerializer serializer];
  // Content type 
  manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html", nil];
  //get Request 
  [manager GET:urlString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    // Progress of data request 
  } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
    success(responseObject);
  } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    failure(error);
  }];

}

//POST Request 
+(void)postWithUrlString:(NSString *)urlString parameters:(NSDictionary *)parameters success:(HttpSuccess)success failure:(HttpFailure)failure{
  // Create a request manager 
  AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
  //
  manager.responseSerializer = [AFHTTPResponseSerializer serializer];
  // Content type 
  manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html", nil];
  //post Request 
  [manager POST:urlString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
    // Progress of data request 
  } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
    success(responseObject);
  } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    failure(error);
  }];
}

Use steps:

1. Import the custom tool class name;

2. Call custom methods;


  [XMAFNRequestTools getWithUrlString:url success:^(id data) {
    self.array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments | NSJSONReadingMutableLeaves error:nil];
    NSLog(@"%@",self.array);
  } failure:^(NSError *error) {
    NSLog(@"%@",error);
  }];

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: