Comparison of several methods to get data from Internet in IOS


There are three ways to obtain network data in IOS: 1. Nsurlcondition 2. Nsurlsession 3. Three party library afnetworking

Nsurlsession is Apple’s substitute for nsulrcondition. Nsurlsession is more than nsulrcondition

1. Configurable data information: nsurlsession configuration. Nsurlsession configuration enables you to set the data you want to request. For general settings such as caching, you can also use the default configuration information: defaultconfiguration

2. Nsurlsession is actually a dialog. Each time you create a dialog, you need to create and start a task. There are three kinds of tasks in nsurlsession: nsurlsession datatask (request data task), nsurldownloadtask (download file), nsurluploadtask (upload data, get, POST1, post2). Each task has one or two different ways to choose from, and those who want to know can try them one by one.

3. Breakpoint continuation

Because nsulrcondition and nsurlsession need to write a lot of non reusable and complex request header information when sending post2 requests, some people who are very proficient in IOS encapsulate the three-party library afnetworking on the basis of nsulrcondition. When Apple publishes nsurlsession, afnetworking also updates the encapsulation of nsurlsession.

So it is very convenient to send post2 request in afnetworking. Some codes are pasted below:

[manager POST:urlStr parameters:data constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    NSString *path=[[NSBundle mainBundle] pathForResource:@"image.png" ofType:@""];
    [formData appendPartWithFileURL:[NSURL URLW ithString:path ] name:@"image" fileName:@" image.png " mimeType:@"image/jpg"  error:nil ]; // this sentence directly encapsulates the first-class information required by the two types of post requests. Those who are interested in it can find out what information is encapsulated
//There are five kinds of mimetypes: image / PNG image / JPG text / JSON text / HTML video / MP4
  } success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"resposeObject %@",responseObject);
  } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"error %@",error);

PS: IOS — two ways to get data

NSMutableArray * dataArray =[responseDictionary valueForKeyPath:@"data.list_dic.list"];
    NSMutableArray * dataArray =responseDictionary[@"data"][@"list_dic"][@"list"];


The above is a comparison of several ways to get data from the Internet in IOS introduced by Xiaobian. I hope it can help you. If you have any questions, please leave me a message.