How to delete a scheme in CocoaPods post_install hook - cocoapods

How does one delete a scheme created by CocoaPods in the post_install hook? It is a little convoluted, but this scheme is breaking Carthage builds for my SwiftMessages library.
According to this thread, deleting schemes is possible. However, I've looked through the CocoaPods reference and don't see a way to do it.
Update
Following Thiago Cruz's suggestion, I added the following post install hook to my project. Keeping it simple, I just blew away all of the user and shared data in the pods project.
post_install do |installer|
# Blow away schemes – the schemes created by CocoaPods break Carthage builds
# because they incluede a SwiftMessages framework that Carthage picks
# over the main SwiftMessages framework. The SwiftMessages framework that gets
# picked is configured for an app extension and doesn't work correctly in an app.
File.directory?(path)
[
"#{installer.sandbox.root}/Pods.xcodeproj/xcuserdata",
"#{installer.sandbox.root}/Pods.xcodeproj/xcshareddata"
].each { |path|
if File.directory?(path)
FileUtils.remove_dir(path)
end
}
end
In addition to this, I had to open the pods project and disable "Autocreate schemes" in the scheme management dialog. It's worth noting that pod install doesn't revert this setting.

Not sure which .xcscheme files you want to delete but in a post_install hook you have access to the root of your project and also the /Pods root. You could glob your way to the file and manually delete it.
post_install do |installer|
puts "#{installer.config.installation_root}"
puts "#{installer.sandbox.root}"
end

Related

Why is Cocoapods 1.8.3 adding APPLICATION_EXTENSION_API_ONLY to the Target Support xcconfig file?

We have used Cocoapods in our project for a long time. Ever since 1.8.3, it is adding APPLICATION_EXTENSION_API_ONLY to one of our private pod's Pods/Target Support Files/*/*.xcconfig files, and this, in turn, is causing the build to fail.
As a workaround, we are removing that line, and things are working again.
I would like to fix this issue for good, and would like to know what precisely makes cocoapods add this flag, and what we need to make the project work again.
The pod in question is supposed to be used in extensions, this is true. It does contain conditional compilation (#ifndef TARGET_IS_EXTENSION) to exclude the code that should not go into extensions.
What are we doing wrong?
Instead of doing it manually, you can do it automatically after a pod install with a post_install hook by adding the following to the bottom of your Podfile.
(Below is a template that might need a few tweaks depending on project)
post_install do |installer|
installer.pods_project.build_configurations.each do |config|
if target.name.include?(“CUSTOM_POD”). // or remove if statement to delete/set value for all pods
config.build_settings.delete('APPLICATION_EXTENSION_API_ONLY')
//OR
config.build_settings['APPLICATION_EXTENSION_API_ONLY'] = 'NO' //or 'YES'
end
end
end

Nativescript Algolia iOS Build Fail

I am trying to build nativescript-algolia but it fails on iOS. This is the error I am getting:
[!] Unable to determine Swift version for the following pods:
- `AlgoliaSearch-Client-Swift` does not specify a Swift version
and none of the targets (`appname`) integrating it have the
`SWIFT_VERSION` attribute set. Please contact the author
or set the `SWIFT_VERSION` attribute in at least one of the
targets that integrate this pod.
I have tried everything I have seen from Googling this problem. I just can't seem to set the Swift Version correctly.
Downgrading to 1.5.3 isn't a proper workaround and will not be sustainable in the future. In fact, this error is due to a new behavior introduced in Cocoapods 1.6.0 version, which force developers to set a Swift version to their projects. The goal is in fact to make library developers specify a Swift version in their podspec, so that library users don't have to do this manually (thanks to post_install scripts for example). You can see my discussion about this with a Cocoapods maintainer here. I do agree that this behavior is a bit misleading, as we try to set the Swift version in post_install script but an error is returned before…
In fact, as I have been told by the maintainer, the correct fix to this issue is to set a Swift version at project level (and therefore not at Pod level). To do this, just add a new User Defined Setting in Build Settings, with key SWIFT_VERSION and value 4.0 (for example, as whatever value should work here if you also set the version at Pod level with post_install script).
Long story short, the fix is to add this key/value :
Note that as you're using NativeScript, you may want to set the Swift version by running command export SWIFT_VERSION=4 in your project folder, before building.
Create a file named Podfile in App_Resources/iOS and add the following lines,
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['SWIFT_VERSION'] = '4.0'
end
end
end
With the help from the plugin creator I was able to fix this by downgrading my Cocoapods version.
sudo gem uninstall cocoapods
sudo gem install cocoapods -v 1.5.3
References:
https://github.com/arpit2438735/nativescript-algolia/issues/19#issuecomment-472847392
Swift Version NativeScript

How to fix "Bus error 10" after update to Xcode 10.2

I updated Xcode to new stable 10.2v. I tried to build my project and it was successful. When I'm trying to archive the project (workspace), I'm getting errors like on screenshot below:
What I've tried so far:
Update cocoa pods to latest version -> COCOAPODS: 1.7.0.beta.3
Clean DeliveredData folder
Reinstall Xcode
Remove repository, clone it again and install pods
Totally remove all pods from project and add them back
Temporary Workaround
For me it was only the Cache framework. Until they have fixed it, you can manually set SWIFT_OPTIMIZATION_LEVEL to -Onone for the configuration you want to use for archiving.
CocoaPods
You could even use your Podfile if you don't want Cococapods to overwrite the settings every time you run pod install
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
if target.name == 'Cache'
config.build_settings['SWIFT_OPTIMIZATION_LEVEL'] = '-Onone'
end
end
end
end
Note that this is specifically checking for the Cache framework. If you have problems with other frameworks you might want to change or extend this condition.
While Lukas' answer of disabling optimization on the Cache pod works, I followed Alex's link to the issue in their GitHub repo and found there is an open pull request with a pretty simple code change that fixes it. I unlocked the file and made the change locally.
Here's the PR: https://github.com/hyperoslo/Cache/pull/236
Apply this diff: https://github.com/hyperoslo/Cache/pull/236/commits/560f00a9a9549db161ca70d96eed74fc580b03e3#diff-9e53dc1370d4f7c9cdaaa103d26ff096
Which, to repeat here, is in the file MD5.swift change the safe_add function to be:
func safe_add(_ x: Int32, _ y: Int32) -> Int32 {
return x &+ y
}
(Disclaimer: I do not claim to know the correctness of the change, but it seems the delay merging the PR is due to figuring out who is currently maintaining the repo.)
The answer is here: https://github.com/hyperoslo/Cache/issues/238
We are waiting for the owners of this repo to make any sign of life...

IBDesignables, no suitable image found, required code signature missing

I recently made some changes to Assets.xcassets and this caused absolute chaos for XCode. I ended up with multiple errors of this kind: Failed to render and update auto layout status for *MyViewController* (6jf-cd-DYU) dlopen (AFramework.framework, 1): no suitable image found. Did find: Aframework.framwork: required code signature missing for 'Aframework.framework' in both .storyboard and .xib files.
Aframework is a placeholder for multiple Pod frameworks I have as dependencies. These worked fine before I modified xcassets.
I tried everything I could find related to this but I've gotten nowhere: Cleaned Derived Data, ran clean/build, removed all XCode cache folders, reinstalled XCode entirely, Refresh all views etc. It compiles and runs fine, everything works in the app (and no images or resources are missing), but all my Storyboards are blank (all white), which makes it pretty hard to work.
I also tried pod deintegrate, removed the xcworkspace-file and reinstalling pods (since the error points to a Pod framework). I also revoked and re-issued all my certificates through XCode since it points to code signing as a problem.
EDIT: I Completely reinstalled OSX and cloned the repo from Git from a working configuration. No change. This must be something other than the xcassets-theory, because even if I check out commits from weeks ago (where I know for sure this wasn't a problem) I still get the error. Maybe something was updated by Apple between now and the last time it worked. I've given up and moved on for now. I'll just click through the views in the explorer on the left side instead of inside the storyboard. Hopefully someone somewhere figures this out at some point.
This issue looks like Cocoapods bug and caused by CODE_SIGNING_ALLOWED and CODE_SIGNING_REQUIRED keys in settings of Pods.
Adding this code in the end of pod file will fix an issue (don't forget do then pod install):
# Workaround for Cocoapods v.1.5 issue #7606
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
end
It turns out this was related to cocoapods. 1.5 has issues with XCode 9.3 it seems. I downgraded to cocoapods 1.4 and the issue went away. Please see this thread on GitHub.
Edit: This issue has been fixed. The latest version of CocoaPods no longer suffers from this problem, so all you have to do is update.
Just adding a complete version of Igor's answer
Please don't downgrade cocoapods. Instead modify pod files as bellow.
#Replace 9.0 with your project ios version
platform :ios, '9.0'
target 'YourProject' do
frameworks
use_frameworks!
pod 'Alamofire', '~> 4.7'
pod 'AlamofireObjectMapper', '~> 5.0'
pod 'SDWebImage', '~> 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
end

Hook in Podfile to edit my project file

I want to fix Cocoapods bug, when it adds Embed Pods Frameworks build phase for Extension targets. These phases are not needed there.
https://github.com/CocoaPods/CocoaPods/issues/4203
I wrote script to remove it
puts "Deleting not needed phases…"
project_path = "Keyboard.xcodeproj"
project = Xcodeproj::Project.open(project_path)
project.targets.each do |target|
if target.name.include?("Extension")
phase = target.shell_script_build_phases.find { |bp| bp.name == 'Embed Pods Frameworks' }
if !phase.nil?
puts "Deleting Embed Pods Frameworks phase…"
target.build_phases.delete(phase)
end
end
end
project.save
I can run this script after pod install manually, but I want to add it to Podfile somehow. It doesn't work in post_install hook
post_install do |installer|
...
end
because UserProjectIntegrator.integrate! is called after post_install and it overrides my changes.
Is there any way to integrate this script in Podfile?
The short answer is No.
The long answer:
From the output of pod install --verbose, the build phase Embed Pods Frameworks is added in the Integrating client project which runs after the post_install step in the Podfile.
This is why you should execute this script separately after the pod install finished running.

Resources