I’m building an application that shares most of the code between macOS and iOS versions (targeting macOS 11 and iOS 14). UIKit for Mac seems like a natural choice to help with this. Unfortunately, one of the libraries uses the Process
type under the hood. Building it produces "Cannot find type Process
in scope" error when a dependency on it is added and when targeting macOS. I’m fine with excluding this library for iOS, but I still need to link with it on macOS while keeping the ability to use UIKit on all platforms.
I’ve selected this library to be linked only for macOS in Xcode, but this has no effect and the same build error persists. Also, I’m getting this error without adding a single import SwiftLSPClient
statement in the app, so I don’t think conditional imports would help in this case.
What would be the best way to resolve this issue within the constraints listed above?
2
Answers
This is a messy solution but I know it works: Add a “Mac bundle” to your Catalyst app and import the MacOS-only framework with that.
Here’s a guide to creating and loading a Mac bundle: https://medium.com/better-programming/how-to-access-the-appkit-api-from-mac-catalyst-apps-2184527020b5
Once you have the bundle, you can add Mac-only libraries and frameworks to it. You’ll have to bridge data and method calls between the bundle and your iOS app, but it’s manageable.
I created a LSPCatalyst class in my Mac Catalyst app to replace the MacOS LanguageServerProcessHost. To make that work, I replaced the
process
property with aprocessProxy
that accesses the process instance in a MacOS bundle using the FoundationApp protocol as explained below.Following @Adam’s suggestion, I created a MacOS bundle to proxy for the process instance. You follow the same idea as he pointed to for AppKit access from Catalyst apps, but you just need Foundation to get access to Process. I called the bundle FoundationGlue and put everything in a FoundationGlue folder in my Xcode project. The bundle needs an Info.plist that identifies the principal class as "FoundationGlue.MacApp", and the MacApp.swift looks like:
The corresponding header I called FoundationApp.h looks like:
And the FoundationAppGlue-Bridging-Header.h just contains:
Once you have the bundle built for MacOS, add it as a framework to your Mac Catalyst project. I created a Catalyst.swift in that project for access to the FoundationGlue bundle functionality::
Then, you use it from your app like: