Getting bundle identifier

I am trying to figure out if this is even possible but i’d like to, at runtime (not in the editor) query the bundle identifier of the app. Basically the game i’m working on has 4 versions that are all essentially the same save a few different variables within the app itself (aka some locked stuff, etc)… 2 for iphone and 2 for ipad. What i was thinkin of doing is to check the current bundle identifier and turn things on/off based on that.

Is that even possible? Is there an easier way to setup the build process so i can check something ahead of time and then flag it appropriately prior to going into xcode? Like an if def or something? These builds are ALL separate builds. So i do have do it x4… little annoying right now.

Cheers
Bryan

Is it possible to use the iPhoneSettings class to get the information you need?

I don’t think so, at least not that i’m aware of. I can definitely find the device “type” which i’m doing for other stuff… but since i have 2 versions on the same device i need to be able to discern those easily enough at runtime… i’m just trying to eliminate the need to make 4 completely unique sets of functions for each platform and instead just check against the app name or bundle id.

You’ll have to use a plugin. Here’s the C# code we use for accessing the bundle version at runtime. It can easily be modified to get whatever bundle value you need.

	#if UNITY_IPHONE
	[DllImport("__Internal")]
	private static extern string _GetCFBundleVersion();
	#endif

	#if UNITY_IPHONE
	public static string BundleVersion {
		get {
			if (m_bundleVersion == null) {
				GetVersionInfo();
			}
			return m_bundleVersion;
		}
	}
	protected static string m_bundleVersion;
	#endif
	
	protected static void GetVersionInfo() {

		#if UNITY_IPHONE
			#if UNITY_EDITOR
				m_bundleVersion = "";
			#else
				m_bundleVersion = _GetCFBundleVersion();
			#endif
		#endif
		
	}

And the C for the native callback:

extern "C" {
	const char * _GetCFBundleVersion() {
		NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
		return strdup([version UTF8String]);
	}
}

Is there still no way with Unity 3.5.6, at run-time, to access BundleID and Version without resorting to native calls?

See
http://docs.unity3d.com/Documentation/ScriptReference/PlayerSettings-bundleVersion.html

PlayerSettings.bundleVersion maybe? Haven’t tried it but sure looks like it’s what you’re looking for.

That’s why I specified, at run-time, I believe all PlayerSettings are only accessible in the Editor?

Rats! Didn’t notice that it was an editor class. Sorry. Darn… I’d like to get the bundle version as well to display in my help dialog.

I made a small easy to use package for everyone with the Daniel Bauer’s code above. All the credits goes to him! :slight_smile:

1223436–50788–$BundleVersion_iOS.unitypackage (5.23 KB)

You can get the bundle identifier from Application. persistentDataPath at least on Android. My colleague suggested this idea.

Thank you for the plugin. I’ll try that out and I guess try to make one for the Android side as well. Still seems very ridiculous to need to create a plugin to access a key component of an application run-time.

FWIW, my workaround for this was to just create a build script that sets a few game properties including bundleID so I could access them at run-time easily, and is platform independent this way.

Example:

[MenuItem("FizzPow/Build Game", priority = 96)]
private static void BuildGame()
{
    //find my app setting object & set some stuff to be accessible at run-time
    AppSettings[] appSettings = Resources.FindObjectsOfTypeAll<AppSettings>();
    appSettings[0]._bundleID = PlayerSettings.bundleIdentifier;
                   
    ...
                   
    BuildPipeline.BuildPlayer(...);
}
1 Like

If you want to have access to the bundleIdentifier, productName etc… at runtime, and much more features, like handling multiple version of your game to different builds, I’ve created Advanced Builder, available right here: Unity Asset Store - The Best Assets for Game Making

Use Application.bundleIdentifier instead of PlayerSettings.bundleIdentifier …It 'll work perfectly…

Application.bundleIdentifier was not supported in old versions. (T.T) , Is there any other way ?

For google time travelers, as of 2017.3 :

Application.identifier

“Returns application identifier at runtime. On Apple platforms this is the ‘bundleIdentifier’ saved in the info.plist file, on Android it’s the ‘package’ from the AndroidManifest.xml.”

2 Likes