Last week we in Bite #223 and #224 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.
Like we covered before, the heart of our HTTP "stack" is built around the Moya framework (Bite #150). One of the neat parts about 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.
The SharedKeycard is simply a singleton that holds the auth token:
funclookForTokenInKeychain()->String?{/* not important */}publicclassKeycard:NSObject{publicvartoken:String?=nilpublicoverrideinit(){super.init()token=lookForTokenInKeychain()}}publicletSharedKeycard=Keycard()
Finally, we simply pass in our custom request closure when creating our Moya "provider":
A quick shout out to reader Justin Williams who wrote in 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. Any and all comments/questions are welcome!
After Bite #223's "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 it's me, Jake Marsh! I write this thing. Writing in the first person feels quite strange at this point.
Let's start with a 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.
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.
I use 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.
For my JSON decoding I like 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 was covered in Bite #86.
I use HanekeSwift for image downloading/caching, it's fast and lightweight. I use RxSwift (Bite #162) for reactive things. I prefer its API over Reactive Cocoa (Bite #127). Both are great. Use what you like.