Programming

Error Protocol Specializations in Swift

Published on

Earlier this week I was scrolling through my Twitter feed as usual and I found this tweet that made me realize I may have been handling errors incorrectly in Swift all my life. This prompted me to research a bit more about error handling in Swift, and it turns out there’s many specialized Error protocols you can conform to, and you should probably be using them over the default Error provided by the language. All these specializations conform to Error themselves. In this article, we will explore a few specializations we can use when dealing with errors in Swift.


Document Scanning and Text Recognition With Vision and VisionKit on iOS

Published on

It is amazing what we can do with smartphones these days. Document scanning and text recognition are nothing new. But being able to have such a functionality in our pockets is pretty neat. These days we can create apps that have such features very quickly thanks to the push Apple has been doing to promote Machine Learning and Artificial Intelligence on their devices.

Starting on iOS 11, we can natively scan documents with a system framework called VisionKit, and we can perform operations on images using a framework called Vision. It wasn’t until iOS 13 that we finally had the ability to recognize text on images ourselves using the Vision framework, without leveraging third party libraries. In this article we will explore how we can use the VisionKit framework to scan documents and the Vision framework to detect text as two separate tasks, so you can see how easy these two tasks are and you can learn to put them together.


Using the iOS Keychain with Biometrics

Published on

If you have been using the keychain on your iOS apps you may want to start using Face ID/Touch ID to let your user access your app and their data. This is a common use case but it’s very easy to do incorrectly.

Apple introduced Touch ID all the way back in 2013, and ever since then, every iOS device has come with some sort of biometric authentication method, be it Touch ID or Face ID. This has allowed developers to implement convenient unlocking into their apps to access sensitive data without having to ask for the passcode. If your app “locks” access in any way your users are probably expecting to “unlock” with their finger or Face ID, so it is your responsibility to implement in a way that is secure and can’t be vulnered.


Using the iOS Keychain in Swift

Published on

This article is an entirely rewritten version of an old tutorial I wrote years ago titled “Using the iOS Keychain”. Originally written in Objective-C, the old version has been archived but it is accessible here.

The Keychain is the place where you would store sensitive data. As secure as iOS currently is, the keychain is the right place to store passwords, authentication tokens, and other sensitive data. You should not store this kind of data in UserDefaults, even if iOS has made it harder to access that data for normal users in the latest versions.


Writing Content Blockers for iOS

Published on

A few years ago, Apple introduced the Content Blocking APIs to Safari. Using these APIs, developers are able to write extensions that allow Safari to block content users do not want to see.

Most commonly used for ads, content blockers are not really ad blockers. While they can, and commonly do, block ads, developers can write content blockers for all kind of content, including profanity, or other questionable content.

Content Blocking VS Ad Blocking.

Content Blockers are actually very limited in terms of what they can do. We cannot really achieve the level of functionality especialized extensions such as AdBlock Plus have to offer. Apple’ implementation has two main focuses in mind:


Dependency Injection with Storyboards on Apple Platforms

Published on

Every iOS developer has written a line of code like this one at least once:

class DollInfoViewController: UIViewController {
  
  var dollModel: Doll?

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
  }


}

Or in the worst case, you may have seen code like this:

var dollModel: Doll!

Then, when you want to create a view controller of that type you’d do:

// First we need to check if we can actually instantiate the view controller.
guard let dollInfoVc = storyboard?.instantiateViewController(withIdentifier: "DollInfo") as? DollInfoViewController else {
    fatalError("Unable to load view controller.")
}

// Then we pass in the data we want to work with. 
dollInfoVc.dollModel = doll

Code like this is very error prone. The worst part is that up untul iOS 13, it was pretty much necessary to pass data around from view controller to view controller. It’s not possible to do this in any different way in different iOS versions.


Quick Introduction Reflection in Swift

Published on

If you have been programming for a few years, you have undoubtedly come across the term Reflection. This feature allows us to inspect and work with the members of a type.

if this doesn’t make sense, suppose you wanted to check what members a type has. How would you do this? Ideally you’d like to iterate over its members and print them. This is a very basic application of Reflection, but it should let think of other potential uses for it.


Understanding @dynamicMemberLookup and @dynamicCallable in Swift

Published on

If you have written code in a programming language such as Python or PHP, you can find many direct comparisons to Swift. For one, Swift is statically typed, whereas PHP and Python are not - Swift is considered a safe language as it has a bunch of features to protect you against mistakes - static typing, error throwing, optionality for dealing with nulls, to name a few -, whereas PHP and Python do not.


Writing Command Line Tools in Swift Using ArgumentParser, Part 6: Releasing And Installing Our Command Line Tool

Published on

I wasn’t sure if I should include this article as part of this series. But for the sake of completion, I decided to include it. This article is very short, but it tells us how to actually install our own tool in a system so we can start using it without writing its full path.

To recap, and before I end my series in Swift’s ArgumentParser, let’s give a quick overview of everything we have learned so far:


Writing Command Line Tools in Swift Using ArgumentParser, Part 5: Tools with Asynchronous APIs

Published on

In the past four weeks we have explored many of the features available to us via ArgumentParser and how to use them. Here’s a recap of everything we learned so far:

In this article, we will not explore a feature exposed to us via ArgumentParser. Instead, we will learn how to do something very essential: Creating tools that require asynchronous APIs.