Serverless SwiftUI

Blumareks
Zero Equals False
Published in
4 min readJan 19, 2021
A small Hello World example with Serverless Swift and SwiftUI

This is just a short blog on creating your first SwiftUI Hello World application with Serverless Backend. It takes just 10 minutes (provided installed Xcode, Cocoapods, and having a free account for Serverless — here IBM Cloud Functions).

In Summer of 2020 I published a book on Serverless Swift — check the book here: https://www.apress.com/us/book/9781484258354 . It is the best time to update the examples here for Serverless SwiftUI, isn’t it?

This is a good introduction to Serverless SwiftUI, even it might be your first attempt with Swift, and/or Serverless.

There are 3 steps to say Hello World in Serverless SwiftUI:

Step 1. Get your SwiftUI app running

Step 2. Create your Serverless hello world greeting

Step 3. Connect the app to Serverless backend and smile!

So let’s begin. By the way — you can watch the free recording here: https://www.crowdcast.io/e/getting-started-with-ios

Prereqs:

  • free — LITE IBM Cloud account — sign in or sign up to IBM Cloud for your free LITE account
  • XCode running on MacOS

You can use this link to invoke IBM Cloud registration/signup/login page (this URL gives an author = me some brownie points):https://ibm.biz/Bd2CUa

STEP 1.

Simply clone the app from github (LAB 1):

git clone https://github.com/blumareks/serverless-swiftui.git
cd serverless-swiftui/lab1/ServerlessButton/

You can now open the app ServerlessButton.xcodeproj and test it.

XCode view of the app
The app results

Congratulations! You have created your SwiftUI app.

Let’s create a Serverless backend. Check out the step 2.

STEP 2.

This step is very simple, and you can create a lot of Serverless actions beyond this example. Check my book on Serverless Swift to learn about the typical Use Cases.

First create an account with IBM Cloud — click here (if you haven’t done it before): https://ibm.biz/Bd2CUa

Create an account, verify your email, and you are ready!

Second select Serverless Functions from the menu.

You can also click on the hamburger — the menu icon top left — to see the expanded descriptions.

Now you can start creating — mind to check if you have a Namespace in Cloudfoundry — it would like like on the screenshot:

Now you can start creating your first Serverless Action

You will use a template to create a “Hello World” action. Since we are writing the app in Swift, you might pick Swift, but you are able to use many other languages (HelloWorld as an example was written just in node.JS, Python, Swift, and PHP).

The flow of getting the Serverless action invoked.

When you invoke a Serverless Action for the first time it takes some time to “warm up” the action. This is described in my book in details and how to warm your actions up — since it is unwanted effect. And the result would be something like this:

The results of a Serverless action — check the activation ID — it allows you to trace the logs, etc.

Congratulations! You have created and invoked your first Serverless Action in Swift!

Let’s now connect a SwiftUI app to Serverless Swift action.

STEP 3.

This is pretty simple step. You have all the hard work done, now just some configuration steps.

Copy the key and secret from the IBM Cloud Functions — iOS app — step 4: https://cloud.ibm.com/functions/learn/ios-sdk

The key and secret details to be copied.

Now go to the lab 3 of your cloned github repository and install dependencies using pod install.

cd ../../lab3/ServerlessButton/
pod install

Analyzing dependencies
Pre-downloading: `OpenWhisk` from `https://github.com/apache/incubator-openwhisk-client-swift.git`, tag `0.3.0`
Downloading dependencies
Installing OpenWhisk (0.3.0)
Installing SwiftyJSON (4.3.0)
...
Pod installation complete! There are 2 dependencies from the Podfile and 2 total pods installed.

Now you can open the file associated with the created Cocoapod workspace: ServerlessButton.xcworkspace

Open the ContentView.swift file and update the key and secret by pasting it from the IBM Cloud from step above. Also update your namespace, package, and action name.

Update the key, secret, namespace, package, and action details.

You are ready to run it! (mind that the first run would need to warm up the action!)

Amazing when getting the hello world results!

Congratulations! You made your SwiftUI app connected to Serverless backend written in Swift! Keep on coding!

The recording of setting up the backend is shown here: https://www.crowdcast.io/e/getting-started-with-ios

Please checkout Lennart Frantzell’s and my book on Serverless Swift: https://www.apress.com/us/book/9781484258354
Thank you for reading this blog post/book. Follow me on Twitter:

--

--

Blumareks
Zero Equals False

I am a technology advocate for autonomous robots, AI, mobile and Internet of Things - with a view from both the enterprise and a robotics startup founder.