AFNetworking is one of the most popular third-party libraries on iOS and OS X. It was awarded the 2012 Best iOS Library Award in our 2012 Reader’s Choice Awards, and is one of the most widely-used open source projects on Github with over 14K stars and 4K forks.
Recently the creator and maintainer of AFNetworking, Mattt Thompson, released a new networking library like AFNetworking, but designed specifically to use modern Swift conventions and language features: Alamofire.
The short answer is that Alamofire is based on NSURLSession, but it frees you from writing boilerplate code and makes writing networking code much easier. You can access data on the Internet with very little effort, and your code will be much cleaner and easier to read.
Sample API call
import Alamofire
Next, add the following code to viewDidLoad() just below setupView():
Alamofire.request(.GET, "https://api.500px.com/v1/photos").responseJSON() {
(_, _, data, _) in
println(data)
}
You'll see the following message appear in the console:
Optional({
error = "Consumer key missing.";
status = 401;
})
Recently the creator and maintainer of AFNetworking, Mattt Thompson, released a new networking library like AFNetworking, but designed specifically to use modern Swift conventions and language features: Alamofire.
Retrieving Data from API
You might be asking why you need Alamofire in the first place. Apple provides the NSURLSession class and related classes for downloading content via HTTP, so why complicate things with another third party library?The short answer is that Alamofire is based on NSURLSession, but it frees you from writing boilerplate code and makes writing networking code much easier. You can access data on the Internet with very little effort, and your code will be much cleaner and easier to read.
Sample API call
import Alamofire
Next, add the following code to viewDidLoad() just below setupView():
Alamofire.request(.GET, "https://api.500px.com/v1/photos").responseJSON() {
(_, _, data, _) in
println(data)
}
You'll see the following message appear in the console:
Optional({
error = "Consumer key missing.";
status = 401;
})
No comments:
Post a Comment