Skip to content

iOS Development – Week 8

And just like that it is already Week 8 of my iOS Development Bootcamp. After having looked at concurrency in Week 7, this time was all about making network calls and as an added bonus we looked into SwiftUI animation.

Networking – URLSession

In many applications, we need to retrieve or put data in a server – think of some of your own usage with your favourite apps: updates to your social media status, or download remote files to your device. All this is happening by making network calls and Apple has us covered with URLSession offering an API that is easy-to-use, it is flexible and has several features that many developers use on a regular basis. This, combined with Codable makes is possible for us to convert Swift objects to JSON for sending and receiving data which in turn can be converted back to Swift objects.

A URLSession can be thought of as a tab in your browser. For instance, when you go to a web page, the browser will make a series of requests to fetch all the assets that contribute to that page. Of course, you’ll quickly find that URLSession allows you to more than fetch data. URLSession comes with a variety of tasks for different jobs.

Just like classes, actors are reference types that represent a shared mutable state. Actors, however, prevent concurrent access to their state, so only one method can access and modify the state of a thread.

URLSession

It is a collection of related tasks that can be configured to our needs: you could configure all your tasks to run in the background, or you could also configure your tasks to run in the equivalent of privacy mode in browsers; that is, it doesn’t cache anything, or store credentials or any session related data to disk.

This is managed by a URLSessionConfiguration object with three types:

  • Default – uses a persistent disk-based cache, except when the result is downloaded to a file. It stores credentials in the user’s keychain. And It uses default values for its properties, unless you customise it
  • Ephemeral – like default, but it doesn’t store cookies, credentials, or cached data to disk. You can think of it like creating a private window in Safari or your preferred browser
  • Background – t can transfer data while the app runs in the background. It also hands control of transfers over to the system, which handles the transfers in a separate process

Changes to the object need to be made before creating the instance. Some configuration options include:

  • allowsConstrainedNetworkAccess – Allows connections in loa data mode
  • allowsExpensiveNetworkAccess – Allows what the OS considers a network interface or connection e.g. cellular connection or personal hotspot

To create a session we need an instance of URLSessionConfiguration. We can then use the session to create URLSessionTask instances to transfer data to or from a server on the network.

To monitor progress or handle authentication challenges you can implement delegate methods. There are three concrete subclasses of URLSessionTask:

  1. URLSessionDataTask – A data task returns the response as an object in memory.
  2. URLSessionUploadTask – An upload task is very similar to a data task, but makes it easier to provide a request body.
  3. URLSessionDownloadTask – A download task doesn’t return the response in memory, but writes the data to a file and returns the location of the file.

When you start a task (sync), they are created in a waiting state. To start the task, you must call the resume method.

SwfitUI Animation

The other aspect that we covered this week is animation as a way to wow our users, and make our app look and feel unique. Practically speaking, animation can grab a user’s attention, and allow them to focus on what’s most important. It can help your users intuitively understand how to navigate your app or alert them to important changes.

Animation is a change in value or state over time. That’s it! Animating things with SwiftUI is made easier as we leverage the fact that we describe our user interface declaratively and leave the rendering to the framework. Each of the views we declare for our UI (like text labels, images or shapes) adheres to the View protocol. View requires each view struct to feature a property called body.

Any time you change your data model, SwiftUI asks each of your views for their current body: because they might change according to your latest changes. It then builds the view hierarchy to render onscreen. In a sense, SwiftUI makes “snapshots” triggered by changes in the data model. Since SwiftUI can identify all views in your view hierarchy, it’s not difficult for it to animate any changes to your views.

Take a look at the code below:

import SwiftUI

struct ContentView: View {
  private struct AnimationData: Equatable {
    let offset: CGSize
    let color: Color

    static let array: [Self]  = [
      .init(
        offset: .init(width: 0, height: 0),
        color: .green
      ),
      .init(
        offset: .init(width: 100, height: 0),
        color: .blue
      ),
      .init(
        offset: .init(width: 100, height: -100),
        color: .red
      ),
      .init(
        offset: .init(width: -100, height: -100),
        color: .orange
      ),
      .init(
        offset: .init(width: -100, height: 0),
        color: .yellow
      )
    ]
  }

  @State private var animationData = AnimationData.array[0]

  var body: some View {
    Circle()
      .scaleEffect(0.5)
      .foregroundColor(animationData.color)
      .animation(.default, value: animationData)
      .offset(animationData.offset)
      .padding()
      .onAppear {
        for (index, data) in AnimationData.array.enumerated().dropFirst() {
          DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(index)) {
            animationData = data
          }
        }
      }
  }
}

We are using the values of in the array to animate a circle that moves around the screen (position given by the offset) and changes color. We dispatch the actions of our for loop to a queue and the end result is a great animation.

There are many things that we can do with this and as such I implemented a couple in my Jelly Belly app.

Jelly Belly – Update

The tasks this week in terms of networking are mainly backend work. The aim was to request dish information from an API and make sure that the data came back. All that is being processed and currently printing information to the console. Not a lot to show you there, but while the calls are being made, the app displays an animation showing the logo of the app and the name. In this case I have been trying to simulate the first letter “e” in “JellyBelly” to animate with a circle… not quite there yet, but so far I like the result:

I also added a second animation, trying to show a Pacman like line moving around the logo in my Welcome tab. Tapping the logo also changes the scale and tint in the logo:

Related Posts

2 thoughts on “iOS Development – Week 8”

  1. Pingback: iOS Development – Week 1 – Quantum Tunnel

  2. Pingback: iOS Development – Week 9 – Quantum Tunnel

Comments are closed.