簡單的網(wǎng)絡(luò)請求封裝

NetWorkHelper.h

#import <Foundation/Foundation.h>

@protocol NetWorkHelperDelegate <NSObject>

@required
//參數(shù)為所需要傳出去的值(解析好的)
- (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];
    //設(shè)置請求方式
    request.HTTPMethod = @"POST";
    //設(shè)置請求參數(shù)
    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]];
    //創(chuàng)建POST請求參數(shù),為NSData類型
    NSString *postString = array[1];
    //將string類型轉(zhuǎn)換為NSData類型
    NSData *postParameterData = [postString dataUsingEncoding:NSUTF8StringEncoding];
    //創(chuàng)建請求,因為NSURLRequest類型不能設(shè)置請求方式,所以如果是post請求,就得使用它的子類NSMutableURLRequest
    NSMutableURLRequest *mutableReq = [NSMutableURLRequest requestWithURL:url];
    //設(shè)置請求方式
    mutableReq.HTTPMethod = @"POST";
    //設(shè)置請求參數(shù)
    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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容