swift posts

Subscribe to just swift posts via RSS.

Adopting TextKit 2

With iOS 16, Apple switched on TextKit 2 for UITextViews. But, if you access any of the TextKit 1 objects on the text view, it will automatically fall back to a compatibility mode. All of the work I did to mimic Safari’s link context menu animation was, of course, using the TextKit 1 APIs, so it was blocking me from fully adopting TextKit 2. So, here’s how to update that code.

Update: Swift Packages and Frameworks

A while ago I wrote about some trouble I had getting Xcode to cooperate with my efforts to bring my app file size back under control after adding a new Swift Package dependency. Well, I’m happy to say I finally have: the most recent TestFlight build of Tusker has a 6.7MB install size, down from 25MB.

Ultimately I did take the route of turning my framework into a Swift Package. I revisited it because I noticed in another project that local packages inside the same folder as the main project worked perfectly fine. The only difference I found was that the project where it worked used only an .xcodeproj, whereas Tusker used an .xcworkspace. So, I deleted the (for unrelated reasons, no longer necessary) workspace and found that, after quitting and relaunching Xcode, the local package worked perfectly fine.

I briefly started writing a feedback report, but upon further testing I found that xcworkspaces in general weren’t the problem—a new project and workspace worked fine. So, I gave up trying to reproduce it and assumed there was just something weird about the 3.5 year old workspace.

Swift Packages and Frameworks

Tusker is divided up into two main parts: the app target itself and a separate framework which encapsulates everything that deals with the Mastodon API. I recently added a Swift Package to the app for uninteresting reasons. But, because the package is used both by the framework as well as the app itself, this caused a surprising number of problems.

Re-Fixing WKWebView Scroll Indicators

As my luck would have it, just a few weeks after I published my last post on this topic, the iOS 15.4 beta came out which broke that hack and once again made my scroll indicators invisible in dark mode.

Using lol-html (or any Rust crate) in Swift

I recently started building a new iOS app and found myself with a need to parse HTML in order to extract some information. My goto tool for this in the past has been SwiftSoup. In this app, I have to deal with larger documents than I’d used it for previously, and unfortunately, its performance leavse something to be desired. Much of the issue comes from the fact that I only want to extract the first paragraph of a document, but SwiftSoup always needs to parse the entire thing—for large documents, potentially a lot of unnecessary work[1]. And, as far as I could find, there are no streaming HTML parsers written in Swift. One I did find, however, was CloudFlare’s lol-html. It’s specifically designed for speed and low latency, exactly what I want. But it’s written in Rust.