Every developer new to Cocoa and Swift development needs to become familiar with Apple's lingo and processes. In this episode, I'd like to talk about App IDs and bundle identifiers. What is an App ID? What is a bundle ID? What is the link between App IDs and bundle IDs?
Bundle Identifier
A bundle ID or bundle identifier uniquely identifies an application in Apple's ecosystem. This means that no two applications can have the same bundle identifier. To avoid conflicts, Apple encourages developers to use reverse domain name notation for choosing an application's bundle identifier.
A bundle ID or bundle identifier uniquely identifies an application in Apple's ecosystem. This means that no two applications can have the same bundle identifier.
Let's take Cocoacasts as an example. Every application I build for Cocoacasts has a bundle identifier that starts with com.cocoacasts
, that is, the reverse of the Cocoacasts website. For my company, Code Foundry, I use bundle identifiers that start with be.codefoundry
.
You don't need to own the domain you use for your application's bundle identifier. The domain is only used to ensure the bundle identifier is unique. For example, I could also use blog.cocoacasts
for the applications I build for Cocoacasts even though I don't own cocoacasts.blog
.
To create a unique bundle identifier, you append the name of the application to the reversed domain, for example, com.cocoacasts.scribbles
. Remember that you choose the bundle identifier. Reverse domain name notation is what Apple recommends and it's a pattern most developers use.
The only requirement is that the bundle identifier is unique. Xcode warns you if you choose a bundle identifier that's already in use by another application. I strongly recommended that you use a bundle identifier that makes sense and that's why most developers stick to reverse domain name notation.
I strongly recommended that you use a bundle identifier that makes sense and that's why most developers stick to reverse domain name notation.
If you're building a suite of applications, then you can use the same approach. Take a look at the following example. The first bundle ID is used for the iOS client of Scribbles whereas the second bundle ID is used for the macOS client of Scribbles.
com.cocoacasts.scribbles.ios
com.cocoacasts.scribbles.macos
App ID
Developers often confuse App IDs with bundle IDs or, even worse, they think they're the same thing. An App ID represents one or more applications in Apple's system. It consists of two components, a Team ID and a bundle ID search string.
In this example, ABCDE12345
is the Team ID and com.cocoacasts.scribbles
is the bundle ID search string. The Team ID and the bundle ID search string are separated by a period.
ABCDE12345.com.cocoacasts.scribbles
The Team ID is the component you don't need to worry about is. It's generated by Apple and prepended to the bundle ID search string.
What is a bundle ID search string? This question takes us back to bundle identifiers. It's possible to use one App ID for several applications. That's where explicit and wildcard App IDs come into play.
The bundle ID search string of an App ID can match a single application or, by including a wildcard, it can match multiple applications of the same development team. Let me show you an example of each App ID type.
What Is an Explicit App ID?
As the name implies, an explicit App ID doesn't contain a wildcard. The bundle ID search string matches the bundle identifier of one application.
ABCDE12345.com.cocoacasts.scribbles
The Team ID, ABCDE12345
, is generated by Apple for your development team. This isn't something you can or should change. The bundle ID search string, com.cocoacasts.scribbles
, doesn't contain an asterisk. In other words, the bundle ID search string matches the bundle identifier of one application, the application with a bundle identifier of com.cocoacasts.scribbles
. An explicit App ID can only be used for one application.
What Is a Wildcard App ID?
A wildcard App ID is a bit different in that it can be used for one or more applications. The App ID can be used for every application with a bundle identifier that matches the App ID's bundle ID search string.
ABCDE12345.com.cocoacasts.*
The above App ID can be used for several bundle identifiers. The asterisk or wildcard indicates that anything following com.cocoacasts.
can be variable.
com.cocoacasts.scribbles
com.cocoacasts.scribbles.ios
com.cocoacasts.scribbles.macos
What Are App IDs Used For?
You already know that a bundle identifier uniquely identifies an application in Apple's ecosystem. What is the purpose of an App ID? An App ID links one or more applications from a development team in Apple's system. Why is this important?
Whenever you want to enable a capability or application service for your application, you enable that capability for the App ID your application is linked to. This used to be tedious, requiring a visit to Apple's developer website. Xcode has evolved quite a bit over the years and it takes care of the details most of the time. Let me show you how this works.
App IDs and Capabilities
Setting Up the Project
Fire up Xcode and create a new project by choosing the App template from the iOS > Application section.
Name the project and set the Organization Identifier using reverse domain name notation as I explained earlier. The organization identifier is used to create the first component of the application's bundle identifier. Project's name is appended to the organization identifier to create the bundle identifier. Don't worry. You can change this after setting up the project. For example, I prefer to keep the bundle identifier lowercased.
With the project created, open the Project Navigator on the left, select the target from the list of targets, and open the General tab. The Identity section displays your application's bundle ID or bundle identifier.
Keep in mind that you can't modify the bundle ID whenever you feel like it. Once your application is pushed to Apple's App Store, it's no longer possible to change your application's bundle ID. Choose your bundle ID carefully.
Enabling Capabilities
Let's say we want to enable iCloud for our application. For that to work, we need to inform Apple that our application needs the iCloud capability.
Years ago, this meant a visit to Apple's developer website, creating an App ID, and flipping a switch. This is no longer necessary. Open the Signing & Capabilities tab in Xcode, click the + Capability button in the top left, and choose iCloud from the list of options.
Xcode jumps into action, talks to Apple's developer portal on your behalf, and takes care of the necessary steps.
Visiting Apple's Developer Website
Let's make sure everything is set up correctly on Apple's servers. Open a browser and visit Apple's developer website. Click Account and sign in with the Apple ID that's linked to your developer account.
Select Identifiers from the Certificates, Identifiers & Profiles section.
From the menu on the left, choose App IDs and look for the App ID Xcode created for us. App IDs generated by Xcode start with the letters XC to indicate that Xcode created those on your behalf.
Click the App ID and inspect its configuration. It gives you an overview of the capabilities and applications services of the App ID in the development and production environments.
Notice that iCloud is enabled because we enabled this capability from within Xcode. Some capabilities and application services are enabled by default, such as Game Center and In-App Purchase.
Manual Configuration
You can manually configure an App ID through Apple's developer website, and that may be necessary from time to time. For example, you can only link SSL certificates to an App ID for push notifications through the developer website. This isn't possible from within Xcode at the time of writing.
Choose Wisely
It's important that you take a few moments when you choose the bundle ID of your application. Once your application is in the hands of users, you're unable to make changes to the bundle ID. While this isn't something users pay attention to, you don't want to have a bundle ID that makes no sense. Stick with Apple's guidelines and you should be fine.
Most companies include the company's name in the bundle identifier and that makes sense. But remember that the bundle identifier cannot be modified even if the application changes hands, for example, in the event of an acquisition.
If you're a freelance developer or you run an agency, make sure you don't include your name or that of your company in the bundle identifier. Discuss this with the client and make sure you choose a bundle identifier that makes sense for the project and the owner.