Coinbase Swift SDK v1.2

Coinbase Swift SDK v1.2 has been released! A full list of changes can be found in the changelog but here are the main updates:

Token Swap Service

To remove the need to embed the oAuth secret key with the application, the SDK now requires a token swap service for the authorization flow. I have open sourced a token swap service that has a convenient Heroku deploy button so anyone can have a token swap service in a couple of clicks!

Fetch Auth Information

The API client has a new fuction that lets you query the user's auth information (docs).

public func fetchAuthInformation(_ completionHandler: @escaping (_ authInformation: AuthInformation?, _ errors: [Error]?) -> Void)

This also introduces a couple of new objects:

  • AuthInformation
  • AuthMetadata

I also updated the Auth object's scopes values to be Scope objects rather than strings.

Implementing Dark Mode with macOS Mojave and Swift

With the latest macOS release, Apple introduced Dark Mode. Dark Mode changes the whole colour scheme of the OS to a darker, more subtle style. Not only does the OS change but so do the apps.

We recently added support for Dark Mode to Quids, our cryptocurrency Mac app. I was impressed with how easy Apple had made Dark Mode to implement and I expect to see a similar system come to iOS in the future.

If you have just used system colours such as NSColor.textColor, NSColor. selectedTextColor and NSColor.windowBackgroundColor, then your work is done!

However, you most likely have some custom colours. Here is an example of what our colour setup originally looked like:

internal extension NSColor
    internal struct Quids
    	internal static let buttonBackground = NSColor(deviceRed: 250.0/255.0, green: 250.0/255.0, blue: 250.0/255.0, alpha: 1.0)
        // ... etc ...

First step is to create a new Asset Catalog. Choose where you want to add the new file, right click, New File, Asset Catalog. Give it whatever name you want, I like to live on the edge and use Colors.xcassets.


Next you need to add a colour set. Do this by either right clicking on the asset catalog’s left list view or by clicking the + button in the catalog’s bottom left and select “New Color Set”


Now you’ll have something that looks like this:


First things first, set the name. On the right side where it says “Name: Color” set this to whatever best describes your colour. I’m going to call mine ButtonBackground.

As it stands, this colour set is only setup as having no appearance, meaning that no matter what colour mode the user has, our ButtonBackground colour will always be the same.

On the right, select the “Any, Dark” appearance.


You’ll see another colour swatch appear. Clicking a swatch will let you set the colour values:


With the colours set, the last step is to update our code:

internal extension NSColor
    internal struct Quids
    	internal static let buttonBackground = NSColor(named: "ButtonBackground")!
        // ... etc ...

That’s it! We’re now all setup to use the colour in our app…

Using The Colour

If you’re using a standard control that takes an NSColor, for example NSTextField and NSWindow then all you need to do is set our new colour:

self.textField.textColor = NSColor.Quids.buttonBackground
self.window.backgroundColor = NSColor.Quids.buttonBackground

However, if you’re doing something a little more custom, like setting a CALayer’s background colour:

self.view.layer?.backgroundColor = NSColor.Quids.buttonBackground.cgColor

This won’t work. Never fear! There a few hooks that get called when the OS’s colour mode changes:

// NSViewController

override func updateViewConstraints() 
    self.view.layer?.backgroundColor = NSColor.Quids.buttonBackground.cgColo

// NSView

override func updateLayer()
    self.layer?.backgroundColor = NSColor.Quids.buttonBackground.cgColor

override func layout()
    self.layer?.backgroundColor = NSColor.Quids.buttonBackground.cgColor

override func updateConstraints()
    self.layer?.backgroundColor = NSColor.Quids.buttonBackground.cgColor


We've recently released xPal which automagically generates a Xcode color asset catalog from a Sketch file!

Observe Screen Locking in macOS

In Quids, similar to 1Password, we lock the app when your Mac locks.

There are two notifications sent via the DistributedNotificationCenter. One when the screen is locked and the other when it is unlocked. I couldn't find any predefined variables so created this Notification.Name extension:

internal extension Notification.Name
    static let screenIsLocked = Notification.Name("com.apple.screenIsLocked")
    static let screenIsUnlocked = Notification.Name("com.apple.screenIsUnlocked")

Then all that is left to do is register an object as an observer:

DistributedNotificationCenter.default().addObserver(self, selector: #selector(...), name: Notification.Name.screenIsLocked, object: nil)

Send Money with the Coinbase Swift SDK

Photo by Thought Catalog / Unsplash

I recently added the ability to send money using the Coinbase Swift SDK that we're building alongside Quids.

When going through the OAuth flow you need to add the CoinbaseAPIClient.Scope.createTransactions to your list of scopes:

let authScopes: [CoinbaseAPIClient.Scope] = [
    .createTransactions(sendLimit: 500.0, currencyCode: "USD", period: .day)

Then once you have the required permission you will need to build a SendMoney request and pass that to your CoinbaseAPIClient:

let request = CoinbaseAPIClient.SendMoney(to: ethAddress, amount: 2.5, currencyCode: "ETH")

coinbase.send(money: request, from: accountID, twoFactorCode: nil) { (transaction, errors) in
    // ...

You'll notice this function can also take a 2FA code. If Coinbase returns a CoinbaseAPIClient.APIError.twoFactorRequired error, you should ask your user for a 2FA code and then re-call the send money function with the 2FA code.

Here is an example of how we're doing this in Quids:

Coinbase Swift SDK

Over the weekend I released the Coinbase Swift SDK I've been building alongside Quids. It is still under heavy development so expect breaking changes!



github "reddavis/Coinbase"



The framework gives you the flexibility of how you store the oAuth credentials. You will need to implement a class that conforms to the CoinbaseAPIClientAuthStore protocol. var isAuthenticated, var hasExpired and func delete() all have default implementations, so you will only need to implement var auth.

The library will handle the refreshing of tokens.

public protocol CoinbaseAPIClientAuthStore: class
    var auth: CoinbaseAPIClient.Auth? { get set }
    var isAuthenticated: Bool { get }
    var hasExpired: Bool { get }

    func delete()

public extension CoinbaseAPIClientAuthStore
    public var isAuthenticated: Bool {
        return self.auth != nil

    public var hasExpired: Bool {
        guard let unwrappedAuth = self.auth else
            return true

        return unwrappedAuth.expiresAt < Date()

    public func delete()
        self.auth = nil


Firstly you need to initialize the auth flow:

let scopes: [CoinbaseAPIClient.Scope] = [

let url = self.coinbaseAPIClient.authorizeURL(scopes: scopes)

Then you need to get the oAuth token:

let redirectURL = URL(string: "quids://coinbase/auth")!

self.coinbaseAPIClient.authenticate(code: code, redirectURL: redirectURL) { (success, error) in
    self.authCompletionHandler?(success, error)


Only a few requests are currently supported, more will be added as we add features to Quids.

public func fetchAccounts(_ completionHandler: @escaping (_ accounts: [Account]?, _ errors: [Error]?) -> Void)
public func fetchTransactions(accountID: String, completionHandler: @escaping (_ transactions: [Transaction]?, _ errors: [Error]?) -> Void)
public func fetchCurrentUser(_ completionHandler: @escaping (_ user: User?, _ errors: [Error]?) -> Void)
public func updateCurrentUser(edits: UserEdits, completionHandler: @escaping (_ user: User?, _ errors: [Error]?) -> Void)
public func createAddress(accountID: String, completionHandler: @escaping (_ address: Address?, _ errors: [Error]?) -> Void)
public func fetchExchangeRates(baseCurrencyCode: String, completionHandler: @escaping (_ exchangeRate: ExchangeRate?, _ errors: [Error]?) -> Void)


Years ago at Togethera we built and open sourced our deprecation framework for iOS. It provided a way to remotely deprecate old versions of an app. Deprecator supported hard and soft deprecations.

Soft deprecations can be used to just let the user know that the latest version of the app has some new awesome feature and hard deprecations are used to force the user to upgrade. It's not good practice, but it is useful to have as a backup incase of something going badly wrong.

The framework was left untouched and didn't work with any new versions of Swift so I have now brought it back to life! You can find it on Github.

Segmented Control

I've just released SegmentedControl; a small UI control for iOS.


In your Cartfile:

github "reddavis/SegmentedControl"


let segmentedControl = SegmentedControl() 
segmentedControl.tintColor = UIColor.blue 
segmentedControl.addButton(title: "Button A") 
segmentedControl.addButton(title: "Button B")
segmentedControl.addButton(title: "Button C") 
segmentedControl.addTarget(self, action: #selector(self.segmentedControlValueDidChange(_:)), for: .valueChanged)

Swift Framework - TableData v1

As mentioned in my Year in Review post from back in December, one of my aims of 2018 is to open source more code.

I’ve built a few private libraries over the years that I use in most of my projects. I figured releasing these would be a good place to start. The first one is the rather bland named; Table Data.

Table Data is a simple framework for building out UITableViews. I use it a lot when building preferences or settings screens.