您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 訊息 >> iOS 相關資訊

iOS 網路請求封裝:GET、POST、上傳與下載

編輯:iOS 相關資訊

可將 AFNetworking 封裝成 DataRequestManager,統一管理 GET、POST、上傳與下載。

基本設計

類別內部建立 manager 或 sessionManager,對外提供 methodGetWithURL、methodPostWithURL、methodUploadWithURL 等方法,並用 delegate 回傳結果。

GET 與 POST

GET 適合取得資料,POST 適合送出參數。兩者都可在 success 與 failure block 中處理回傳。

[manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    [self.delegate passGetValue:responseObject];
} failure:nil];

上傳圖片

使用 constructingBodyWithBlock 加入檔案資料,並指定 name、fileName 與 mimeType。

下載與斷點續傳

iOS 7 後可用 AFURLSessionManager 的 downloadTask,透過 resume、suspend 或 cancel 管理下載;若要續傳,需搭配 outputStream 與適當的快取策略。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved