The first harvest and thought of reading AFNetworking source code

Keywords: Mobile Session PHP github JSON

Source address:

https://github.com/AFNetworking/AFNetworking/tree/0.2.1

First, add the corresponding library to your Demo, set the MRC and set the new syntax according to the prompts, so that the project can run: Including support for HTTP:

1. Add NSAppTransportSecurity type Dictionary in Info.plist;

2. Under NSAppTransportSecurity, add nsallowsarbitrryloads type Boolean, and set the value to YES;

Let's first analyze how AFNetworking achieves get requests:


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://ip.taobao.com/service/getIpInfo.php?ip=myip"]];
    AFHTTPOperationCallback *callback = [AFHTTPOperationCallback callbackWithSuccess:^(NSURLRequest *request, NSHTTPURLResponse *response, NSDictionary *data) {
        NSLog(@"data: %@",response);
        response.
    }];
    //    [AFHTTPOperation operationWithRequest:request callback:callback];
    AFHTTPOperation *operation = [AFHTTPOperation operationWithRequest:request callback:callback];
    [operation start];
}

Let's take a look at how to handle the get request directly by Apple's native without using a third party?


- (void)getRequest {
    
    //1. First, you need to have the link path of get request:
    NSURL *url = [NSURL URLWithString: @"http://ip.taobao.com/service/getIpInfo.php?ip=myip"];
    
    //2. Create request object
    //The request object contains the request header and request method (GET) by default
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    //3. Get the session object
    
    NSURLSession *session = [NSURLSession sharedSession];
    
    //4. Create a task according to the session object (send request)
    /*
     
          First parameter: request object
     
          Second parameter: completionHandler callback
     
          data: Response body information (expected data)
     
          response: Response header information, mainly the description of the server
     
          error: Error message. If the request fails, the error has a value
     
          */
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error == nil) {
            //6. Analyze the data returned by the server
            //Note: (the data returned here is in JSON format, so NSJSONSerialization is used for deserialization)
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
            //            successBlock(dict);
            NSLog(@"dict%@",dict);
        }
    }];
    //5. Perform tasks
    
    [dataTask resume];
}

I'm not very familiar with the principle of network request, so I can only see the number of native and AFNetworking codes by comparing the dialogue.

There are many things to think about, so first look at the file directory structure:

Posted by crazylegseddie on Sun, 03 Nov 2019 14:09:22 -0800