Search code examples
iosxcodeamazon-web-servicesaws-amplifyswift-package-manager

How to exclude XCFramework from Catalyst?


Given that

There is an iOS app that needs Analytics and it's decided to use Amazon as a provider. So I added Amplify, and it works ok on iOS and on Simulator. But when building for Mac Catalyst, compiler produces a set of similar errors related to various XCFrameowrk's from Amplify bundle:

While building for Mac Catalyst, no library for this platform was found in '... artifacts/AWSiOSSDKV2/AWSCore.xcframework'.

Quick search shows that Amazon do not want to support Catalyst due to business reasons and there an open issue on this topic, and the first PR was raised 3 years ago then accidentally closed.

Attempts to avoid the error

Analytics isn't absolutely necessary feature, so I tried to remove Amplify libraries from Catalyst and keep them on iOS. I'm using this macro (which obviously is compile-time, not build-time, but some SO answers reported that it may help):

#if !TARGET_OS_MACCATALYST
import Amplify
import AWSPinpointAnalyticsPlugin
import AWSCognitoAuthPlugin
#endif

Then in Link Binary With Libraries I set Amplify-related libs to be Optional. In Filter section I removed checkmarks near Mac Catalyst, thinking that it will make them iOS-only:

Xcode Link Binary With Libraries

All this tricks combined together didn't help and I'm still getting the same errors.

Q

How to exclude XCFramework for certain unsupported platforms?

P.S.

Seems, like a new AWS SDK, which claims to support Catalyst, is emerging. But it's raw and low level, and I simply not ready to debug it and build my own Analytis framework on top of it in scope of this question.


Solution

  • I didn't find the exact answer regarding Swift PM. But I was able to fix my issue, just switched to good old (and much more flexible) cocoapods. I used cocoapods-catalyst-support to restrict Amplify to iOS only. My podfile:

    require 'cocoapods-catalyst-support'
    
    platform :ios, '14.1'
    use_frameworks!
    
    target 'MyApp' do
      pod 'Amplify'
      pod 'AmplifyPlugins/AWSPinpointAnalyticsPlugin'
      pod 'AmplifyPlugins/AWSCognitoAuthPlugin'
    
      target 'MyAppTests' do
        inherit! :search_paths
        pod 'Amplify'
        pod 'AmplifyPlugins/AWSPinpointAnalyticsPlugin'
        pod 'AmplifyPlugins/AWSCognitoAuthPlugin'
      end
    end
    
    catalyst_configuration do
      ios 'Amplify'
      ios 'AmplifyPlugins/AWSPinpointAnalyticsPlugin'
      ios 'AmplifyPlugins/AWSCognitoAuthPlugin'
    end
    
    post_install do |installer|
      installer.configure_catalyst
    end
    

    I basically did the same thing, @PaulBeusterien recommended in comments, but the framework is generated by cocoapods instead of Firebase ReleaseTooling.

    Swift Package Manager do not support platform filters, as for Feb 2022. I contacted swift forums and Amazon support and also did my own research.