简单的网络请求封装

NetWorkHelper.h

#import <Foundation/Foundation.h>

@protocol NetWorkHelperDelegate <NSObject>

@required
//参数为所需要传出去的值(解析好的)
- (void)passValueWithData: (id)value;

@end

@interface NetWorkHelper : NSObject

@property (nonatomic,assign) id<NetWorkHelperDelegate> delegate;

//同步get请求
- (void)getAndSynchronousMethodWithURL: (NSString *)urlString;
//同步post请求
- (void)postAndSynchronousMethodWithURL: (NSString *)urlString;

//异步post请求
- (void)postAndAsynchronousMethodWithUrl: (NSString *)urlString;
//异步get
- (void)getAndAsynchronousMethodWithURL: (NSString *)urlString;

@end

NetWorkHelper.m

#import "NetWorkHelper.h"

@implementation NetWorkHelper

- (void)jsonParserWithData: (NSData *)data
{
    if (data) {
        id value = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        [self.delegate passValueWithData:value];
    }
    
}

//同步get请求
- (void)getAndSynchronousMethodWithURL: (NSString *)urlString
{
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSError *error;
    NSData *receiveData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
    [self jsonParserWithData:receiveData];
    
}

//异步POST请求
- (void)postAndAsynchronousMethodWithUrl: (NSString *)urlString
{
    NSArray *array = [urlString componentsSeparatedByString:@"?"];
    NSURL *url = [NSURL URLWithString:array[0]];
    NSData *postData = [array[1] dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //设置请求方式
    request.HTTPMethod = @"POST";
    //设置请求参数
    request.HTTPBody = postData;
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        [self jsonParserWithData:data];
    }];
    
}

//同步post
- (void)postAndSynchronousMethodWithURL: (NSString *)urlString
{
    NSArray *array = [urlString componentsSeparatedByString:@"?"];
    NSURL *url = [NSURL URLWithString:array[0]];
    //创建POST请求参数,为NSData类型
    NSString *postString = array[1];
    //将string类型转换为NSData类型
    NSData *postParameterData = [postString dataUsingEncoding:NSUTF8StringEncoding];
    //创建请求,因为NSURLRequest类型不能设置请求方式,所以如果是post请求,就得使用它的子类NSMutableURLRequest
    NSMutableURLRequest *mutableReq = [NSMutableURLRequest requestWithURL:url];
    //设置请求方式
    mutableReq.HTTPMethod = @"POST";
    //设置请求参数
    mutableReq.HTTPBody = postParameterData;
    
    //建立同步连接
    NSData *receiveData = [NSURLConnection sendSynchronousRequest:mutableReq returningResponse:nil error:nil];
    [self jsonParserWithData:receiveData];
    
}

//异步get
- (void)getAndAsynchronousMethodWithURL: (NSString *)urlString
{
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        [self jsonParserWithData:data];
    }];
}

@end

推荐阅读更多精彩内容