Exclude pod when porting to mac with catalyst - ios

Porting apps to mac is finally possible thanks to Catalyst, problem is, numerous pods don't support AppKit.
Most common one would be Crashlytics / Firebase.
In [...]/Pods/Crashlytics/iOS/Crashlytics.framework/Crashlytics(CLSInternalReport.o), building for Mac Catalyst, but linking in object file built for iOS Simulator, file '[...]/Pods/Crashlytics/iOS/Crashlytics.framework/Crashlytics' for architecture x86_64
Since it's a recent topic, I couldn't find doc on how to remove a pod from my build for macOS but keep it for iOS and iPadOS.
It is possible to use in code:
#if !targetEnvironment(macCatalyst)
// Code to exclude for your macOS app
#endif
But that one part of the problem, the other part is to link the pod only for iOS...
What would be the easiest/best course of action when the library is not vital for macOS but still wanted on iOS?

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

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

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 )

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

With cocoapods 1.8.4, I had to adapt #AncAinu's excellent answer as follows:
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

Based on what has already been discussed here... here is my solution for projects with multiple targets. It basically is validating the usage of the libs on each target instead of following the target name.
post_install do |installer|
installer.pods_project.targets.each do |target|
# handle non catalyst libs
libs = ["FirebaseAnalytics", "Google-Mobile-Ads-SDK"]
target.build_configurations.each do |config|
xcconfig_path = config.base_configuration_reference.real_path
xcconfig = File.read(xcconfig_path)
values = ""
libs.each { |lib|
if xcconfig["-framework \"#{lib}\""]
puts "Found '#{lib}' on target '#{target.name}'"
xcconfig.sub!(" -framework \"#{lib}\"", '')
values += " -framework \"#{lib}\""
end
}
if values.length > 0
puts "Preparing '#{target.name}' for Catalyst\n\n"
new_xcconfig = xcconfig + 'OTHER_LDFLAGS[sdk=iphone*] = $(inherited)' + values
File.open(xcconfig_path, "w") { |file| file << new_xcconfig }
end
end
end
end
It outputs something like this
Generating Pods project
Found 'Google-Mobile-Ads-SDK' on target 'Pods-TheApp'
Found 'FirebaseAnalytics' on target 'Pods-TheApp'
Preparing 'Pods-TheApp' for Catalyst
Found 'Google-Mobile-Ads-SDK' on target 'Pods-TheApp-TheAppTests'
Found 'FirebaseAnalytics' on target 'Pods-TheApp-TheAppTests'
Preparing 'Pods-TheApp-TheAppTests' for Catalyst
Found 'Google-Mobile-Ads-SDK' on target 'Pods-TheApp-TheApp_iOS_UI_Tests'
Found 'FirebaseAnalytics' on target 'Pods-TheApp-TheApp_iOS_UI_Tests'
Preparing 'Pods-TheApp-TheApp_iOS_UI_Tests' for Catalyst
Found 'Google-Mobile-Ads-SDK' on target 'Pods-TheAppIntentsExtension'
Found 'FirebaseAnalytics' on target 'Pods-TheAppIntentsExtension'
Preparing 'Pods-TheAppIntentsExtension' for Catalyst
Found 'Google-Mobile-Ads-SDK' on target 'Pods-TheAppTodayExtension'
Found 'FirebaseAnalytics' on target 'Pods-TheAppTodayExtension'
Preparing 'Pods-TheAppTodayExtension' for Catalyst

Related

Expo - React Native Build Failed (Starting from Xcode 14, resource bundles are signed by default....)

I'm trying to update an application built with expo + react native and I encounter the last problem.
expo.dev
Visual Code terminal
🍎 iOS build failed:
Starting from Xcode 14, resource bundles are signed by default, which requires
setting the development team for each resource bundle target.
To resolve this issue, downgrade to an older Xcode version using the "image" field in
eas.json, or turn off signing resource bundles in your Podfile:
https://expo.fyi/r/disable-bundle-resource-signing
Learn more: https://docs.expo.dev/build-reference/infrastructure/#ios-build-server-
configurations
I tried to solve this problem by cleaning cache and reinstalling all pods.
I went to the permissions in xcode, I tried logging in with an apple developer account but still the same.
I tried to see the changes in this link https://expo.fyi/r/disable-bundle-resource-signing but it is very different from mine, I made the changes but all app is broken when i try to build.
Expo Version: 43.00
cocoapods: "1.11.2"
eas cli version: ">= 0.38.1"
xCode Version: 13.2.1
Podfile
require File.join(File.dirname(`node --print
"require.resolve('expo/package.json')"`), "scripts/autolinking")
require File.join(File.dirname(`node --print "require.resolve('react-
native/package.json')"`), "scripts/react_native_pods")
require File.join(File.dirname(`node --print "require.resolve('#react-native-
community/cli-platform-ios/package.json')"`), "native_modules")
platform :ios, '12.0'
require 'json'
podfile_properties = JSON.parse(File.read('./Podfile.properties.json')) rescue {}
target 'appName' do
use_expo_modules!
config = use_native_modules!
use_react_native!(
:path => config[:reactNativePath],
:hermes_enabled => podfile_properties['expo.jsEngine'] == 'hermes'
)
# Uncomment to opt-in to using Flipper
#
# if !ENV['CI']
# use_flipper!('Flipper' => '0.75.1', 'Flipper-Folly' => '2.5.3', 'Flipper-RSocket'
=> '1.3.1')
# end
post_install do |installer|
react_native_post_install(installer)
# Workaround `Cycle inside FBReactNativeSpec` error for react-native 0.64
# Reference: https://github.com/software-mansion/react-native-screens/issues/842#issuecomment-812543933
installer.pods_project.targets.each do |target|
if (target.name&.eql?('FBReactNativeSpec'))
target.build_phases.each do |build_phase|
if (build_phase.respond_to?(:name) && build_phase.name.eql?('[CP-User] Generate Specs'))
target.build_phases.move(build_phase, 0)
end
end
end
end
end
end
How can i solve this problem in may case?
To solve the problem, follow these steps:
In terminal:
cd ios
pod deintegrate
After changing the code from Podfile with this one.
require File.join(File.dirname(`node --print "require.resolve('expo/package.json')"`), "scripts/autolinking")
require File.join(File.dirname(`node --print "require.resolve('react- native/package.json')"`), "scripts/react_native_pods")
require File.join(File.dirname(`node --print "require.resolve('#react-native-community/cli-platform-ios/package.json')"`), "native_modules")
platform :ios, '12.0'
require 'json'
podfile_properties = JSON.parse(File.read('./Podfile.properties.json'))
rescue {}
target 'YOUR APP NAME' do
use_expo_modules!
config = use_native_modules!
use_react_native!(
:path => config[:reactNativePath],
:hermes_enabled => podfile_properties['expo.jsEngine'] == 'hermes'
)
# Uncomment to opt-in to using Flipper
#
# if !ENV['CI']
# use_flipper!('Flipper' => '0.75.1', 'Flipper-Folly' => '2.5.3',
'Flipper-RSocket' => '1.3.1')
# end
post_install do |installer|
react_native_post_install(installer)
# __apply_Xcode_12_5_M1_post_install_workaround(installer)
# This is necessary for Xcode 14, because it signs resource bundles by default
# when building for devices.
installer.target_installation_results.pod_target_installation_results
.each do |pod_name, target_installation_result|
target_installation_result.resource_bundle_targets.each do |resource_bundle_target|
resource_bundle_target.build_configurations.each do |config|
config.build_settings['CODE_SIGNING_ALLOWED'] = 'NO'
end
end
end
end
end
After run on terminal:
pod install
pod update
cd ..
These changes worked for me and allowed me to adapt the application
This is just a workaround, not a fix. In fact, you may have multiple targets with different team IDs.
This post_install script in podfile fixed it. As it seems, setting the own developer team is necessary. Replace Your Team ID with the TeamID of your project.
Adding to what #Pruteanu Alexandru answer
cd ios
pod deintegrate
After changing/adding the code from/to Podfile with this one.
post_install do |installer|
installer.generated_projects.each do |project|
project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings["DEVELOPMENT_TEAM"] = "Your Team ID"
end
end
end
end
Your PodFile might look like this:
post_install do |installer|
react_native_post_install(installer)
# Workaround `Cycle inside FBReactNativeSpec` error for react-native 0.64
# Reference: https://github.com/software-mansion/react-native-screens/issues/842#issuecomment-812543933
installer.pods_project.targets.each do |target|
if (target.name&.eql?('FBReactNativeSpec'))
target.build_phases.each do |build_phase|
if (build_phase.respond_to?(:name) && build_phase.name.eql?('[CP-User] Generate Specs'))
target.build_phases.move(build_phase, 0)
end
end
end
end
installer.generated_projects.each do |project|
project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings["DEVELOPMENT_TEAM"] = "YOUR TEAM ID"
end
end
end
end
After run this on terminal:
pod install
pod update
cd ..

How can I define preprocessor flags in iOS project and use it in Swift code?

My Problem: I defined the preprocessor flag for my Xcode project, but it doesn't work as I used it in my Swift code.
What I've Done: I defined multiple Targets for my Xcode project (and multiple schemes as well) and for each target created a custom build configuration file (.xcconfig file). For each Target, I override SWIFT_ACTIVE_COMPILATION_CONDITIONS flag in my .xcconfig file.
My ca805.debug.xcconfig file is like this:
#include "Pods/Target Support Files/Pods-Common-ca805/Pods-Common-ca805.debug.xcconfig"
#include "base.debug.xcconfig"
// other configs
SWIFT_ACTIVE_COMPILATION_CONDITIONS = CA805
And I assigned each build configuration to each Target in .xcodeproj file.
In Swift code I used this flag like below:
#if CA805
print("I'm using CA805 target specific code")
#else
print("I'm using common code")
#endif
And it printed I'm using common code.
Also, I've tried to add SWIFT_ACTIVE_COMPILATION_CONDITIONS flag using Podfile but it didn't work:
platform :ios, '12.0'
use_frameworks!
abstract_target 'Common' do
project 'Main', 'base.debug' => :debug, 'base.release' => :release
use_frameworks!
# Pods for all targets
# ...
# Define Targets
target 'Main'
target 'ca805'
end
post_install do |installer|
installer.pods_project.targets.each do |target|
if target.name.start_with?('Pods-Common')
target.build_configurations.each do |config|
if !config.name.start_with?('base')
target_name = target.name.split('-')[2].upcase
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', "#{target_name}=1"]
config.build_settings['OTHER_SWIFT_FLAGS'] ||= ['$(inherited)', "-D#{target_name}"]
config.build_settings['SWIFT_ACTIVE_COMPILATION_CONDITIONS'] ||= ['$(inherited)', "#{target_name}"]
end
end
end
end
end
I checked Active Compilation Conditions section in my Target's Build Settings tab and something overrides my flag and I don't know the reason.
Active Compilation Conditions section in my project
Finally, I found a workaround for this issue. I modified .xcconfig files:
post_install do |installer|
installer.pods_project.targets.each do |target|
if target.name.start_with?('Pods-Common')
target.build_configurations.each do |config|
if !config.name.start_with?('base')
target_name = target.name.split('-')[2]
flag = target_name.upcase
puts "[Injecting flag:'#{flag}' to '#{target_name}.#{config.name.downcase}.xcconfig'"
xcconfig_path = config.base_configuration_reference.real_path
# read from xcconfig to build_settings dictionary
build_settings = Hash[*File.read(xcconfig_path).lines.map{|x| x.split(/\s*=\s*/, 2)}.flatten]
# modify FLAGS
build_settings['SWIFT_ACTIVE_COMPILATION_CONDITIONS'] = "$(inherited) #{flag}"
# write build_settings dictionary to xcconfig
File.open(xcconfig_path, 'w') {|file| file.puts ''}
build_settings.each do |key, value|
File.open(xcconfig_path, 'a') {|file| file.puts "#{key} = #{value}"}
end
end
end
end
end
end

RxSwift - Playground execution failed: Couldn't lookup symbols

I created a new project and used pods to install RxSwift. Then I created a playground and wrote following code :
import UIKit
import RxSwift
import PlaygroundSupport
var name = "hello"
let names = Variable(["Good"])
But Console shows this error :
Playground execution failed:
error: Couldn't lookup symbols: __T07RxSwift8VariableCACyxGxcfC
__T07RxSwift8VariableCMa
Try this for the Podfile instead:
platform :ios, '11.0'
target 'RxSwiftPlayground' do
use_frameworks!
# Pods for RxSwiftPlayground
pod 'RxSwift', '~> 4.0'
end
post_install do |installer|
installer.pods_project.build_configurations.each do |config|
config.build_settings.delete('CODE_SIGNING_ALLOWED')
config.build_settings.delete('CODE_SIGNING_REQUIRED')
end
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['CONFIGURATION_BUILD_DIR'] = '$PODS_CONFIGURATION_BUILD_DIR'
end
end
end
Or
Delete the line ‘import RxSwift’
Clean (Cmd + Shift + K)
Build (with error)
Put back ‘import RxSwift’
From https://github.com/ReactiveX/RxSwift/issues/1660

Post install hook for .podspec file

Hi I have private pod framework, I'm using, and till now it wasn't a problem because in pod file I can edit SWIFT_OPTIMIZATION_LEVEL like:
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
if (target.name == "MyLib")
config.build_settings['SWIFT_OPTIMIZATION_LEVEL'] = '-Onone'
end
end
end
end
but now I want to use this pod as dependency in my other pod so in .podspec file I Have
s.dependency 'MyLib', '~> 1.0'
So I can't do it in post_install Podfile because it doesn't exist. I tried to do this like
prepare = <<-PREPARECOMMAND
ruby SWIFT_OPTIMALIZATION.rb
PREPARECOMMAND
s.prepare_command = prepare
But it run to early and at the end it's not changed. I also tried to run this in
s.script_phase
It works but only after first failure, in first build it changes the optimisation and cancel, on second time it build. Is there any way to add post install hook to podspec?
s.pod_target_xcconfig = { 'SWIFT_OPTIMIZATION_LEVEL' => '-Onone' }

Cocoapods optimization level for schemes

Looking at the project file for Cocoapods Pods.xcodeproj it looks like every single scheme for every library (except for Debug scheme) has an optimization level of Fastest, Smallest.
Is there a quick and easy way to change the Podfile or some other configuration of Cocoapods so that the optimization level is None for specific schemes when I use pod install?
I use post_install hook in Podfile. Like this:
post_install do |installer|
installer.pods_project.build_configurations.each do |config|
if config.name.include?("Debug")
config.build_settings['GCC_OPTIMIZATION_LEVEL'] = '0'
config.build_settings['SWIFT_OPTIMIZATION_LEVEL'] = '-Onone'
end
end
end
EDIT
GCC_OPTIMIZATION_LEVEL is ignored for Swift Pods. If you're using Swift Pods you also need to set SWIFT_OPTIMIZATION_LEVEL.
To anybody seeing this using cocoapods 0.38.0 or later:
Use "pods_project" instead of "project"
The previous answers use the word "project" (installer.project.build_configurations.each)
Project was deprecated and replaced with pods_project.
https://github.com/CocoaPods/CocoaPods/issues/3747
post_install do |installer|
installer.pods_project.build_configurations.each do |config|
if config.name.include?("Debug")
config.build_settings['GCC_OPTIMIZATION_LEVEL'] = '0'
end
end
end
If you also want the DEBUG macro added for debugging with assertions enabled, you can use the following script:
post_install do |installer|
installer.project.build_configurations.each do |config|
if config.name.include?("Debug")
# Set optimization level for project
config.build_settings['GCC_OPTIMIZATION_LEVEL'] = '0'
# Add DEBUG to custom configurations containing 'Debug'
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)']
if !config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'].include? 'DEBUG=1'
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'DEBUG=1'
end
end
end
installer.project.targets.each do |target|
target.build_configurations.each do |config|
if config.name.include?("Debug")
# Set optimization level for target
config.build_settings['GCC_OPTIMIZATION_LEVEL'] = '0'
# Add DEBUG to custom configurations containing 'Debug'
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)']
if !config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'].include? 'DEBUG=1'
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'DEBUG=1'
end
# Enable assertions for target
config.build_settings['ENABLE_NS_ASSERTIONS'] = 'YES'
config.build_settings['OTHER_CFLAGS'] ||= ['$(inherited)']
if config.build_settings['OTHER_CFLAGS'].include? '-DNS_BLOCK_ASSERTIONS=1'
config.build_settings['OTHER_CFLAGS'].delete('-DNS_BLOCK_ASSERTIONS=1')
end
end
end
end
end
Instead of post_install you can add following line to Podfile:
project 'ProjectName', 'DebugConfigurationName' => :debug

Resources