How I boosted my iOS development with Swift Libraries
Summary
The author shares how discovering open-source Swift libraries through sources like iOS Dev Weekly and GitHub Trending Repositories helped reduce boilerplate and solve common development tasks more efficiently.
Three recommended libraries are highlighted: Kingfisher for downloading and caching images, Alamofire for performing network requests, and SnapKit for building Auto Layout views (especially for iOS8 support).
The post notes a growing community shift from Objective-C to Swift, citing Facebook's beta Swift SDK and Apple's upcoming Swift Package Manager as examples of this trend.
As an iOS developer working at a startup focused in collaborative development, I’ve been involved in several projects so far, and most of them share common tasks such as downloading and caching images, performing network requests, and building Auto Layout views.
At first, I had a flow that I thought was good enough to accomplish these basic tasks (or any other, for that matter):
Try to implement using the iOS SDK
Get stuck at a problem that doesn’t have a straightforward solution
Look up the solution on StackOverflow and implement it
Move on to the next task
That seemed like a good flow at first, but got a bit tiresome in the long run – after all, nobody likes to hack for a living.
This all changed when I started reading a couple of widely followed news feeds such as iOS Dev Weekly, and also checking GitHub Trending Repositories. Looking at the these sources regularly, I realized that most of the basic tasks I need to accomplish on a daily basis have already been solved by the open-source community – and are available in the form of libraries.
Overall, these tools boosted my development by reducing boilerplate and also by addressing my problems better than I (or anyone) could in almost no time – which is a key factor in any startup business. Don’t get me wrong here – I’m not assuming you’re a lousy dev, but using tools that were validated by a bunch of people may come in really handy.
I’ve put together some of my favorite Swift libraries that I use on a daily basis to solve basic problems.
1. Downloading and Caching Images
Images are present in almost all apps I’ve seen so far, even if it’s just a profile picture in the settings screen. Combining NSURLSession and NSURLCache to respectively download and cache the images can definitely do the job, but why bother with all that if you’re able to use Kingfisher (a Swift-flavoured SDWebImage) and set the image of your ImageView within a single line? Check this out:
It also provides simple ways to define placeholder images, completion blocks and even check the download progress.
2. Performing Network Requests
Games storing user content, news feeds providing up-to-date content, chat apps interfacing the message exchange and so much more – nowadays it’s nearly impossible to imagine an app that does not communicate with a server through an HTTP(S) API. If you ever had tons of headaches to fetch and parse data from an API, here is the solution for you my friend: Alamofire, a Swift version for AFNetworking, developed and maintained by the same team.
Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
.validate()
.responseJSON { response in
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
It does all sorts of things, from uploading files to response validation, in a very elegant way.
3. Building Auto Layout Views
If you have ever had to work with NSLayoutConstraint (or worse VFL o.O) you know how painful it can be to implement even the most simple views programmatically. After years of complaints from the community, Apple has finally provided a fluent Auto Layout API for iOS9 and up, but if your app still has to support iOS8, SnapKit (a Swift version of Masonry, developed and maintained by the same team) offers the best solution out there.
import SnapKit
class MyViewController: UIViewController {
lazy var box = UIView()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(box)
box.snp_makeConstraints { make in
make.width.height.equalTo(50)
make.center.equalTo(self.view)
}
}
}
The Shift to Swift
If you pay attention to the most famous libraries out there, you’ll realize how the community is moving from Objective-C to Swift more and more every day. Most of the big companies are moving on that direction as well – a great example is Facebook, which already has a beta version of its Facebook SDK in Swift.
Another boost to this trend lies on Apple working on its own alternative to Cocoapods and Carthage, the Swift Package Manager. According to Ben Morrow in What’s New In Swift 3, it will be the first release to include the SPM. There are several libraries that already support it and we’ll likely start to see even more in the coming months.
Whether you decide to keep your Objective-C or move on to Swift, I hope these libraries help you as much as they have helped me so far. Agree, disagree or have any suggestions? Let me know your thoughts!
share this article
FAQ
Why use open-source libraries instead of implementing everything with the iOS SDK?
Most basic tasks needed on a daily basis have already been solved by the open-source community and are available as libraries. These tools boost development by reducing boilerplate and addressing problems better and faster than implementing solutions from scratch, which is a key factor in any startup business.
Which library is recommended for downloading and caching images?
Kingfisher, a Swift-flavoured SDWebImage, allows setting an ImageView's image within a single line. It also provides simple ways to define placeholder images, completion blocks, and check the download progress.
Which library is recommended for performing network requests?
Alamofire, a Swift version of AFNetworking developed and maintained by the same team. It handles tasks like fetching and parsing data from APIs, uploading files, and response validation in an elegant way.
Which library is recommended for building Auto Layout views?
SnapKit, a Swift version of Masonry developed and maintained by the same team. While Apple provided a fluent Auto Layout API for iOS9 and up, SnapKit offers the best solution if your app still has to support iOS8.
What is the Swift Package Manager and why does it matter?
The Swift Package Manager (SPM) is Apple's own alternative to CocoaPods and Carthage. According to Ben Morrow in What's New In Swift 3, it will be the first release to include the SPM, and several libraries already support it, with more expected in the coming months.
A developer motivated by solving problems and learning how stuff works. Someone excited by new experiences, hearing good music and sharing a good bottle of wine ;)