LoginSignup
1
2

More than 5 years have passed since last update.

DelphiでiOS/AndroidのOSバージョンや機種名、言語設定を取得するために、各プラットフォームのネイティブな機能を使う

Last updated at Posted at 2017-02-22

Delphiで作ったスマホアプリで、iOS/Androidのバージョンやデバイス名を確認する方法を調べてみたら、プラットフォーム依存の箇所があって意外に手間取ったのでメモしておく。

実装例

以下の実装例では FMX のデフォルトのフォーム名 (TForm1) を用いている。

unit uOSVersionAndLocale;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
  FMX.Controls.Presentation, FMX.ScrollBox, FMX.Memo, FMX.Platform
{$IFDEF Android}
  ,Androidapi.JNI.Os
  ,Androidapi.Helpers
{$ENDIF}
{$IFDEF IOS}
  ,iOSapi.UIKit
  ,Posix.SysSysctl
  ,Posix.StdDef
{$ENDIF}
  ;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    ToolBar1: TToolBar;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
{$IFDEF IOS}
    function GetDeviceModelString: String;
{$ENDIF}
    { private 宣言 }
  public
    { public 宣言 }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

{$IFDEF IOS}
function TForm1.GetDeviceModelString: String;
var
  Size: size_t;
  DeviceModelBuffer: array of Byte;
begin
  sysctlbyname('hw.machine', nil, @Size, nil, 0);

  if Size > 0 then
  begin
    SetLength(DeviceModelBuffer, Size);
    sysctlbyname('hw.machine', @DeviceModelBuffer[0], @Size, nil, 0);
    Result := UTF8ToString(MarshaledAString(DeviceModelBuffer));
  end
  else
    Result := EmptyStr;
end;
{$ENDIF}


procedure TForm1.Button1Click(Sender: TObject);
var
  OSVersion: TOSVersion;
  OSLang: String;
  LocaleService: IFMXLocaleService;

  ModelName: String;
begin
  ModelName := 'unknown';
{$IFDEF Android}
  ModelName := JStringToString(TJBuild.JavaClass.MODEL);
{$ENDIF}
{$IFDEF IOS}
  ModelName := GetDeviceModelString;
{$ENDIF}

  Memo1.Lines.Add(Format('ModelName=%s', [ ModelName ] ));
  Memo1.Lines.Add(Format('OSName=%s', [OSVersion.Name]));
  Memo1.Lines.Add(Format('Platform=%d', [Ord(OSVersion.Platform)]));
  Memo1.Lines.Add(Format('Version=%d.%d', [OSVersion.Major,OSVersion.Minor]));

  OSLang := '';
  if TPlatformServices.Current.SupportsPlatformService(IFMXLocaleService, IInterface(LocaleService)) then
  begin
    OSLang := LocaleService.GetCurrentLangID();

    // Android は日本語環境で Locale = jp を返すので、これを ja に付け替える
    if (OSLang = 'jp') then OSLang := 'ja';
  end;
  Memo1.Lines.Add(Format('Lang=%s', [ OSLang ] ));

end;

end.

参考にしたページなど

条件付きコンパイル(Delphi)
http://docwiki.embarcadero.com/RADStudio/Seattle/ja/%E6%9D%A1%E4%BB%B6%E4%BB%98%E3%81%8D%E3%82%B3%E3%83%B3%E3%83%91%E3%82%A4%E3%83%AB%EF%BC%88Delphi%EF%BC%89

Delphi XE7でOSのプラットフォームとバージョンを取得するには
http://www.gesource.jp/weblog/?p=6895

現在のロケール(en、 fr、ja など)を取得するには
http://www.gesource.jp/weblog/?p=7121

Delphi XE5でAndroidのシステム情報を取得する
http://www.gesource.jp/weblog/?p=6314

How to get the iOS device model?
https://plus.google.com/112344597455394727345/posts/ToHHsAh6XoW

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2