Delphi iOS Tips (6) : identifierForVendorの取得
PCのMacアドレスみたいに、iOSデバイスを識別する方法をさがしていたが、
下記の「identifierForVendor」を取得するのがよさそう。
■UIDevice.identifierForVendor
デバイス固有であるが、ベンダごと(Team IDごと)で異なるUUIDを返す。
(転載元)http://nolili.wordpress.com/2012/09/19/uuid/
下記のサイトにidentifierForVendorを取得するソースがあったので、試してみた。
Apple.UtilsはXE4のデモフォルダにあったので、そちらを使った。
■iOS: Identifying Your Users Devices — A Recipe How to Import iOS Classes
http://www.monien.net/ios-identifying-your-users-devices-a-recipe-how-to-import-ios-classes/
■identifierForVendorの取得ソース
unit Unit1; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, DX.Apple.Utils, Apple.Utils, FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); private { private 宣言 } public { public 宣言 } end; var Form1: TForm1; implementation {$R *.fmx} procedure TForm1.FormCreate(Sender: TObject); begin ShowMessage(VendorIdentifier); end; end.
unit DX.Apple.Utils; interface /// <summary> /// DX.Apple.Utils is a helper library to get easier acces to certain Apple Mac/iOS functions. /// </summary> /// <description> /// This library also imports certain classes or parts of them which have been "forgotten" by EMBT /// The code is intended to be used with Delphi XE4 only. Other versions may or may not work. /// There is a dependency to AppleUtils.pas, which ships with XE4 and can usually be founc here: /// C:\Users\Public\Documents\RAD Studio\11.0\Samples\Delphi\RTL\CrossPlatform Utils /// Make sure use the most recent version - that samples folder above /// is connected to an SVN repository of Embarcadero. /// </description> /// <remarks> /// <para> /// DX.Apple.Utils is Copyright 2013 Olaf Monien / Developer Experts, LLC. /// </para> /// <para> /// All code comes "as is", without any guaranties granted. This code may contain bugs and my not work as advertised. /// </para> /// <para> /// RESTClient is licensed under Mozilla Public License 2.0 /// In other words you are free to use this library in your projects, just leave this header intact. /// </para> /// </remarks> // Logs to the console // Named NSLog2 to avoid name clash // iOS device: Xcode - Organizer -> Device - Console // Mac / iOS Simulator: Mac - Console procedure NSLog2(const AMessage: string); // Retrieves the vendor specific device ID - DO NOT USE UIDevice.uniqueIdentifier - this would lead to AppStore rejection since May 1st, 2013! function VendorIdentifier: string; implementation uses System.SysUtils, Apple.Utils, {$IFDEF IOS} Macapi.ObjectiveC, iOSApi.Foundation, iOSApi.UIKit, iOSApi.QuartzCore, iOSApi.CocoaTypes {$ELSE} {$IFDEF MACOS} Macapi.ObjectiveC, Macapi.Foundation {$ENDIF MACOS} {$ENDIF IOS} ; {$IFDEF IOS} // Hack to import forgotten classes/functions and properties // Be careful - classes with same name may already exist in iOSApi!! type // **** NSUUID NSUUIDClass = interface(NSObjectClass) ['{D9518F5E-DDBC-4702-A555-411D32B85340}'] end; // We just need the UUIDString here NSUUID = interface(NSObject) ['{4C137FF5-E854-461F-B77E-8CD357FD4E9C}'] function UUIDString: NSString; cdecl; end; TNSUUIDDX = class(TOCGenericImport<NSUUIDClass, NSUUID>) end; // **** UIDevice UIDeviceClass = interface(NSObjectClass) ['{D5105207-FBA7-4F55-BC7B-1ADACE347ECA}'] { class } function currentDevice: Pointer; cdecl; end; UIDevice = interface(NSObject) ['{481E431F-2C02-4F2D-86C5-7728480ECF48}'] function identifierForVendor: NSUUID; cdecl; end; TUIDeviceDX = class(TOCGenericImport<UIDeviceClass, UIDevice>) end; {$ENDIF} procedure NSLog2(const AMessage: string); var LMessage: NSString; begin LMessage := NSSTR(FormatDateTime('hh:nn:ss,zzz', now) + ' - ' + AMessage); iOSApi.Foundation.NSLog(PtrForObject(LMessage)); end; {$IFDEF IOS} function currentDevice: DX.Apple.Utils.UIDevice; begin result := TUIDeviceDX.Wrap(TUIDeviceDX.OCClass.currentDevice); end; function VendorIdentifier: string; var LDevice: DX.Apple.Utils.UIDevice; begin LDevice := currentDevice; result := NSStringToString(LDevice.identifierForVendor.UUIDString); end; {$ENDIF} end.