ASIHTTPRequest vs AFNetworking vs NSUrlRequest

2019-03-09 08:56发布

问题:

In the past I have use ASIHTTPRequest but now there is NSURLRequest. Should we be using the NSURLRequest now? Are there any disadvantages?

For people reading this now: I ended up using AFNetworking as mentioned in the answers

https://github.com/AFNetworking/AFNetworking

Thanks,

回答1:

NSURLConnection and NSURLRequest are the provided Cocoa classes for managing connections. In iOS 7, Apple added NSURLSession.

But I think you'll find AFNetworking to be a framework that further simplifies network requests (notably complex HTTP requests). If you don't want to use third-party frameworks for any reason, you can use NSURLConnection and/or NSURLSession directly. It just takes a little more coding.

If you're looking for a good ASIHTTPRequest alternative, AFNetworking is a great candidate.

For information on NSURLConnection and NSURLSession see the URL Loading System Programming Guide.



回答2:

NSURLRequest has been there since iOS2.0 (see the doc), so it is not precisely new. ASIHTTPRequest has been discontinued, the last update was on 15th May 2011, so you shouldn't use it. AFNetworking is a delightful networking library for iOS and Mac OS X as described by them self, and really makes your live easier, in my case it is the first framework I include in my network required projects.

Which one you should use?, I think this depends on your requirements, with NSURLRequest and NSURLConnection you can do everything, in fact AFNetworking us built on top of NSURLConnection. But how I tell you before, AFNetworking has my upvote.

EDIT post iOS7

If you are targeting iOS7 you should take a look to NSURLSession, check the official documentation, and this tutorial.

EDIT for Swift

Take a look to the Alamofire project for an implementation done in Swift, it is also created by mattt (the main author of AFNetworking).



回答3:

The asihttprequest is outdated library that doesn't support now for development. Do not use it.

the NSURLRequest is default iOS (cocoa) class to use. I recommend it for very little usage because you will need to write lots of code to handle connection responses. On top of it are build all http libraries.

The AFNetwoking is standart now for http, imho. It uses blocks for callbacks, it supports different types of data json, xml etc. it is well designed and can be easily subclassed. it is best.



回答4:

Native (NSURLSession) vs AFNetworking or any other library.

I would recommend you to use NSURLSession because it gives you a lot of customization on app end. Recently I was working on a project, I have to use multipart form data some where in the app. I have to provide my request body to the backend guy, I did use AfNetworking logger to print my body but it did not help me. so I did move to cocoa native library and got my solution.

so I would suggest you to use iOS native networking because it gives you a lot of customization but if you would need less code then you can use any 3rd party library.