Reading iOS Provisioning Profile in your Swift App

In this short post, I will describe how to read a provisioning profile from iOS mobile app to discover some apps metadata.

The mobile provisioning profile is a file embedded by XCode when you build and package your application. It contains several pieces of information that can be useful for your app. For example, you can get the Apple push environment to use, either sandbox for development apps or production for store or Testflight production builds.

Principles

Getting and reading the mobile provisioning file

The mobile provision file is embedded in the app. You can read it from the main bundle. You first need to read the path of that file from the main bundle:

And then you can try reading the file into an string:

Extracting the metadata plist

The provisioning profile contains a plist file that exposes some app metadata. However, the plist string is embedded in a larger binary file. The easiest way to extract the plist is to read only the XML part of the file. This can be done as follows:

Parsing the plist string

You can finally parse the plist strong to get the plist content into a MobileProvision struct, using Swift decodable protocol, introduced in Swift 4:

Putting it all together

Here is the full code with the MobileProvision struct implementing the Decodable protocol:

You can use the code from your mobile app as follows:

That’s it. Your application is now able to discover if it should use APNS in development or production mode automatically, without having to rely on defining a DEBUG build environment variable.


Originally published at blog.process-one.net on November 9, 2018.