Swift-NIO is now available for iOS, tvOS

Recap Swift-NIO

This post is a follow-up of Setup Swift-NIO for iOS, tvOS, in that tutorial, I spent time to try to compile the SwiftNIO project for iOS, tvOS, to make use of Swift-NIO infrastructure for a custom in-app server on iOS, tvOS platforms. The procedure was done in a manual manner, which is risky and unreliable.

For anyone who has not known about Swift-NIO yet, I recap it in a few words. Swift-NIO is one of Apple open-source project which provides a reliable networking implementation. You can quickly make up a web server, socket server and so on effortlessly.

One thing to know is that initially Swift-NIO was written for MAC OS and Linux platforms. Therefore the code was completely tied up POSIX socket, and low level Networking C APIs to make the outgoing requests and listen the incoming traffic. The POSIX socket, and low-level Networking C APIs are basically good, standard and recognized across the world. However the downsides of them are:

  • Those are primitive APIs. It is hard to write, difficult to read, and maintain in later phases.
  • Not well-optimized for iOS in term of battery life.
  • Not multi-path network compatibility
  • Hard to sync up with the application life-cycle and interruption events

Now available for iOS/tvOS

Apple has spent effort to enable Swift-NIO for iOS/tvOS. Swift-NIO transport services is introduced silently. This new extension is official unleashed in mid 2019. Any developer is free to use it in the project with free of risks.

What is in Swift-NIO transport services? To be simple, Swift-NIO transport services provides the alternative implementations which use Network.framework instead. The Network.framework was introduced few years ago which is aimed to replace POSIX socket and low level Networking APIs. Hence it makes sure maximum compatible to mobile devices, and the modern platforms later on.

Steps to import Swift-NIO

Swift-NIO and Swift-NIO Transport service support Swift Package Manager, Hence it is feasibly to import those packages since XCode 11

In XCode, Firstly, Navigate to Frameworks, Libraries, and Embedded Content sections of the project, Then add packages dependency

This slideshow requires JavaScript.

Add Swift-NIO package

This slideshow requires JavaScript.

Add Swift-NIO Transport Service package

This slideshow requires JavaScript.

Now, You have done importing Swift-NIO and the Transport Service for your project.

Try out

Let’s make up a simple HTTP server.

In Xcode, navigate to  swift-nio-transport-services > sources > NIOTSHTTPServer > main

Simple HTTP sample

There is a sample code of a simple HTTP server in main.swift. You can quickly use this to try out.

Open main View Controller class, put the code of HTTP1ServerHandler there.

Put Simple HTTP Server in ViewController

Then We need to set up a sequence of procedures to start up the server

This slideshow requires JavaScript.

Test it

Let’s test the code. First, compile and run the project from XCode and leave it running.

In order to test the server, We can simply send a request in Terminal.

Use this command curl -v

After all, You should see this outcome below. Good luck.

Test simple http server


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s