get and post steps of AFNetworking network request in IOS

  • 2021-11-02 02:57:05
  • OfStack

1. First download AFNetworking through the third party: CocoaPods

1.1. Find the 3-party library you want to find first: pod search + AFNetworking

1.2. Go out to the list page and select the latest version command of the 3-side library, such as pod 'MBProgressHUD', '~ > 0.8 '(: q returned)

1.3. Create Project, Enter Project: cd + Project Path

1.4. Edit the Podfile file of the project: vim Podfile

1.5. (platform: iOS, '8.0' target "Project Name" dopod 'AFNetworking', '~ > 3.1. 0 'end) new version (edit key i)- > (Esc Key: Enter: wq Return)

1.6. 6. Save the settings of Podfile, then update and download the 3-party library: pod update

2. Enter the project to carry out relevant operations


//  Header file of network request 
#import <AFNetworking.h>
@interface ViewController ()

{
  //  Carrying out network monitoring and judgment bool Value 
  BOOL isOpen;
}

//  Used for network requests Session Object 
@property (nonatomic, strong) AFHTTPSessionManager *session;


@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  //  Initialization Session Object 
  self.session = [AFHTTPSessionManager manager];
  //  Set what type of data is supported when the request interface comes back 
  self.session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"application/x-json",@"text/html", nil];
}

#pragma mark -  Response method of network monitoring button 
- (IBAction)NetworkmonitoringAction:(id)sender {
  
  if (!isOpen) {
    // Turn on network monitoring 
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    
    isOpen = YES;
    
    
  } else {
    //  Turn off network monitoring 
    [[AFNetworkReachabilityManager sharedManager] stopMonitoring];
    
    isOpen = NO;
    
  }
  
  //  Next, it will be judged that the current is WiFi Status or 3g Status , Network unavailable state 
  [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    switch (status) {
      case AFNetworkReachabilityStatusUnknown:
        NSLog(@" The current network is in an unknown state ");
        break;
      case AFNetworkReachabilityStatusNotReachable:
        NSLog(@" The current network is in an unlinked state ");
        break;
      case AFNetworkReachabilityStatusReachableViaWWAN:
        NSLog(@" Mobile phone traffic network ");
        break;
      case AFNetworkReachabilityStatusReachableViaWiFi:
        NSLog(@"wifi Status ");
        break;
      default:
        break;
    }
  }];
  
}

#pragma mark - get Request 
- (IBAction)getRequestAction:(id)sender {
  
  //  Parameter 1: get Requested URL 
  //  Parameter 2:  Splicing parameters 
  //  Parameter 3:  Current progress 
  //  Parameter 4:  Request succeeded 
  //  Parameter 5:  Request failed 
  [self.session GET:@"http://api.yhouse.com/m/city/dynmiclist" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    NSLog(@" Download progress ");
  } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
    NSLog(@" Request succeeded :%@", responseObject);
  } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    NSLog(@" Request failed :%@", error);
  }];
  
  
}

#pragma mark - post Request 
- (IBAction)postRequestAction:(id)sender {
  
  /*{
   do = "pri_memberlist";
   "member_id" = zpHr2dsRvQQxYJxo2;
   "workspace_id" = ILfYpE4Dhs2gWcuQx;
   }*/
  NSString *urlString = @"http://m.taskwedo.com/API/wedo1/wedo.php";
  
  NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  [dict setObject:@"pri_memberlist" forKey:@"do"];
  
  [dict setObject:@"zpHr2dsRvQQxYJxo2" forKey:@"member_id"];
  [dict setObject:@"ILfYpE4Dhs2gWcuQx" forKey:@"workspace_id"];
  //  Parameter 1: url
  //  Parameter 2: body Body 
  [self.session POST:urlString parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
    NSLog(@" Progress of uploading ");
  } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
    NSLog(@"post Request succeeded %@", responseObject);
  } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    NSLog(@"post Request failed :%@", error);
  }];
}

#pragma mark - post2 Request 
- (IBAction)postTwoRequestAction:(id)sender {
  
  /*address = "";
      comment = "\U7c7b\U6a21\U5757\U8ba1\U5212\U7528\U5230\U7b2c\U4e09\U90e8\U5206\U4e2d\Uff0c\U5f85\U63d0\U95ee\U3001\U56de\U7b54\U79ef\U7d2f\U5230\U4e00\U5b9a\U6570\U91cf\U65f6\Uff0c\U4fbf\U4e8e\U5927\U5bb6\U7684\U95ee\U9898\U7684\U5feb\U901f\U67e5\U627e\Uff0c\U6240\U4ee5\U63d0\U95ee\U90e8\U5206\U6682\U65f6\U4e0d\U52a0\U5165\U8fd9\U4e2a";
      do = "add_comment";
      kind = task;
      "member_id" = zpHr2dsRvQQxYJxo2;
      other = "";
      "task_id" = 55a47e79ec25e3641;*/
  
  NSString *urlString = @"http://m.taskwedo.com/API/wedo1/wedo.php";
  
  NSString *commonContent = @" Class module plans to use the 3 In the section, the questions and answers to be asked are accumulated to 1 When the quantity is fixed, it is convenient for everyone to find out the questions quickly, so this is not added to the question part for the time being ";
  //  Encode Chinese characters 
  commonContent = [commonContent stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  [dict setValue:@"" forKey:@"address"];
  [dict setValue:commonContent forKey:@"comment"];
  [dict setValue:@"add_comment" forKey:@"do"];
  [dict setValue:@"task" forKey:@"kind"];
  [dict setValue:@"zpHr2dsRvQQxYJxo2" forKey:@"member_id"];
  [dict setValue:@"" forKey:@"other"];
  [dict setValue:@"55a47e79ec25e3641" forKey:@"task_id"];
  
  [self.session POST:urlString parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
    NSLog(@" Progress of uploading ");
  } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
    NSLog(@"post Request succeeded :%@", responseObject);
  } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    NSLog(@"post Request failed :%@", error);
  }];
}


Related articles: