Create a Captcha With SwiftUI

Figure out if you’re dealing with a human or not

Mark Lucking
Better Programming
Published in
4 min readDec 7, 2020

--

Animated captcha
An example of a captcha. GIF by the author.

Trying to figure out if you’re talking to a human being or a bot these days is becoming increasingly difficult. In recent years, the de facto method to solve this has been a captcha — a challenge/response protocol that either gives you a set of images and asks you to identify something within some of them or a string of text that has been distorted (as shown above).

I am going to focus on the latter in this article. We’re going to try to disguise a string of text such that a human can still figure out what it says, but a bot will struggle. I will present a solution to do so in SwiftUI.

Where do we begin? I published a couple of articles a while back on rotating images. They talked extensively about the rotationEffect, which is a possible starting point. I can turn and twist the numbers and letters in my text and produce a captcha version:

Example of captcha
The hexidecimal alphabet being captcha’d with rotation3DEffect

In this GIF, I have flipped the letters on every axis randomly, although I have limited the angle to 65% degrees to keep it readable. It’s OK but leaves a good deal of room for improvement. One of the gotchas I already have to solve…

--

--

Coding for 35+ years, enjoying using and learning Swift/iOS development. Writer @ Better Programming, @The StartUp, @Mac O’Clock, Level Up Coding & More