iOS開發之AFNetWorking初次使用會報錯的坑

YungFan發表於2017-12-13
  • 第一次用 CocoPods 安裝好了 AFNetWorking 後,無論使用 Get 還是 Post,總是直接進入 failure 的 block,錯誤資訊如下:
error=Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html"
複製程式碼
  • 解決辦法一:修改框架中的 AFURLResponseSerialization.m檔案的223行左右,修改以後如下:
self.acceptableContentTypes = [NSSetsetWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/plain",nil];
複製程式碼
  • 解決辦法二:在每次構建AFHTTPSessionManager以後,手動設定:
session.responseSerializer.acceptableContentTypes = [NSSetsetWithObjects: @"text/html", @"text/plain",nil];
複製程式碼

這樣修改的好處是不需要更改框架本身的東西,否則一旦更新 CocoPods 就會失效,希望給初次使用該框架的朋友提個醒。

相關文章