English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Brief Discussion on AFNetworking Network Request GET and POST Steps in iOS

1.Firstly, download AFNetworking through the third-party: CocoaPods

1.1.First find the third-party library to be searched: pod search + AFNetworking

1.2.A list of pages appears, select the command for the latest version of the third-party library, for example: pod ‘MBProgressHUD’,'~>0.8''  (:q to return)

1.3.Create the project, enter the project: cd + Project path

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

1.5.(platform :iOS, ‘8.0'
target “工程名” do
pod ‘AFNetworking', ‘~> 3.1.0'
end)New version (Edit key i)-(Esc key: Enter:wq to return)

1.6.6.Save the settings of Podfile and then update and download third-party libraries: pod update

2.Enter the project for related operations

// Header file for network requests
#import <AFNetworking.h>
@interface ViewController ()
{
  // Boolean value for network monitoring judgment
  BOOL isOpen;
}
// Session object used for network requests
@property (nonatomic, strong) AFHTTPSessionManager *session;
@end
@implementation ViewController
- (void)viewDidLoad {
  [super viewDidLoad];
  // Initialize the Session object
  self.session = [AFHTTPSessionManager manager];
  // Set the supported data types when the request interface returns
  self.session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"application/x-json",@"text/html", nil];
}
#pragma mark - Response method for network monitoring button
- (IBAction)NetworkmonitoringAction:(id)sender {
  if (!isOpen) {
    //Open network monitoring
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    isOpen = YES;
  }
    // Close network monitoring
    [[AFNetworkReachabilityManager sharedManager] stopMonitoring];
    isOpen = NO;
  }
  // Next, it will judge whether the current status is WiFi or3g status, network unavailable status
  [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    switch (status) {
      case AFNetworkReachabilityStatusUnknown:
        NSLog(@"Current network is in an unknown state");
        break;
      case AFNetworkReachabilityStatusNotReachable:
        NSLog(@"Current network is not connected");
        break;
      case AFNetworkReachabilityStatusReachableViaWWAN:
        NSLog(@"Mobile data network");
        break;
      case AFNetworkReachabilityStatusReachableViaWiFi:
        NSLog(@"WiFi status");
        break;
      default:
        break;
    }
  };
}
#pragma mark - GET request
- (IBAction)getRequestAction:(id)sender {
  // : parameters1: URL of the GET request
  // : parameters2: Concatenating parameters
  // : parameters3: Current progress
  // : parameters4: Request successful
  // : parameters5: Request failed
  [self.session GET:@"http://api.yhouse.com/m/city/dynmiclist" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    NSLog(@"Download progress");
  }; * _Nonnull task, id _Nullable responseObject) {
    NSLog(@"Request successful:%@", responseObject);
  }); * _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:@"zpHr"2dsRvQQxYJxo2" forKey:@"member_id"];
  [dict setObject:@"ILfYpE4Dhs2gWcuQx" forKey:@"workspace_id"];
  // : parameters1: url
  // : parameters2: body
  [self.session POST:urlString parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
    NSLog(@"Upload progress");
  }; * _Nonnull task, id _Nullable responseObject) {
    NSLog(@"post request successful%@", responseObject);
  }); * _Nullable task, NSError * _Nonnull error) {
    NSLog(@"Post request failed:%@", error);
  };
}
#pragma mark - post2Request
- (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 = @"The class module is planned to use the third part, so that it is convenient for everyone to quickly search for questions when questions and answers accumulate to a certain amount, so the question part will not be added to this temporarily";
  // 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(@"Upload progress");
  }; * _Nonnull task, id _Nullable responseObject) {
    NSLog(@"Post request succeeded:%@", responseObject);
  }); * _Nullable task, NSError * _Nonnull error) {
    NSLog(@"Post request failed:%@", error);
  };
}

That's all for this article. Hope it will be helpful to your study, and also hope everyone will support the Yelling Tutorial more.

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not edit manually, and does not assume relevant legal liabilities. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)

You May Also Like