'One Xcode project contains multi Targets with Cocoapods
I have an iOS project X which have several targets, these targets have different bundle IDs, APP names, APP icons, launch screens but share one pod MyBusinessLogicPod (which contains most business logic source code and different image resources. I define some macros to judge which target I am building for, such as TARGET_A_MACRO,TARGET_B_MACRO
#if defined(TARGET_A_MACRO)
static NSString *kAPPID = @"123456";
static NSInteger kChannel = 1;
#elif defined(TARGET_B_MACRO)
static NSString *kAPPID = @"789";
static NSInteger kChannel = 2;
#endif
Question 1:
How can I load different image resources in podspec file according to X's target, Different targets do share this pod, but we don't wanna pack Target B's resource into Target A's bundle.
Question 2:
Is it possible that we do pod install once, and all targets' macro are set, so I can build different targets directly. My Podfile didn't work.
Question 3: Any better solutions for my scenario?
My podfile:
platform :ios, '12.0'
def shared_pods
pod 'AFNetworking'
pod 'Masonry'
pod 'MyBusinessLogicPod'
end
target 'TargetA' do
shared_pods
end
target 'TargetB' do
shared_pods
end
post_install do |installer|
project_path = Pathname.pwd.children.select { |pn| pn.extname == '.xcodeproj' }.first
puts "Main Target xcodeproj path: #{project_path}"
xcproj_objc = Xcodeproj::Project.open(project_path)
xcproj_objc.targets.each do |xctarget|
puts "xctarget.name: #{xctarget.name}"
if xctarget.name == 'TargetA'
puts "xctarget.name1: #{xctarget.name}"
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
if config.name == 'Release'
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'TARGET_A_MACRO=1']
elsif config.name == 'Debug'
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'TARGET_A_MACRO=1']
end
end
end
elsif xctarget.name = 'TargetB'
puts "xctarget.name2: #{xctarget.name}"
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
if config.name == 'Release'
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'TARGET_B_MACRO=1']
elsif config.name == 'Debug'
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'TARGET_B_MACRO=1']
end
end
end
end
end
end
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
