---
title: "Standard Setup bites - Little Bites of Cocoa"
description: "Little Bites of Cocoa about Standard Setup: short tips and techniques for iOS and Mac development, each a brief explanation of one concept, technique or tool."
url: "https://littlebitesofcocoa.com/topics/70-standard-setup"
author: "Jake Marsh"
---
# Bites about: [Standard Setup](https://littlebitesofcocoa.com/topics/70-standard-setup)

Topics

- [Standard Setup](https://littlebitesofcocoa.com/topics/70-standard-setup)

## [#228: Standard Setup - Authentication with Moya 🚪](https://littlebitesofcocoa.com/228-standard-setup-authentication-with-moya)

 Wednesday April 6th, 2016

Topics

- [Standard Setup](https://littlebitesofcocoa.com/topics/70-standard-setup)

Last week we in [Bite #223](https://littlebitesofcocoa.com/223-standard-setup) and [#224](https://littlebitesofcocoa.com/224-standard-architecture) we covered an example *"Standard Setup"* for new **apps**. Since those **Bites** were published, readers have been asking many questions, requesting more information about how all these things fit together.

Today we'll answer one of those questions in more depth: Authentication. Let's dive in.

**Who?** [Again](https://littlebitesofcocoa.com/223-standard-setup) it's me, [Jake Marsh](https://twitter.com/jakemarsh)! I write [this thing](https://littlebitesofcocoa.com/about).

Like we covered before, the heart of our HTTP "stack" is built around the [Moya framework](https://github.com/Moya/Moya) ([Bite #150](https://littlebitesofcocoa.com/150-standardizing-network-abstractions-with-moya)). One of the neat parts about [Moya](https://github.com/Moya/Moya) is how it lets us customize how it works. One way is through an **"request closure"**. As the name implies, this closure's job is to create the actual **NSURLRequest** that will be sent.

Here's an example implementation. Inside we can set **HTTP headers**, and modify the **URL request** in any way we want. This makes adding authentication super simple.
 Here we set a "Bearer" token as the **Authorization** header.

```swift
let requestClosure = { (endpoint: RxMoya.Endpoint<StewardAPI>, done: NSURLRequest -> Void) in
  let request = endpoint.urlRequest.mutableCopy() as! NSMutableURLRequest

  if let token = SharedKeycard.token {
    request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
  }

  done(request)
}
```

The **SharedKeycard** is simply a singleton that holds the auth token:

```swift
func lookForTokenInKeychain() -> String? { /* not important */ }

public class Keycard : NSObject {
  public var token: String? = nil

  public override init() {
    super.init()
    token = lookForTokenInKeychain()
  }
}

public let SharedKeycard = Keycard()
```

Finally, we simply pass in our custom **request closure** when creating our **Moya "provider"**:

```swift
let StewardAPI = RxMoyaProvider<StewardAPI>(
  requestClosure: requestClosure
)
```

A quick shout out to reader [Justin Williams](https://twitter.com/justin) who [wrote in](https://twitter.com/justin/status/716729616879063044) asking for more information about how authentication fits into this puzzle.

Have any questions about how all this fits together? Want to see some other developer's example setup? Don't be shy! [hello@littlebitesofcocoa.com](mailto:hello@littlebitesofcocoa.com). Any and all comments/questions are welcome!

Topics

- [Standard Setup](https://littlebitesofcocoa.com/topics/70-standard-setup)

## [#224: Standard Architecture 🏣](https://littlebitesofcocoa.com/224-standard-architecture)

 Thursday March 31st, 2016

Topics

- [Standard Setup](https://littlebitesofcocoa.com/topics/70-standard-setup)

After [Bite #223's "Standard Setup"](https://littlebitesofcocoa.com/223-standard-setup), many readers asked for more detail about the actual architecture involved. Today we'll take a look at an example request, model, and a bit of how data "flows" from the web in to **views** in the **app**.

**Who?** [Again](https://littlebitesofcocoa.com/223-standard-setup) it's me, [Jake Marsh](https://twitter.com/jakemarsh)! I write [this thing](https://littlebitesofcocoa.com/about). Writing in the first person feels quite strange at this point.

Let's start with a [Moya](https://github.com/Moya/Moya) setup for one endpoint. I love this convention. It standardizes "what goes where" for HTTP verbs, params, etc. I'm never trying invent, just define it, and move on.

```swift
public enum StewardAPI : RxMoya.TargetType {
  case Checkins(start: Int)
}

extension StewardAPI {
  public var method: RxMoya.Method { return .GET }
}

extension StewardAPI {
  public var path: String {
    switch self {
      case .Checkins: return "/checkins"
    }
  }
}

extension StewardAPI {
  public var parameters: [String: AnyObject]? {
    switch self {
    case .Checkins(let start):
      return ["start": start, "maxResults" : 50]
```

Next, to actually use this API, I have an small extension I add with a global function called `fetch`. This returns an `Observable<Decodable>`.

This "calls" the HTTP endpoint, and expects some JSON that can be transformed into one of my defined `Decodable` types:

```swift
fetch(.Checkins(start: start), type: [Checkin].self).subscribe { event in
  switch event {
    case .Next(let checkins): print(checkins)
    case .Error(let error): print(error)
    default: break
  }
}.addDisposableTo(rx_disposeBag)
```

Normally, I wouldn't switch here, I'd map this into a type I use called a "Presenter" (people also call it a "Decorator" or a "View Model").

It is immutable and contains boring business logic. This is what actually gets handed to my view controllers and views:

```swift
fetch(.Checkins(start: start), type: [Checkin].self)
  .map { ListItemPresenter(item: $0) }
```

Please send any questions about any of this to [hello@littlebitesofcocoa.com](mailto:hello@littlebitesofcocoa.com). Happy to go in to further detail if there is interest here.

Topics

- [Standard Setup](https://littlebitesofcocoa.com/topics/70-standard-setup)

## [#223: Standard Setup 🛠💭](https://littlebitesofcocoa.com/223-standard-setup)

 Wednesday March 30th, 2016

Topics

- [Standard Setup](https://littlebitesofcocoa.com/topics/70-standard-setup)

Everyone does their iOS development a *little bit* differently. Today we'll take a look at one developer's example "default" depdency setup and describe the choices within. Let's get started.

**Who?** It's me, [Jake Marsh](https://twitter.com/jakemarsh)! I write [this thing](https://littlebitesofcocoa.com/about).

I use [Moya](https://github.com/Moya/Moya) for all of my network requests. It's a fantastic tool that allows you to abstract your requests away using **Swift enum cases**. **Associated values** are used to pass in params. Love the convention this offers, feels like *Swift on Rails*. Moya was covered in [Bite #150](https://littlebitesofcocoa.com/150-standardizing-network-abstractions-with-moya).

For my JSON decoding I like [Decodable](https://github.com/Anviking/Decodable) at the moment, it again offers a nice convention of "define a type, define its deserialization below". I also like how it uses native **Swift errors** combined with **Optionals** to gracefully fallback when deserializing. [Decodable](https://github.com/Anviking/Decodable) was covered in [Bite #86](https://littlebitesofcocoa.com/86-decodable).

I use [HanekeSwift](https://github.com/Haneke/HanekeSwift) for image downloading/caching, it's fast and lightweight. I use [RxSwift](https://github.com/ReactiveX/RxSwift) ([Bite #162](https://littlebitesofcocoa.com/162-reacting-with-rxswift)) for reactive things. I prefer its API over [Reactive Cocoa](https://github.com/ReactiveCocoa/ReactiveCocoa) ([Bite #127](https://littlebitesofcocoa.com/127-introduction-to-reactivecocoa)). Both are great. Use what you like.

Who's standard setup do you want to see? Send it in: [hello@littlebitesofcocoa.com](mailto:hello@littlebitesofcocoa.com)

 

 
