iOS 封裝網路請求

2021-07-12 06:19:29 字數 1050 閱讀 5010

在開發過程中,很多模組都要去請求網路資料,如果每遇到一次都要去編寫請求網路的方法,為了方便,封裝網路請求

.h檔案

//寫乙個列舉標示請求型別

typedef ns_enum(nsinteger,requsttype);

//定義兩個block,把block當做引數

typedef void(^success)(nsdata *data);

typedef void(^faill)(nserror *error);

@inte***ce networkrequestmanager : nsobject

/** * <#description#>

* * @param type 請求型別

* @param urlstr 請求的url

* @param dicpare 請求的引數 post

* @param success 請求成功

* @param error 請求失敗

*/+(void)requestwithtype:(requsttype)type url:(nsstring *)urlstr para:(nsdictionary *)dicpare finish:(success)success error:(faill)faill;

.m檔案

+(void)requestwithtype:(requsttype)type url:(nsstring *)urlstr para:(nsdictionary *)dicpare finish:(success)success error:(faill)faill

nsurlsessiontask *task = [session datataskwithrequest:request completionhandler:^(nsdata * _nullable data, nsurlresponse * _nullable response, nserror * _nullable error) else

}];[task resume];

}

iOS 網路請求 與 網路封裝

void viewdidload void buttonclick uibutton button void getsynchrnized void postsynchrnized void getasynchrnized void postasynchrnized 新建立乙個類,繼承與nsobje...

iOS 網路請求

pragma mark 網路請求 方式 非同步 ibaction delegatebuttondidclicked uibutton sender 方法 客戶端收到伺服器的響應 pragma mark 客戶端收到伺服器的響應 void connection nsurlconnection conne...

封裝Ger網路請求

connection.h created by 毛連帥 on 14 6 24.import typedef void finishedblock nsdata data inte ce connection nsobject property nonatomic,retain nsmutableda...