Get all available Emoji using the Objective-C runtime

  • 5 Min. Read.

I recently found myself in a situation where I had to display a list of all available emoji on the screen, but Apple forgot to provide a documented way of doing this. After inspecting some of the iOS 9 runtime headers I created a small helper class which would list all Emoji (variations included), ordered by category. TLDR; You can find the sample code on github.

Update for iOS 10

iOS 10 includes emojis (Take a look at the changelog for all the details.) Updates include a new flat style for some emoji, more females and lots & lots of redesigned emoji. The helper provided is also compatible with iOS 10!

Update for iOS 9.1

You probably heard iOS 9.1 includes new emojis (Take a look at the changelog for all the details.) Updates include sports — volleyball and hockey; gestures — such as the middle finger and sign of the horns (“rock on”); as well as food items — such as a hot dog, taco, and more. The helper provided is also compatible with iOS 9.1!

Let’s approach the problem

A quick inspection on the iOS 9 runtime showed there was a class named UIKeyboardEmojiCategory which is the object responsible for managing Emoji in the current category.

First we get a reference to every category available. We do this by creating a Class type using NSClassFromString. Using NSClassFromString we’re able to access classes which weren’t meant to be public in the first place. Next, we check if our reference to UIKeyboardEmojiCategory responds tonumberOfCategories and if it does, we loop over the number of categories and add the category to our categories array.

Building this code results into some compiler errors:

To fix this, we should make the compiler ‘know’ about these methods. We do this by creating a category on NSObject and copy over the method signatures from the UIKeyboardEmojiCategory header.

Next, we should loop over every category & get out the Emoji. But first let’s create some containing objects to store our information.

Next up, we’re going to loop over every category we got and get out thename and the displayName.

We’re going to ignore the ‘Recent’ category because we only want unique Emoji.

The Flags category is a bit special, more on that later.

Don’t forget to add the following method signature to your UIKeyboardEmojiCategory category.

Now, within this loop, we’ll add another loop for all the emoji.

A bit about diversity

In the code above you’ll notice we’re checking the variantMask of the Emoji, and we’ll be populating the emojiVariations array with 5 combinations. Where do these codes come from? If you take a look at the Emoji technical report at the diversity section, you’ll see that there are 5 modifiers available for skin tone. A colored emoji is basically simply a combination of 2 emoji:

Diversity modifiers
Diversity modifiers

So what we’re literally doing in the code above, is combine the base emoji with a colored swatch, which then results in a colored emoji.

Diverse baby emoji
Diverse baby emoji

What about the flags?

The flags are a special category of Emoji, which aren’t populated by default. We get all flag Emoji by calling computeEmojiFlagsSortedByLanguage on UIKeyboardEmojiCategory, which returns an array of Emoji.

Problem with this is that the Emoji array on every other category is an object with keys & values, so we create a dict, wrap the Emoji in it and set the Emoji property on the category.

Don’t forget to add the method signatures:

Creating the demo

For the demo, I created a UITableViewController to display every Emoji (variations included) in its own section and with every variation.

You can find the helper with the demo project on Github.

A concept by Wonderlus