iOS基礎--網絡請求的簡單封裝

#import <Foundation/Foundation.h>

@protocol NetWorkingHelperDelegate <NSObject>

// 參數為所需要傳出去的值(解析好的數據)
- (void)passValueWithData : (id)value ;

@end

@interface NetWorkingHelper : NSObject

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

// 同步get
- (void)getAndSynchronousMethodWithURL : (NSString *)urlString ;
// 同步post
- (void)postAndSynchronousMethodWithURL : (NSString *)urlString parameterString : (NSString *)parameterString ;
// 異步get block實現
- (void)getAndAsynchronousMethodWithURL : (NSString *)urlString ;
// 異步post  block實現
- (void)postAndAsynchronousMethodWithURL :(NSString *)urlString parameterString : (NSString *)parameterString ;

@end
#import "NetWorkingHelper.h"

@implementation NetWorkingHelper

// 解析
- (void)jsonParserWithData :(NSData *)data
{
    id receiveResult = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil] ;
    // 通過代理將解析好的數據傳遞出去
    [self.delegate passValueWithData:receiveResult] ;
}

// 同步連接的get請求
- (void)getAndSynchronousMethodWithURL : (NSString *)urlString
{
    // 定義一個URL網址
    NSURL *url = [NSURL URLWithString:urlString] ;
    // 初始化請求方式,默認為get方式
    NSURLRequest *request = [NSURLRequest requestWithURL:url] ;
    // 創建同步鏈接
    // request : 請求對象,里邊承載著我們的請求信息,有網址,請求頭等信息
    // response : 請求的返回(響應),里面包含了響應頭的一些信息,如果需要用到響應頭,需要傳遞此參數,一般不需要.
    // error:請求出錯的時候,會有錯誤信息保存在該參數中,一般置為nil就可以,咱們可以根據返回數據來判斷是否請求有問題.
    NSError *error ;
    NSData *receiveData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error] ;
    [self jsonParserWithData:receiveData] ;
}

// 同步連接的post請求
- (void)postAndSynchronousMethodWithURL : (NSString *)urlString parameterString : (NSString *)parameterString
{
    //date=20151031&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213
    NSURL *url = [NSURL URLWithString:urlString] ;
    // 將string類型轉換為NSDate類型.
    NSData *postParameter = [parameterString dataUsingEncoding:NSUTF8StringEncoding] ;
    // 創建請求,因為NSURLRequest類型不能設置請求方式,所以如果是post請求,就得使用它的子類NSMutableURLRequest
    NSMutableURLRequest *mutableRequest = [NSMutableURLRequest requestWithURL:url] ;
    // 設置請求方式
    mutableRequest.HTTPMethod = @"POST" ;
    // 設置請求參數
    mutableRequest.HTTPBody = postParameter ;
    // 創建同步連接
    NSError *error ;
    NSData *receiveData = [NSURLConnection sendSynchronousRequest:mutableRequest returningResponse:nil error:&error] ;
    [self jsonParserWithData:receiveData] ;
}

// 異步連接的get請求 block實現
- (void)getAndAsynchronousMethodWithURL : (NSString *)urlString
{
    NSURL *url = [NSURL URLWithString:urlString] ;
    NSURLRequest *request = [NSURLRequest requestWithURL:url] ;
    // 異步連接 block實現
    // queue :需要將請求連接放到一個隊列中,目前我們是將該請求放到主隊列中,在主隊列中,操作所占有資源的優先等級更高,說白了就是請求會更快.
    // handle : 請求有返回結果的時候,會執行該block回調.
    /*
     block中參數解釋:
     NSURLResponse _Nullable response:請求返回的響應,內部包含響應頭
     NSData * _Nullable data : 使我們所需要的實際數據,也就是我們所需要的解析數據
     NSError * _Nullable connectionError : 請求出錯的時候返回的錯誤信息.
     */
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError)
     {
         [self jsonParserWithData:data] ;
     }];
}

// 異步連接的post請求 block實現
- (void)postAndAsynchronousMethodWithURL :(NSString *)urlString parameterString : (NSString *)parameterString
{
    
    NSURL *url = [NSURL URLWithString:urlString] ;
    NSData *postParameter = [parameterString dataUsingEncoding:NSUTF8StringEncoding] ;
    NSMutableURLRequest *mutableRequest = [NSMutableURLRequest requestWithURL:url] ;
    mutableRequest.HTTPBody = postParameter ;
    mutableRequest.HTTPMethod = @"POST" ;
    [NSURLConnection sendAsynchronousRequest:mutableRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError)
     {
         [self jsonParserWithData:data] ;
     }] ;
}
@end

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容