How to be more effective when working with certificates and assets during iOS developmentAugust 5, 2020
In the last few years our team of iOS developers tried countless tools and libraries. Some of them weren't useful at all and we never used them again. Some were useful only when solving specific problems. Only a few of them were so essential that we started to use them on all our projects from their very beginning.
In this category of essential tools definitely belongs Fastlane Match and R.swift. The first one helps you with the always unending battle with certificates. The second one radically improves your effectiveness when working with assets like images, files, localizations etc.
I think every small team knows this. App is developed, everything is tested and the app goes on the App Store. People start to use it and they find a critical bug. Unfortunately, your colleague, who developed the app, went on a holiday and that's why you're tasked with fixing this bug.
You open up the project but you find out you're missing signing certificates. Your colleague is the one who created them and he's the only one who has them but he didn't bring his notebook with him.
That's why you have to erase the certificates and create new ones and after your colleague returns from his vacation you need to remember to give them to him so he's not forced to do the same as you a week later.
Fastlane Match is built exactly for these kinds of situations. It's a tool for easy management and sharing of certificates and provisioning profiles. It's able to connect to your Apple Developer account, create signing certificates and provisioning profiles for a given app and then install them onto your local machine. It also uploads them to a private repository where they are accessible to the rest of your team.
Starting with Fastlane Match is pretty simple. First you need to install it using this command:
sudo gem install fastlane
Then go to the root directory of your project and write this command to initialize Fastlane Match:
fastlane match init
You'll be prompted to enter a URL of your private repository where the created certificates will be saved. After that a new file called Matchfile will be created inside your project. The newly created file doesn't have much inside it but after a simple configuration it can look something like this:
git_url("https://gitlab.com/dactylgroup/ios-certificates.git") git_branch("my-project") team_id("XYZ") app_identifier(["com.dactylgroup.my-project"]) username("APPLE_ID")
- URL of the repository where everything gets uploaded.
- Name of the branch where the generated certificates are saved. Fastlane Match will then create a new branch in your repository with the given name.
- Team ID which can be found here: https://developer.apple.com/account/#/membership
- Bundle identifier of your application.
- Apple ID of your team.
After simple configuration all that's left to do is write this command:
fastlane match development
Fastlane Match will generate new certificates and provisioning profiles. If the certificates already exist it will download them from your private repository and install them onto your local machine. Besides development you can also use appstore, enterprise or adhoc.
R.swift is a script which runs every time you build your application. It searches the whole project for resources like images, fonts, localizations or storyboards and then creates references to these resources in a single file named R.generated.swift.
As a result, instead of loading an image from asset catalogue using its name it can look something like this:
And if you decide to change the name in the asset catalogue, you'll get an error from the compiler:
The benefits when using R.swift are:
- If you decide to delete an image from your project or you just want to rename it, you won't be able to build your project and the compiler will tell you what went wrong.
- Fully supported autocomplete.
- No more casting! When dequeuing UITableViewCell it already has the correct type.
Another situation in which this library is useful are localizations. They can easily grow into hundreds or thousands of keys and it gets harder and harder not to lose your mind when working with them. Luckily, using R.swift it can go from this:
label.text = NSLocalizedString("registration.button.privacy-policy", comment: "")
To something like this:
label.text = R.string.localizable.registrationButtonPrivacyPolicy()
And if you need extra parameters it can be accomplished like this:
/* “Email '%@' už existuje. */ label.text = R.string.localizable.errorEmailExists(email)
For more examples on how R.swift can make your code more readable and safe go here https://github.com/mac-cain13/R.swift where you will also find instructions on how to implement it into your project.