How to use OTHER_SWIFT_FLAGS in a Pod - ios

Has anybody tried to use build configuration flags in a Swift based Pod? I am trying to use a post_install hook to add "OTHER_SWIFT_FLAGS" to a private Swift pod that I'm working on. Basically, I want to be able to do something like #if DEV #elseif QA #else #endif to change endpoints based on the target that I'm using. The project I'm integrating the pod with also uses DEV & QA flags which work fine with corresponding DEV & QA targets.
Here is a post_install hook that I came up using help from other similar posts:
post_install do |installer|
installer.pods_project.targets.each do |target|
if target.name == 'Pods-Test-QA'
puts "Found #{target.name}. Adding Swift Flag..."
target.build_configurations.each do |config|
puts "Configuration: #{config}"
config.build_settings['OTHER_SWIFT_FLAGS'] ||= ['$(inherited)']
config.build_settings['OTHER_SWIFT_FLAGS'] << '-DQA'
puts "#{config.build_settings['OTHER_SWIFT_FLAGS'].inspect}"
end
end
if target.name == 'Pods-Test-DEV'
puts "Found #{target.name}. Adding Swift Flag..."
target.build_configurations.each do |config|
puts "Configuration: #{config}"
config.build_settings['OTHER_SWIFT_FLAGS'] ||= ['$(inherited)']
config.build_settings['OTHER_SWIFT_FLAGS'] << '-DDEV'
puts "#{config.build_settings['OTHER_SWIFT_FLAGS'].inspect}"
end
end
end
end
After running pod install the post_install successfully adds the flags as shown in the image below for Pods-Test-QA & Pods-Test-DEV.
However, when I run the project using QA or DEV targets and use the following flags it always hits PROD:
#if DEV
print("DEV ENVIRONMENT")
#elseif QA
print("QA ENVIRONMENT")
#else
print("PROD ENVIRONMENT")
#endif
Is there something I'm missing?
Also, for some reason Pods-Test-QA.debug.xcconfig & DEV don't seem to be updated with the flags I've added. Does anyone have any idea why it's not updating in the .xcconfig files?
OTHER_SWIFT_FLAGS = $(inherited) "-D" "COCOAPODS"
Any help would be appreciated. Thanks in advance.
Using:
cocoapods (1.0.1)
Xcode 7.3.1

If you develop your own pod and would like to add some flags depending on the configuration you can do so by mentioning it in your podspec file.
My goal was to add 'BETA' flag to my pod if parent's project active configuration contains 'BETA' in its name without post-install hooks.
s.pod_target_xcconfig = {
'OTHER_SWIFT_FLAGS[config=*Beta*]' => '-DBETA',
}
Podspec not only allows to do add custom flags but gives an opportunity to use wildcards.
The result: if my parent project uses 'Extra Early Beta' configuration then the following fork in pod's sources goes to 'BETA'
#if BETA
// Beta related code
#else
// Code I don't need in beta.
#endif

Related

Flutter IOS build error - "select a development team in the Signing & Capabilities editor."

The following error is displayed when attempting to build on an IOS device:
As shown in the picture below, the Signing setting in Xcode is well done.
Can you tell me the cause and solution of this?
Could not build the precompiled application for the device.
Error (Xcode): Signing for "GoogleSignIn-GoogleSignIn" requires a development team. Select a development team in the Signing & Capabilities editor.
/Users/home_1/StudioProjects/Example%20Project/app/ios/Pods/Pods.xcodeproj
Error (Xcode): Signing for "DKPhotoGallery-DKPhotoGallery" requires a development team. Select a development team in the Signing & Capabilities editor.
/Users/home_1/StudioProjects/Example%20Project/app/ios/Pods/Pods.xcodeproj
Error (Xcode): Signing for "DKImagePickerController-DKImagePickerController" requires a development team. Select a development team in the Signing & Capabilities editor.
/Users/home_1/StudioProjects/Example%20Project/app/ios/Pods/Pods.xcodeproj
Error (Xcode): Signing for "gRPC-C++-gRPCCertificates-Cpp" requires a development team. Select a development team in the Signing & Capabilities editor.
/Users/home_1/StudioProjects/Example%20Project/app/ios/Pods/Pods.xcodeproj
This happened to me today after switching to Xcode 14. Try adding this to your podfile
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
if config.build_settings['WRAPPER_EXTENSION'] == 'bundle'
config.build_settings['DEVELOPMENT_TEAM'] = 'YOUR_DEVELOPMENT_TEAM_ID'
end
end
end
end
Don't forget to replace the YOUR_DEVELOPMENT_TEAM_ID with your actual development Team ID which you can find in developer.apple.com
This will permanently fix the issue.
If you don't want to use the Team ID you can do this instead:
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
if target.respond_to?(:product_type) and target.product_type == "com.apple.product-type.bundle"
target.build_configurations.each do |config|
config.build_settings['CODE_SIGNING_ALLOWED'] = 'NO'
end
end
end
end
end
If you have other post_install steps, for example flutter_additional_ios_build_settings(target), make sure to keep them
I'm wondering when Apple is going to launch a new xCode version without breaking anything from previous ones!
Rants aside, this is how I fixed my Flutter project based on previous answers:
Open your awesome_flutter_project/ios/Podfile:
and replace these lines:
post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
end
end
for:
post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
target.build_configurations.each do |config|
if target.respond_to?(:product_type) and target.product_type == "com.apple.product-type.bundle"
target.build_configurations.each do |config|
config.build_settings['CODE_SIGNING_ALLOWED'] = 'NO'
end
end
end
end
end
Just for everyone, who thinks the accepted solution is a hack, you are right. This is a known bug in the flutter framework which has been fixed in flutter 3.3.3 .
So I recommand everyone to upgrade to this version, if you are able/allowed to do so.
In case you can't upgrade to 3.3.3 then the accepted solution is your best bet ;)
It's a Flutter framework issue
Update Flutter version into 3.3.3
I also got this error after updating XCode to version 14.
Add the following to ios > podfile
post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
target.build_configurations.each do |config|
if config.build_settings['WRAPPER_EXTENSION'] == 'bundle'
config.build_settings['DEVELOPMENT_TEAM'] = 'YOUR_DEVELOPMENT_TEAM_ID'
end
end
end
end
To get YOUR_DEVELOPMENT_TEAM_ID: https://developer.apple.com/account/#!/membership/
This will permanently fix the issue.
If you don't want to use the Team ID you can do this instead:
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
if target.respond_to?(:product_type) and target.product_type == "com.apple.product-type.bundle"
target.build_configurations.each do |config|
config.build_settings['CODE_SIGNING_ALLOWED'] = 'NO'
end
end
end
end
end
I was able to solve the problem by doing the following:
Open project on xCode
Open Pods and find out "GoogleSignIn-GoogleSignIn" and other three on Pods > Target
Select Team on Signing & Capabilities
Open your flutter project in Xcode
Pods -> Targets -> Signing & Capabilities -> Select Team
Select Team for every Targets
I am using CodeMagic.io (CI/CD) and had this same problem, I found the issue to be the Xcode version: 14
I solved this at CodeMagic.io by doing this :
Go to the Workflow Editor
Go to 'Build'
Change the Xcode version: 13.4.1
If you are using another CI/CD try using Xcode version: 13.4.1
It was mentioned to solve this you can also update your Flutter version to 3.3.3 however if like me this is not an option for you try using Xcode version 13.4.1
adding following code solved issue in Xcode 14 and flutter version 2.8
config.build_settings['CODE_SIGNING_ALLOWED'] = 'NO'
When I put the the suggested changes in the pod file and run the flutter build ipa command, I am getting a different error.
Error:
In file included from /Volumes/Data/FlutterSDK/.pub-cache/hosted/pub.dartlang.org/webview_flutter_wkwebview-2.9.3/ios/Classes/FWFUIViewHostApi.m:5:
/Volumes/Data/FlutterSDK/.pub-cache/hosted/pub.dartlang.org/webview_flutter_wkwebview-2.9.3/ios/Classes/FWFUIViewHostApi.h:5:9: fatal error: 'Flutter/Flutter.h' file not found
#import <Flutter/Flutter.h>
For CI/CD, this is the only solution that worked for me.
Update podfile.
target_is_resource_bundle = target.respond_to?(:product_type) && target.product_type == 'com.apple.product-type.bundle'
target.build_configurations.each do |build_configuration|
if target_is_resource_bundle
build_configuration.build_settings['CODE_SIGNING_ALLOWED'] = 'NO'
build_configuration.build_settings['CODE_SIGNING_REQUIRED'] = 'NO'
build_configuration.build_settings['CODE_SIGNING_IDENTITY'] = '-'
build_configuration.build_settings['EXPANDED_CODE_SIGN_IDENTITY'] = '-'
end
end
then go to the team drop list and select your team

How to define custom preprocessor macros in xconfig file when using CocoaPods

I has created two configurations in Project > Project name > Info (tab) > Configurations Target.
Let's call em CustomDebug and CustomRelease. For each configuration, i has created xconfig files – CustomDebug.xconfig and CustomRelease.xconfig and set em as 'Base on Configuration File' field to corresponding configurations. After that i did run pod install. Pods has been installed with warnings – I should use config files generated by pods directly in my configs or by including em in my config files. So i choose 2nd option and add corresponding includes to my config files.
All works fine. But...
I tried to define custom macro in my config file:
GCC_PREPROCESSOR_DEFINITIONS='$(value) MACRO=1'
And it overrides same var from config file generated by pods.
GCC_PREPROCESSOR_DEFINITIONS = $(inherited) COCOAPODS=1 $(inherited) GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS=1 $(inherited) PB_FIELD_32BIT=1 PB_NO_PACKED_STRUCTS=1 PB_ENABLE_MALLOC=1
When i swap the import and custom macro lines, obviously, value defined in pods config for GCC_PREPROCESSOR_DEFINITIONS overrides my one.
Does any body know, how to "extend" value of GCC_PREPROCESSOR_DEFINITIONS defined in config file generated by CocoaPods?
You can't "extend" value of GCC_PREPROCESSOR_DEFINITIONS defined in config file generated by CocoaPods.
To fix nanopb framework in Pods, add to Podfile post-install script:
post_install do |installer|
installer.pods_project.build_configuration_list.build_configurations.each do |configuration|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
if target.name == "nanopb"
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)']
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'PB_ENABLE_MALLOC=1'
end
end
end
end
Add $(inherited):
GCC_PREPROCESSOR_DEFINITIONS='$(inherited) $(value) MACRO=1'

TesseractOCRiOS | Warning: Multiple build commands for output file

versions:
TesseractOCRiOS 4.0.0
CocoaPods 1.2.1
When i'm building on simulator - everything is fine, but when i'm trying to build on my iPhone xCode gives me next warning:
Warning: Multiple build commands for output file /Users/Username/Library/Developer/Xcode/DerivedData/ProjectName-hjheurpncvhpfbabezufoumrybad/Build/Products/Debug-iphoneos/TesseractOCRiOS/TesseractOCR.framework/PrivateHeaders/config_auto.h
Faced with the same problem recently, definitely not the best, but acceptable workaround - just delete one of the duplicate lines "config_auto.h" with path "./Pods/TesseractOCRiOS/TesseractOCR/include/leptonica/" in "Build Phases"-"Headers"-"Private" settings of TesseractOCRiOS target.
OCR recognition functionality does not affected by this change.
See screenshot for details.
If you're using cocoapods, you can add following script at the end of your pod file:
post_install do |installer|
installer.pods_project.targets.each do |target|
if target.name == 'TesseractOCRiOS'
target.build_configurations.each do |config|
config.build_settings['ENABLE_BITCODE'] = 'NO'
end
header_phase = target.build_phases().select do |phase|
phase.is_a? Xcodeproj::Project::PBXHeadersBuildPhase
end.first
duplicated_header_files = header_phase.files.select do |file|
file.display_name == 'config_auto.h'
end
duplicated_header_files.each do |file|
header_phase.remove_build_file file
end
end
end
end
Just noticed that it's also disabling bitcode. Remove it if that's not what you need. The script removes the duplicate header files since they are under private section only.
I think you have a problem in your project directory. You have a duplicate file of config_auto.h
Go to your Target , Delete config_auto.h under the build phases.
Hope!! This helps you

How to exclude Pods from Code Coverage in Xcode

Is there a way to exclude Pods from Code Coverage?
I would like to see Code Coverage only for the code I've written.
Not that it should matter, but I'm using Xcode 8.
These steps will help:
1. add these lines to Podfile
# Disable Code Coverage for Pods projects
post_install do |installer_representation|
installer_representation.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['CLANG_ENABLE_CODE_COVERAGE'] = 'NO'
end
end
end
2. run pod install
Now you won't see pods in test coverage.
Note: It only excludes Objective-c pods but not Swift
XCode 10 Update
In Xcode 10 you can set which targets you want to enable code coverage for in
Edit Schemes > Test > Options
Just select 'Gather coverage for some targets' and add your main project.
To disable coverage for swift code you can use a wrapper for SWIFT_EXEC (I verified this so far with Xcode 9.3). Hence the complete solution (incl. Swift) would be the following:
Append to your Podfile (and invoke pod install after that):
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |configuration|
configuration.build_settings['CLANG_ENABLE_CODE_COVERAGE'] = 'NO'
configuration.build_settings['SWIFT_EXEC'] = '$(SRCROOT)/SWIFT_EXEC-no-coverage'
end
end
end
Place the following script (name it SWIFT_EXEC-no-coverage) at the root of your source tree (chmod +x as necessary):
#! /usr/bin/perl -w
use strict;
use Getopt::Long qw(:config pass_through);
my $profile_coverage_mapping;
GetOptions("profile-coverage-mapping" => \$profile_coverage_mapping);
exec(
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc",
#ARGV);
Here's a link to the corresponding gist: https://gist.github.com/grigorye/f6dfaa9f7bd9dbb192fe25a6cdb419d4
Click on your Pods project in the Project Navigator on the left
On the right hand side, open project and target list if it's not already open; then click on the Pods project name (NOT the targets).
Click Build Settings.
In the search bar, search for "CLANG_ENABLE_CODE_COVERAGE".
Change "Enable Code Coverage Support" to NO.
Re-run test.
If you are developing a pod and want to have code coverage just for yours:
# Disable Code Coverage for Pods projects except MyPod
post_install do |installer_representation|
installer_representation.pods_project.targets.each do |target|
if target.name == 'MyPod'
target.build_configurations.each do |config|
config.build_settings['CLANG_ENABLE_CODE_COVERAGE'] = 'YES'
end
else
target.build_configurations.each do |config|
config.build_settings['CLANG_ENABLE_CODE_COVERAGE'] = 'NO'
end
end
end
end
Based on answer of #Tung Fam adding exclusion list for some pods
# Disable Code Coverage for projects listed in excluded_pods
excluded_pods = ['Pod1', 'Pod2', 'Pod3']
post_install do |installer_representation|
installer_representation.pods_project.targets.each do |target|
next if !excluded_pods.include?(target.name)
target.build_configurations.each do |config|
config.build_settings['CLANG_ENABLE_CODE_COVERAGE'] = 'NO'
end
end
end

Cocoapods: turning MagicalRecord logging off

Turning MagicalRecord logging off requires a #define to be made before it is first included in the project, but in the case of a project managed by Cocoapods I have no access to add a #define in the Pods project. How do I turn logging off completely in this scenario?
Took me a few hours to figure out a way to do it, posting here in the hope it will help others.
EDIT: this is not a duplicate as it discusses turning logging off under Cocoapods
You can use a post_install hook to modify pretty much any build setting. Just add this code to your Podfile:
post_install do |installer|
target = installer.project.targets.find{|t| t.to_s == "Pods-MagicalRecord"}
target.build_configurations.each do |config|
s = config.build_settings['GCC_PREPROCESSOR_DEFINITIONS']
s = [ '$(inherited)' ] if s == nil;
s.push('MR_ENABLE_ACTIVE_RECORD_LOGGING=0') if config.to_s == "Debug";
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] = s
end
end
Note that this will only disable logging in the debug configuration - logging is disabled by default in the release configuration.
In my case, I was building a library that depended on MagicalRecord. I didn't want my users to have to add a post_install in their Podfile to silence the noisy logging, so I added it to my podspec instead.
s.prefix_header_contents = '#define MR_ENABLE_ACTIVE_RECORD_LOGGING 0'
This automatically adds this #define statement to Pods-prefix.pch, which silences MagicalRecord logging in projects that use my pod.
I updated ank's answer for those using the new cocoapods version alongside MagicalRecord 2.3.0:
post_install do |installer|
target = installer.pods_project.targets.find{|t| t.to_s == "MagicalRecord"}
target.build_configurations.each do |config|
s = config.build_settings['GCC_PREPROCESSOR_DEFINITIONS']
s = [ '$(inherited)' ] if s == nil;
s.push('MR_LOGGING_DISABLED=1') if config.to_s == "Debug";
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] = s
end
end
Changes:
project renamed to pods_project
Target Pods-MagicalRecord renamed to MagicalRecord
Macro MR_ENABLE_ACTIVE_RECORD_LOGGING renamed to MR_LOGGING_DISABLED and value changed from 0 to 1
You can switch off logging in Pod project!
Simply add Preprocessor Macros:
Just go into "Pods" (!!!) project.
Then find out Pods-MagicalRecord target.
Choose "Build Settings" tab
Find "Apple LLVM 6.1 Preprocessing" -> "Processor Macros"
Rollout "Processor Macros" and add to "Debug" schema: "MR_ENABLE_ACTIVE_RECORD_LOGGING=0"
It`s all!
For the development branch (version 2.3.0 and higher) of Magical Record logging seems to still not work correctly. When imported like this:
pod 'MagicalRecord', :git => 'https://github.com/magicalpanda/MagicalRecord', :branch => 'develop'
I have no logging output on my Xcode console. But I altered the post_install script of the Cocoapod. The following should enable logging:
https://gist.github.com/Blackjacx/e5f3d62d611ce435775e
With that buildsetting included in GCC_PREPROCESSOR_DEFINITIONS logging of Magical Record can be controlled in 2.3.0++ by using [MagicalRecord setLoggingLevel:]

Resources