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? …
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:
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.
Without using Auto Layout, the position of a subview added on the view is fixed. …
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.
Create your own AWS account by following the link if you don't have one yet.
The AWS Command Line Interface (AWS CLI) is an open-source tool to interact with AWS services using commands in your command-line shell. …
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
.
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:
UIBlurEffect.Style
. We are using .light
for this example.UIVisualEffectView
is initialized with the specific height and centered with the background image. …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.
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
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
. …
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.
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. …
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…
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.
Consider a hypothetical API that sometimes returns a String
and sometimes returns an Int
for the key id
:
The most common and straightforward solution is using an Enum to map to the different cases. …
About