Devlog: Nov 15, 2022

I started off with some general puttering around. Still deciding what direction I want to take things.

I decided I might like to have a package to share some common stuff between apps. So I made SamKit. To start off with I added a class with some helpers to get app info—like version and build—and a View that I use to show the version info in my settings views. I have never made my own Swift library before, so a couple things I learned:

  1. You need to make things public, including the init of a View.
  2. You can load a local library, not just from GitHub.
  3. The package needs to be added under Frameworks, Libraries, and Embedded Content.

After that I did a little experiment with share sheets. SwiftUI has ShareLink now, which is quite nice. However, it appears there is no way to know if the share was successful or not. So I dug a bit more into using UIActivityViewController.completionWithItemsHandler. At first, the completion handler was never getting called. Turns out I forgot to remove the old ShareLink and call my new function instead 🤦🏻‍♂️. Classic. Then, it wasn’t getting presented. I was getting the root view controller, but it was already presenting a sheet. So I needed to get the presentedViewController from that. After that, it worked beautifully.

private func showShareSheet() {
    let activityVC = UIActivityViewController(activityItems: [idea], applicationActivities: nil)
    activityVC.completionWithItemsHandler = { (activity, completed, items, error) in
        if completed {
            withAnimation {
                idea = ""
            }
            return
        }
    }
    guard let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene else {
        return
    }
    guard let window = scene.windows.first else {
        return
    }
    window.rootViewController?.presentedViewController?.present(activityVC, animated: true)
}

Tags
devlog

Date
November 15, 2022