With the next version of iOS already nearing completion, we wanted to develop Snug using the latest version of Swift - 2.0.

Swift 2.0 iOS Development - Snug for Instagram

The fundamental logic behind Snug was to implement the OAuth protocol to grant a user access to their Instagram account, be able to send RESTful requests to the Instagram API and then process the JSON data we get back. Just like all of our PHP web development projects, finding the right dependencies is key to speeding up your development.


We wanted a lightweight OAuth2 integration that was built on Swift. We’re not against using an Objective-C bridging header but having decided to develop our app in Swift, we really want to go the full distance. With OAuthSwift you simply need to delegate your app a URL scheme that can handle the callbacks and then make sure your AppDelegate can process the request:

func application(application: UIApplication!, openURL url: NSURL!, sourceApplication: String!, annotation: AnyObject!) -> Bool {
    return true

After firing off our authorisation request and receiving the access token and its secret key, we can make requests to the Tumblr API with the same class. For example, to get a particular users data:

// Main content processing
let url: String = "https://api.instagram.com/v1/users/" + userId;
// Send the request
oAuthSwift.client.get(url, parameters: emptyParameters, success: {
   data, response in
   // Handle our response
}, failure: {(error:NSError!) -> Void in
    // Gracefully handle errors

We define a constant in our class to speed up API calls that have no custom parameters:

let emptyParameters = Dictionary<String, AnyObject>()


Dealing with OAuth2 and keeping track of the user’s OAuth access token can be a pain and Locksmith is the painless solution that lets you store data in their iOS’ keychain. Typically, storing and accessing keychain data is quite laborious and there’s a lot of code that you’ll repeat over and over. The developers behind Locksmith have made integrating it effortless. All you need to do is code:

let OAUTH = "OAuthData"
let SNUG = "Snug"
// Load from the Keychain
let (oAuthData, _) = Locksmith.loadDataForUserAccount(OAUTH, inService: SNUG)
// Do we have one?
if let accessToken = oAuthData?.objectForKey("accessToken") {
    // We got the data back, so we’ve got an access token
} else {
    // Locksmith couldn’t find anything in the keychain

If you didn’t find an access token and you use the OAuth2 authorization flow from above to get your credentials back, you can then store the access token with:

// Save the token data
let error = Locksmith.saveData([
    "accessToken": accessTokenValue as! String
], forUserAccount: OAUTH, inService: SNUG)


If you’re playing with RESTful API’s in your iOS application, you’ll need a great solution for dealing with JSON. You’ll want something that’s easy to integrate, understand and use. SwiftyJSON is exactly that.

Adding the ‘JSON’ data type to your formatting, you just have to declare the object as a JSON object and then you can access its modifiers as you’re used to doing in Javascript and PHP. It has a variety of built in type-casting measures to export data as integer, string, boolean, etc.

// Get the JSON data
let json = JSON(data: xhrResponse)
// Access some properties
let followers = json[“data”][“counts”][“followed_by”].doubleValue
let imageUrl = json[“data”][“media”]["images"]["low_resolution"]["url"].stringValue

Adding Padding to UITextField

One thing that always puzzles me about iOS applications and the development effort behind them, is how simple mundane objectives can prove to be relatively tricky. The styling on a UITextField doesn’t have any inner padding to the left or right by default. So one great way to achieve this is by creating your own subclass of UITextField and overriding the ‘textRectForBounds’ and ‘editingRectForBounds’ methods like such:

class SnugTextField: UITextField {
   var leftMargin: CGFloat = 10.0   
   override func textRectForBounds(bounds: CGRect) -> CGRect {
       var newBounds = bounds;
       newBounds.origin.x += leftMargin;
       return newBounds;
   override func editingRectForBounds(bounds: CGRect) -> CGRect {
       var newBounds = bounds;
       newBounds.origin.x += leftMargin;
       return newBounds;

Want to be one of the first to know when Snug is available? Click the link below!

Keep me updated!