swift posts

Subscribe to just swift posts via RSS.

Implement a Gemini Protocol Client Using Network.framework

Gemini is a small protocol bulit on top of TCP and TLS that’s designed to serve as a transport mechanism primarily for text documents while lacking a great deal of the complexity of HTTP. Network.framework was introduced to Apple’s platforms in 2018 as a modern framework for dealing with network connections and building custom network protocols. So, let’s use it to build a Gemini client implementation.

Replicating Safari's Link Preview Animation

In iOS 13, Apple replaced the Peek and Pop force touch system with new context menus and previews[1]. These new previews have a fancy animation for when the appear, in which they expand out of the content and onto the screen. Back when I first replaced the previews in Tusker with the new context menus (over a year ago, shortly after WWDC19), I wanted to replicate the behavior in Safari for links and mentions in post bodies. At the time, there was pretty much zero official documentation about the new context menu APIs, so I decided to wait for either Apple to publish docs or for someone else to figure it out first. Now that WWDC20 has come and gone, and I’ve been working on it a little bit at a time for over a year, I finally have a fully working implementation.

Writing a JavaScript Syntax Highlighter in Swift

For a project I’m currently working on, I need to display some JavaScript code[1], and because I’m a perfectionist, I want it to be nice and pretty and display it with syntax highlighting. Originally, I was planning to use John Sundell’s Splash Swift syntax highlighting library (both a “(Swift syntax) highlighting library” and a “Swift (syntax highlighting) library”). It can already render to my desired output format, an NSAttributedString, and it has an interface for defining new grammars, which I thought would make it relatively easy to extend to support JavaScript. After getting started, it quickly became apparent that it wouldn’t be quite so easy. In addition to writing all the code to parse JavaScript, I’d have to go through the Splash codebase and understand a decent amount about how it works. This grew uninteresting pretty quickly, so I decided I would try just writing everything myself. My highlighting needs were fairly simple, how hard could it be?

Simple Swift Promises

Recently, I’ve been working on cleaning up the networking code in Tusker, my iOS client for Mastodon/Pleroma and I briefly played around with using the new Combine framework as well as the built in URLSession.DataTaskPublisher helper. Combine, much like SwiftUI, uses Swift’s incredibly powerful type system to great advantage because it’s a Swift-only framework. It’s quite efficient, but because there are so many generic types and implementations of different protocols, the API (in my experience) isn’t the most pleasant to work with. I was thinking about other asynchronous programming schemes and the one that came to mind as being the nicest to use was JavaScript’s Promises. It has a fairly simple API, so I started wondering how much work it would be to build something similar in Swift. Turns out: not that much.

Faking the Mongo Eval Command

One of the changes in MongoDB 4.2 was the removal of the eval command. While a reasonable security measure, this is rather annoying if you’re building an app for interacting directly with a Mongo database. If you want to be able to run commands directly on the database, you now have to go through the mongo shell. This seems straightforward, but actually getting the data back into a format that’s usable is a bit of a hassle.