Exclude pod when porting to mac with catalyst
Open your Pods-$projectname.release.xcconfig file in your project's Pods directory, and locate the OTHER_LDFLAGS line. Add [sdk=iphone*]
immediately after the variable name (as an example, mine now looks like this):
OTHER_LDFLAGS[sdk=iphone*] = $(inherited) -ObjC -l"MailCore-ios" -l"c++" -l"iconv" -l"resolv" -l"xml2" -l"z"
That conditionally sets the link options only when building iphone variants, preventing the pod from being linked on OSX. Of course as you mention, this needs to be combined with #if !targetEnvironment(macCatalyst)
and #endif
surrounding the code calling the pod or you'll get linker errors.
This allowed me to get past the same problem. (And in case you're wondering what other cool things besides conditional variables you can add to your .xcconfig files, here's a reference I found: https://pewpewthespells.com/blog/xcconfig_guide.html )
Following @ajgryc answer, I was able to make a sleek solution:
In your podfile add
post_install do |installer|
installer.pods_project.targets.each do |target|
if target.name == "Pods-[Name of Project]"
puts "Updating #{target.name} OTHER_LDFLAGS to OTHER_LDFLAGS[sdk=iphone*]"
target.build_configurations.each do |config|
xcconfig_path = config.base_configuration_reference.real_path
xcconfig = File.read(xcconfig_path)
new_xcconfig = xcconfig.sub('OTHER_LDFLAGS =', 'OTHER_LDFLAGS[sdk=iphone*] =')
File.open(xcconfig_path, "w") { |file| file << new_xcconfig }
end
end
end
end
Since Cocoapods 1.8.4
post_install do |installer|
installer.pods_project.targets.each do |target|
if target.name == "Pods-[Name of Project]"
puts "Updating #{target.name} to exclude Crashlytics/Fabric"
target.build_configurations.each do |config|
xcconfig_path = config.base_configuration_reference.real_path
xcconfig = File.read(xcconfig_path)
xcconfig.sub!('-framework "Crashlytics"', '')
xcconfig.sub!('-framework "Fabric"', '')
new_xcconfig = xcconfig + 'OTHER_LDFLAGS[sdk=iphone*] = -framework "Crashlytics" -framework "Fabric"'
File.open(xcconfig_path, "w") { |file| file << new_xcconfig }
end
end
end
end
And then in run script build phase for Fabric:
if [[$ARCHS != "x86_64"]]; then
"${PODS_ROOT}/Fabric/run" [your usual key]
fi
For the best approach of handling unsupported framweorks for Catalyst, you guys should read the solution of Fernando Moya de Rivas, he has a github with a solution here with more up to date information.
He basically said you just need to define an array of all of the libs you don't want to install on mac osx, like this: ['Fabric', 'Crashlytics', 'Firebase/Core', ...]
.
Then, your pod file can look simple as this:
# Podfile
load 'remove_unsupported_libraries.rb'
target 'My target' do
use_frameworks!
# Install your pods
pod ...
end
# define unsupported pods
def catalyst_unsupported_pods
['Fabric', 'Crashlytics', 'Firebase/Core', ...]
end
# Remove unsupported pods from your project
post_install do |installer|
installer.configure_support_catalyst
end
I have an updated solution that works for me with the following Google pods:
pod 'FirebaseUI/Auth'
pod 'FirebaseUI/Phone'
pod 'FirebaseUI/Email'
pod 'Firebase/Auth'
pod 'Firebase/Analytics'
pod 'Fabric', '~> 1.10.2'
pod 'Firebase/Crashlytics'
pod 'Firebase/AdMob'
post_install do |installer|
installer.pods_project.targets.each do |target|
if target.name.start_with?("Pods")
puts "Updating #{target.name} to exclude Crashlytics/Fabric"
target.build_configurations.each do |config|
xcconfig_path = config.base_configuration_reference.real_path
xcconfig = File.read(xcconfig_path)
xcconfig.sub!('-framework "FirebaseAnalytics"', '')
xcconfig.sub!('-framework "FIRAnalyticsConnector"', '')
xcconfig.sub!('-framework "GoogleMobileAds"', '')
xcconfig.sub!('-framework "Google-Mobile-Ads-SDK"', '')
xcconfig.sub!('-framework "GoogleAppMeasurement"', '')
xcconfig.sub!('-framework "Fabric"', '')
new_xcconfig = xcconfig + 'OTHER_LDFLAGS[sdk=iphone*] = $(inherited) -framework "FirebaseAnalytics" -framework "FIRAnalyticsConnector" -framework "GoogleMobileAds" -framework "GoogleAppMeasurement" -framework "GoogleUtilities" "-AppMeasurement" -framework "Fabric"'
File.open(xcconfig_path, "w") { |file| file << new_xcconfig }
end
end
end
end