SPM library for iOS and macOS

I'm trying to create an SPM library that can be used on both iOS and macOS for UI purposes. The library will present an abstract API to consuming applications, but internally use either UIKit or AppKit as appropriate. What's the best way to do this?

So far the best solution I've found is to wrap #if canImport(UIKit) and #if canImport(AppKit) around every single file, but that gets a little tiring. I tried separating things out into different libraries, but the platform specifier for SPM is at a Package level, not a library or target level... so that seems to imply I'll have to create 2 separate libraries which has it's own problems.

Ideally I'd like to keep all the code in the same repository as it'll be easier to ensure API compatibility across the two platforms. Anyone else have any other solutions?

I did something like that with Swift-CAN-Anywhere, which — depending on the platform — itself depends on either a Linux or a macOS package and provides a common API on top of that.

1 Like