Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Getting bundle identifier

Discussion in 'iOS and tvOS' started by bpritchard, Apr 6, 2011.

  1. bpritchard

    bpritchard

    Joined:
    Jan 29, 2009
    Posts:
    444
    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
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Is it possible to use the iPhoneSettings class to get the information you need?
     
  3. bpritchard

    bpritchard

    Joined:
    Jan 29, 2009
    Posts:
    444
    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.
     
  4. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    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.

    Code (csharp):
    1.  
    2.     #if UNITY_IPHONE
    3.     [DllImport("__Internal")]
    4.     private static extern string _GetCFBundleVersion();
    5.     #endif
    6.  
    7.     #if UNITY_IPHONE
    8.     public static string BundleVersion {
    9.         get {
    10.             if (m_bundleVersion == null) {
    11.                 GetVersionInfo();
    12.             }
    13.             return m_bundleVersion;
    14.         }
    15.     }
    16.     protected static string m_bundleVersion;
    17.     #endif
    18.    
    19.     protected static void GetVersionInfo() {
    20.  
    21.         #if UNITY_IPHONE
    22.             #if UNITY_EDITOR
    23.                 m_bundleVersion = "";
    24.             #else
    25.                 m_bundleVersion = _GetCFBundleVersion();
    26.             #endif
    27.         #endif
    28.        
    29.     }
    30.  
    And the C for the native callback:

    Code (csharp):
    1.  
    2. extern "C" {
    3.     const char * _GetCFBundleVersion() {
    4.         NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
    5.         return strdup([version UTF8String]);
    6.     }
    7. }
     
  5. akasurreal

    akasurreal

    Joined:
    Jul 17, 2009
    Posts:
    442
    Is there still no way with Unity 3.5.6, at run-time, to access BundleID and Version without resorting to native calls?
     
  6. kenlem

    kenlem

    Joined:
    Oct 16, 2008
    Posts:
    1,630
  7. akasurreal

    akasurreal

    Joined:
    Jul 17, 2009
    Posts:
    442
    That's why I specified, at run-time, I believe all PlayerSettings are only accessible in the Editor?

     
  8. kenlem

    kenlem

    Joined:
    Oct 16, 2008
    Posts:
    1,630
    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.
     
  9. zackivano

    zackivano

    Joined:
    Mar 20, 2012
    Posts:
    11
    I made a small easy to use package for everyone with the Daniel Bauer's code above. All the credits goes to him! :)
     

    Attached Files:

  10. jpkader@wgen.net

    jpkader@wgen.net

    Joined:
    Nov 28, 2012
    Posts:
    2
    You can get the bundle identifier from Application. persistentDataPath at least on Android. My colleague suggested this idea.
     
  11. Clint.Carpenter

    Clint.Carpenter

    Joined:
    Apr 28, 2015
    Posts:
    4
    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.
     
  12. akasurreal

    akasurreal

    Joined:
    Jul 17, 2009
    Posts:
    442
    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:

    Code (csharp):
    1.  
    2. [MenuItem("FizzPow/Build Game", priority = 96)]
    3. private static void BuildGame()
    4. {
    5.     //find my app setting object & set some stuff to be accessible at run-time
    6.     AppSettings[] appSettings = Resources.FindObjectsOfTypeAll<AppSettings>();
    7.     appSettings[0]._bundleID = PlayerSettings.bundleIdentifier;
    8.                    
    9.     ...
    10.                    
    11.     BuildPipeline.BuildPlayer(...);
    12. }
    13.  
     
  13. nahoy

    nahoy

    Joined:
    Dec 10, 2012
    Posts:
    21
    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: https://www.assetstore.unity3d.com/#!/content/13624
     
  14. sumitkr99

    sumitkr99

    Joined:
    Aug 18, 2014
    Posts:
    1
  15. unitymir

    unitymir

    Joined:
    Nov 12, 2014
    Posts:
    1
    Application.bundleIdentifier was not supported in old versions. (T.T) , Is there any other way ?
     
  16. tarahugger

    tarahugger

    Joined:
    Jul 18, 2014
    Posts:
    129
    For google time travelers, as of 2017.3 :

    Code (CSharp):
    1. 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."