Sharing a framework between iOS and watchOS

The biggest promise of TCA is that it's composable and allows for apps to be split into small, maintainable frameworks. I build an app using that approach and now want to add a watchOS version of it. After I added the extension and tried running it, I got the following error:

Building for watchOS Simulator, but the linked framework is building for iOS Simulator. You may need to configure to build for watchOS Simulator.

After searching for this error, it appears that it's not possible to share frameworks between iOS and watchOS. My question is, did I miss anything or is this really the case? And if it is, will I need to move all my code in one framework in order to support multiple platforms?

Source compatibility is the promise of TCA, not binary compatibility. You need to compile your code to build watchOS compatible frameworks. You should not need to change the source, but, the binaries have to compiled for the separate platforms (different CPU architectures, hardware limitations, etc.).

Apple provides xcframeworks so you can package various framework binaries for each platform (macOS, iOS, iOSSimulator, watchOS, watchOSSimulationr, tvOS) into a convenient overall framework that you can link with your application. The Xcode linker will select the appropriate framework depending on the destination platform.

Another option to an XCFramework particularly if you don't plan to distribute a binary is a Swift Package.

Move all the code you want to share between iOS and watchOS in to the package, add the package to Xcode, then link against it in your iOS and watchOS targets.

Terms of Service

Privacy Policy

Cookie Policy