Must-Have Cocoapods for 2016 — with Code Samples!

If you’re writing an iOS App in 2016, there’s a great chance your project will depend on Cocoapods. If you aren’t familiar, Cocoapods is kind of like the npm of iOS/OSX development.

I’ve come up with a list of some of my favorite Cocoapod libraries. I’ve taken a strong bias in featuring libraries that have first class Swift 2.1 support. Some of them may not be backwards-compatible with Objective-C libraries.

Async Caching Library for Objects and Images — Haneke

pod 'HanekeSwift'

What does it do? If you’re dealing with loading images from a URL and want a build in caching system that quickly loads URLs from the past you’re in for a treat because this library is incredibly fast. Not only does it support images, it also support generic objects.

Here’s how you would load a url into a UIImageView

import Haneke
// some more code here..

let url = NSURL(string:"https://placeholdit.imgix.net/~text?txtsize=33&txt=iluvedenmsg&w=350&h=150")!
myImageView.hnk_setImageFromURL(url)

What about other types of data? Luckily you get native support for UIImage, NSData, JSON and String Think of it as a robust Async-IO NSUserDefaults.

import Haneke

// lets load an MP4 video into the cache!
let myData : NSData = NSData.dataWithContentsOfMappedFile("myPathToTheMp4.mpg")
let cache = Shared.dataCache
cache.set(value: myData, key: "myvideo.mp4")

//later somewhere else...
cache.fetch(key: "funny-games.mp4").onSuccess { data in
// Do something with data
}

Reactive Programming — RxSwift & RxCocoa

When dealing with multiple async functions, it’s incredibly tempting to create nasty nested code. Reactive Programming takes a stab at making async programming much more manageable and type-safe. RxSwift comes paired with RxCocoa, a set of helper extension methods on CocoaTouch and Cocoa controls. You’ll need to install both to reap the full benefits.

pod 'RxSwift', '~> 2.0.0-beta.4'
pod 'RxCocoa', '~> 2.0.0-beta.4'

Everything around RxSwift revolves around:

Observable<T>

Observables, are pretty much events that fire over time.. However the beauty is that we can operate on Observables just like we can operate on Arrays and collections. The topic is very large and I hope to do a full gitbook on the topic, but I encourage you to check out the link and find out more about it. Once you go Rx, you’ll want it in all your projects!

Chat Messages? — SlackTextViewController

Building a chat application typically involves either a UITableView, UIToolbar, with a lot of constraints and keyboard functionality. Setting up the layout can be a pain in the butt but luckily Slack, has open sourced their UIViewController to the world so that you can focus more on the UITableViewCells and your API integration rather than the setup of the view.

pod 'SlackTextViewController'

Getting started is incredibly easy. All you need to do is import the framework and inherit from SLKTextViewController:

import SlackTextViewController

class MyMessagesViewController : SLKTextViewController {

override func viewDidLoad(){
super.viewDidLoad()
}
}

Beautiful Camera and Image Picker ala Instagram — ImagePicker

Instagram has one sleek ImagePickerController. It’s incredibly beautiful and intuitive and that’s exactly what ImagePicker intends to do for your app.

pod 'ImagePicker'

With a minimal amount of code you can present ImagePicker easily:

import ImagePicker

let imagePickerController = ImagePickerController()
self.imagePickerController.delegate = self //set the delegate
self.presentViewController(self.imagePickerController, animated: true, completion: nil)

// implemented ImagePickerDelegate methods

// BEGIN - IMAGE PICKER DELEGATE
func cancelButtonDidPress() {}

func wrapperDidPress(images: [UIImage]) { print("wrapperDidPress")}

func doneButtonDidPress(images: [UIImage]){
self.imagePickerController.dismissViewControllerAnimated(true, completion: nil)
UploadService.sharedInstance.uploadImages(images, handleId: handleId)
}
// END - IMAGE PICKER DELEGATE

Playing Sounds — JSQSystemSoundPlayer

If you ever need to play sounds from your app, you’ll find out that it takes quite a lot of copy and pasting. JSQSystemSoundPlayer saves you from having to scour StackOverflow for the best way to play MP3, WAV, AIFF etc… files.

import JSQSystemSoundPlayer

JSQSystemSoundPlayer.sharedPlayer().playSoundWithFilename("alert", fileExtension: "wav", completion: nil)

// you can even vibrate your device!!
JSQSystemSoundPlayer.sharedPlayer().playVibrateSound()

Also, if your user has some global preference to play or not play sounds, you can easily toggle the shared player’s preferences. This way you don’t have to change your app’s code in all the various locations to check for an NSUserDefaults value!

JSQSystemSoundPlayer.sharedPlayer().toggleSoundPlayerOn(false)

Stylish Image View — JTSImageViewController

Sometimes we’d like to view an image up close and personal. When we’re done with it, we can just flick it away. JTSImageViewController makes it super easy to replicate image viewing stylishly with a minimal amount of code:

pod 'JTSImageViewController'
import JTSImageViewController

//eventually.......

if let image = myImageView.image{
let imageInfo = JTSImageInfo()
imageInfo.image = image
imageInfo.referenceRect = myImageView.frame
imageInfo.referenceView = myImageView.superview
let imageViewer = JTSImageViewController(imageInfo: imageInfo, mode: .Image, backgroundStyle: .Blurred)
imageViewer.showFromViewController(self, transition: .FromOriginalPosition)
}

Easy user-device permissions — PermissionScope

Asking for user permissions is one of the most painful processes of app development, particularly if your user “accidentally” denied permissions.

pod 'PermissionScope'

Permission scope has an incredibly easy chaining functionality where you can ask your users for the permissions relevant to your application.

override func viewDidLoad(){
super.viewDidLoad()
// Set up permissions

let pscope = PermissionScope()
pscope.addPermission(ContactsPermission(),
message: "We use this to help\r\nyou find your existing friends.")
pscope.addPermission(NotificationsPermission(notificationCategories: nil),
message: "We use this to send you\r\nspam messages")
pscope.addPermission(LocationAlwaysPermission(),
message: "We use this to update your location")

// Show dialog with callbacks
pscope.show({ finished, results in
print("got results \(results)")
}, cancelled: { (results) -> Void in
print("thing was cancelled")
})
}

Note, you will still have to setup your App’s capabilities in the Info.plist for certain APIs like CoreLocation.


Originally published at Eden Messenger Blog.