Search code examples
macosdelphifiremonkey

Retrieve Delphi runtime application version and application build information for OSX in fire monkey (FMX)


I try to find an example on how to retrieve the application version and build nr so I can display it in the help box of an OSX application.

Trivial on Windows but on Mac its not.

Hope you can help!

Edward


Solution

  • I use this code that I have written. Simply call:

    osxNSBundle.BundleVersion()
    

    Here is the code:

    uses
      Macapi.Foundation,
      Macapi.Helpers;
    
    osxNSBundle = class
    private
      class function MainBundle: NSBundle;
    public
      class function BundlePath: string;
      class function BundleVersionStr: string;
      class procedure BundleVersion(var aMajor,aMinor,aBuild: integer);
    end;
    
    implementation
    
    class function osxNSBundle.MainBundle: NSBundle;
    begin
      result := TNSBundle.Wrap(TNSBundle.OCClass.mainBundle);
    end;
    
    class function osxNSBundle.BundlePath: string;
    begin
      result := NSStrToStr(MainBundle.bundlePath);
    end;
    
    class function osxNSBundle.BundleVersionStr: string;
    begin
      Result := NSStrToStr(TNSString.Wrap(MainBundle.objectForInfoDictionaryKey(StrToNSStr('CFBundleVersion'))));
    end;
    
    class procedure osxNSBundle.BundleVersion(var aMajor,aMinor,aBuild: integer);
    var lStrArray: TArray<string>;
    i: Integer;
    begin
      aMajor := 0; aMinor := 0; aBuild := 0;
      lStrArray := BundleVersionStr.Split(['.']);
      if Length(lStrArray)>=3 then
      begin
        aMajor := lStrArray[0].ToInteger;
        aMinor := lStrArray[1].ToInteger;
        aBuild := lStrArray[2].ToInteger;
      end;
    end;