Codementor Events

Developing on an iOS Beta - CocoaPods & You

Published Jul 20, 2018
Developing on an iOS Beta - CocoaPods & You

It's Beta Time

It's about that time again, when Apple goes through its developer betas and public previews. From June to September, Apple Developers get to test out the latest iOS beta and begin updating support for their applications.

What about all those libraries you use? If you're like me, you don't like re-inventing the wheel. If a library exists for it, why do it again? Unless, of course, you're super amazing at coding and just want to do it yourself, you're going to use a dependency, and that means other people's code. Code you don't have the time or energy or even the will to go through and understand.

And of course, with a beta, and a new version of Apple's programming language, Swift, that means there's going to be changes. Changes that the developers of your favorite libraries haven't implemented. And that means more work for you, right?

Wrong.

Good News, Everyone!

There's a neat little trick you can use any time you want to support a library through CocoaPods, whether you're developing on a beta, or just want to use an old library that hasn't been updated since Swift 2! It takes the frustration out of using the code, and the irritation out of updating it to make it work! I mean, seriously, who wants to update their code every year?

It's Alive!

I first ran into this problem when working on a personal project. Of course, my development phone is running iOS 12 beta (and so is my personal phone, don't judge). I wanted to mess with some new ideas, so I went out to matteocrippa's awesome-swift list to find a library that fit my needs. Turns out, the one that I wanted most hadn't been updated since Swift 3 (apparently, that developer has a life!) so I had to find a workaround. I didn't want to bother updating the code, since this was just some app I was building to toy around with. I wanted a quicker solution.

The Real Deal

After hours and hours of going through Stack Overflow posts, I finally found a real gem for CocoaPods. I knew that CocoaPods allows you to set some build configuration options when you're installing Pods, but had forgotten that Xcode allows you to set a version of Swift for each target. CocoaPods allows you to set that version any time you run pod install or pod update!

Brass Tacks

The solution I found was simple. Set the build configuration up so that when a Pod installs or updates, it gets its Swift version set. Let's see what that looks like

platform :ios, '12.0'

target 'MyAwesomeApp' do
  use_frameworks!
    
    pod 'Parse'
    pod 'SCLAlertView'
    pod 'GCDKit'
    
    post_install do |installer|
    	installer.pods_project.targets.each do |t|
        	if ['GCDKit'].include? t.name
            	t.build_configurations.each do |config|
                	config.build_settings['SWIFT_VERSION'] = '3.0'
                end
            end
        end
    end
end

Awesome! GCDKit doesn't support Swift 4.2 (latest as of this writing) just yet, so let's set the Swift version to 3.0 instead. Now, Xcode builds and runs the project just fine!

A Little Help Here?

What did we just do? Well, post_install is a hook provided by CocoaPods to allow us some actions after Pods have been installed. Most notably, it allows us to make changes to the Build Configuration, which can also be manually changed in Xcode. Be warned, though. Any changes made to the Pods target in Xcode's Build Configuration panel may be erased after pod update is run again. So we change it here. Next, we use a little Ruby to find only the Pods that we are interested in, for this example that would be GCDKit. Then, we change the build settings to set Swift up as version 3.0

Ok, But Two Different Pods Need Two Different Swift Versions...

That's cool too! Let's see what that looks like.

# After defining our Pods...
post_install do |installer|
  installer.pods_projects.targets.each do |t|
    	if ['MyPod'].include? t.name
        	t.build_configurations.each do |config|
            	config.build_settings['SWIFT_VERSION'] = '3.0'
             end
        end
        
        if ['MyOtherPod'].include? t.name
        	t.build_configurations.each do |config|
            	config.build_settings['SWIFT_VERSION'] = '3.2'
            end
        end
  end
end

And that's it! Just add a new if statement for each set of Pods you want to change.

So, What Did We Learn?

CocoaPods has a post-install hook that allows us to make some changes to Pods that are installed. By using that hook to change the Swift version number, we can get our Pods to run until the developer has chosen to update their code (or, until we feel like doing it for them).

Interestingly enough, though, if you set a low enough version, Xcode will give you a warning that the code may be unsupported, so use this carefully.

Discover and read more posts from Mason Phillips
get started
post commentsBe the first to share your opinion
Show more replies