1.在.h中
#import <Foundation/Foundation.h> //typedef void(^success)(id); @interface NetUtil : NSObject /** 通过session获取 POST网络请求 */ + (void) POST:(NSURL *) url SuccessState:(void(^)(id responseData))success FailureState:(void(^)(id error))failure; @end
2.在.m中
#import "NetUtil.h" @implementation NetUtil #pragma mark - 通过NSURLSession进行POST请求 + (void) POST:(NSURL *) url SuccessState:(void(^)(id responseData))success FailureState:(void(^)(id error))failure{ NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:url]; request.HTTPMethod = @"POST"; request.timeoutInterval = 6; NSURLSessionDataTask * dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { if (error != nil || data == nil || response == nil) { // 请求失败 failure(error); }else{ // 请求成功 success(data); } }]; [dataTask resume]; } @end