Grow your own Cocoapod
Why create a Cocoapod?
Over the years we’ve gathered a tonne of useful code snippets/functions/classes to make coding easier. Whether it’s wanting to detect if your app is running on an iPad or checking if an NSDate is between two others, there are a tonne of code snippets out there (mostly on Stack Overflow). Others you may have written yourself and re-used between projects
After a few years copying over the same ‘helper’ classes and categories to every new project, I decided it’s time for be a better way to manage this ‘base’ code.
Cocoapods is already widely used as an iOS and OSX library dependency manager so why not package everything into a nice, clean cocoapod, then it’s only one line in a podfile to include it in future projects!
Creating a cocoapod is quite straightforward. Cocoapods.org have a great guide to accomplish this so I won’t go into too much detail here. However there were a few things to note during setup.
It’s one line to create a cocoapod environment. Navigate to a new project folder and run:
pod lib create <project name>
replacing ‘JDUtils’ with your preferred project name. You’ll then be presented with a few config options. I said ‘No’ to including testing frameworks and view based testing but these are up to you.
Likewise you have the option to give a class prefix but can leave blank if desired (My classes all start with “JD” so I chose that).
Eventually you’ll end up with a folder containing your podspec file, classes folder and an Xcode project shortcut. I’ve modified the default podspec file so it looks like this:
Really all that’s changed is the summary, description and source/homepage git URLs. Notice Cocoapods automatically chooses MIT as the licence type. For most open source projects MIT is fine, however I recommend checking out http://choosealicense.com/ if you’re looking for options.
This last line is where I set the default values for my fonts and colours categories. More on that in a bit.
Once you’re happy with your podspec, run the following in terminal replacing ‘<project name>’ with your own to validate it. Then push your project to a git repository of your choosing and tag with the same version number in your podspec. You can commit, tag and push in Sourcetree if easier.
pod lib lint <project name>.podspec
git add -A
git commit -m “Release 0.1.0”
git tag ‘0.1.0’
git push – – tags
To submit a podspec to the cocoapods repository (where it’ll be accessible to the public) you need to register your machine with
pod trunk register <email address> –description='<machine name>’
Then check your inbox for a verification email. After verified it’s just one more command to push to cocoapods specs repo:
pod trunk push <project name>.podspec
That’s it! Your cocoapod will be fetched and included in any project with a podfile that references it.
So what is JDUtils?
JDUtils is a collection of Objective-C Categories that I created for many native objects used in iOS development. The main two are UIFont+Additions and UIColor+Additions. JDAdditions.h also includes a bunch of useful methods such as NSDate comparison, Notification permission check, email regex, UIImage orientation fixing and more.
The JD Utils UIFont category provides shortcut methods for getting the most common fonts you might use in a project. Just add fonts to your project as you would normally, include them in your info.plist’s “Fonts provided by application” then use the following method to find their usable names:
This will log a list of all fonts installed for your application at runtime. For example:
Next set a preprocessor macro for your desired font name in the list. Preprocessor macros are like #defines but are accessible globally throughout your project. Go to your target’s Build Settings and find the preprocessor macro rows. Then enter the following to set a macro with the font name
Now you can use the following method in UIFont+JDAdditions to get this font:
label.font = [UIFont fontWithMacro:JD_APP_FONT size:16.0f];
JDUtils comes with a few pre-defined macros for a Logo font and bold/regular/light versions of an app font of your choosing. Remember that xcconfig line in my podspec earlier? Just override the following macros:
Then use the shortcut methods in UIFont+JDAdditions for each:
label.font = [UIFont logoFontWithSize:16.0f];
Like fonts, colours are defined as hex strings in your target preprocessor macros and set using the following method:
label.color = [UIColor colorWithMacro:MY_MACRO_COLOR];
JDUtils comes with a few shortcut methods for colours too. Just override the macros for:
Then use a pre-defined colour method like:
label.color = [UIColor appBlack];
UIColor+JDAdditions includes methods for getting a UIColor from hex directly, darker and lighter colours from an existing UIColor and quick social channel colour methods.
Feel free to check out JDUtils on Github and give it a go. Eventually we’ll be upgrading to Swift so stay tuned for updates soon!
Frankie & Benny's restaurant app
The new loyalty app for Frankie & Benny's features mobile vouchers and full integration with existing CRM platforms.
Onto the High Street: Event Wrap Up
Omnifi were proud to sponsor the latest TLA Retail Tech event looking at eCommerce brands who are investing in physical retail.
Award winners with Be At One
We've been crowned award winners with Be At One by the Restaurant Marketer & Innovator awards.