How URLSession works and how to stub responses by subclassing URLProtocol

aerial photo of car driving on a highway next to the ocean
aerial photo of car driving on a highway next to the ocean
Photo by Pat Kay on Unsplash

iOS apps interact a lot with the network. They read or write state to or from the server, and fetch data, images, audios, and videos from remote. To protect and verify the network layer, we write unit tests around it. Sadly, if we write tests that rely on networking, they will be slow and unstable. And to be fair, they are not really unit tests but more integration tests.

How can you stub the network request and response and isolate the unit test’s code from networking without installing any third-party libraries? …

External displays, Rosetta 2, Homebrew, and more

Laptop in the dark
Laptop in the dark
Photo by Ales Nesetril on Unsplash.

The first Apple silicon ARM-based M1 chip has received rave reviews due to its mind-blowing performance and efficiency. I’m impressed by the incredible performance and “System on a Chip” architecture that integrates CPU, GPU, unified memory, Neural Engine, etc.

I was so excited when I received my first M1 chip MacBook Air:

NSLayoutConstraint, Visual Format Language, and more

Plate of fruit
Plate of fruit
Photo by Brenda Godinez on Unsplash.

To create an adaptive UI that responds appropriately to changes in screen size and device orientation, we use Auto Layout, the constraint-based layout system. This article gives an overview of five different approaches to adding constraints programmatically.

Why Auto Layout?

Without using Auto Layout, the position of a subview added on the view is fixed. …

Quickly set up WebSockets API service with Serverless on AWS

Image for post
Image for post
Photo by Jeremy Bishop on Unsplash

On some projects, we need the client to be able to communicate with the server in real-time. The Websocket protocol works over the TCP socket connection and provides the ability of real-time, bi-directional communication between client and server.

The article gives a simple guide to set up the WebSockets API service on AWS, by using the serverless framework, API Gateway, and DynamoDB.


There is some basic knowledge and information you need before we get going.

AWS account

Create your own AWS account by following the link if you don't have one yet.

Configuring the AWS CLI

The AWS Command Line Interface (AWS CLI) is an open-source tool to interact with AWS services using commands in your command-line shell. …

Using UIBlurEffect, CIFilter, and Metal with GPU acceleration

Blurry rainy window
Blurry rainy window
Photo by Wenniel Lun on Unsplash.

It's a fairly common requirement to apply a blur effect to iOS content. This article introduces three different approaches by using three different iOS frameworks: UIKit, CoreImage, and Metal.

1. Using UIBlurEffect to Blur Image

UIBlurEffect is a neatly designed UI element in UIKit. By using it along with UIVisualEffectView, a visual effect view is added on top of the background content.

To add the blur effect, do the following:

Here’s what is happening:

  • There were a bunch of blur styles introduced in UIBlurEffect.Style. We are using .light for this example.
  • The instance of UIVisualEffectView is initialized with the specific height and centered with the background image. …

‘swift’, ‘swiftc’, and ‘lldb’

Generic stock photo of code on a laptop screen
Generic stock photo of code on a laptop screen
Photo by Ilya Pavlov on Unsplash

Sometimes when writing a block of Swift code, I don't bother to launch Xcode. Running the Swift commands swift and swiftc from the terminal are more lightweight and convenient. To debug and set a breakpoint, lldb does the job.

1. ‘swift’ Command to Get the Swift Version

After installing the latest version of Swift from the download page, I’ll run the command to verify I’m running the expected Swift version:

$ swift --version

is vs. isKind(of:) vs. isMember(of:), append() vs. appending(), isEmpty() vs. count==0, and more

Smartphone on a sheer sheet.
Smartphone on a sheer sheet.
Photo by David Grandmougin on Unsplash.

is vs. isKind(of:) vs. isMember(of:)

is is the type check operator that checks whether an instance is of a certain type. It can be used to check the instance of class:

The is operator can also be used to check the instance of a subclass of class:

The is operator can be used to check the instance of struct:

isKind(of:) works with class and adopts the NSObjectProtocol. It checks if the given value is an instance of class or an instance of any class that inherits from that class.

isMember(of:) works with class and adopts the NSObjectProtocol. …

Secure sensitive information in your app from screenshots and screen recordings

Arm taking a selfie.
Arm taking a selfie.
Photo by Jona Friedri on Unsplash.

iOS 14 comes with a new feature called Back Tap. It lets you customize double and triple taps on the back of your iPhone to trigger shortcuts, accessibility, scroll gestures, and system actions, including screenshots. It doesn’t make a big splash but is super useful. I have seen many users who are excited by the convenience of the tap-for-screenshot.

As developers, we work on apps that may have sensitive content: selfies, pictures with families, payment details, copyright content, and confidential messages. How to protect our apps and secure users’ sensitive information from screenshots and screen recordings is a big concern.

1. Build Our Sample Selfie App

Say we are building an app named Selfie for sharing selfies with friends and connections that are not intended to be captured and posted to the public. …

Understand how to use the attributes correctly and efficiently

Image for post
Image for post
Photo by Nathan Dumlao on Unsplash.


It’s normally used as an escaping closure passed as a function argument. It exists in the memory when the function ends and then gets executed.

When defining an optional closure as a property, it’s already escaping by default.

The escaping closure can be stored for later use:

Or it can be executed after the function ends:


@unknown was introduced with @frozen and @nonfrozen enumerations. A non-frozen enum may gain new enumeration cases in the future. For instance, the UILayoutConstraintAxis contains two axes at the moment: AxisHorizontal and AxisVertical:

It may gain another case axisZ later:

To safely avoid the compile…

Customize your Codable implementations to suit different advanced scenarios

Image for post
Image for post
Photo by Laurenz Heymann on Unsplash.

Swift 4 introduced the Codable protocol to decode/encode JSON objects. Codable is tightly integrated into the Swift toolchain. It is widely used on client-side and server-side Swift.

Sometimes, we meet challenges that require us to customize how we use Codable to suit different advanced scenarios. Here are three hard challenges I’ve met when using Codable and my clean code solutions.

Multiple Value Types for the Same Key

A hypothetical API

Consider a hypothetical API that sometimes returns a String and sometimes returns an Int for the key id:

MultipleTypes.json by Eric Yang

The most common and straightforward solution is using an Enum to map to the different cases. …


Eric Yang

Senior iOS engineer🚀, double degrees with Computer Science⚡Content writer? Why not!🚀

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store