iOS でのデバイス ID または Mac アドレスの取得 [重複]
-
12-09-2019 - |
質問
この質問にはすでに答えがあります:
REST を使用してサーバーと通信するアプリケーションがあります。一意性を検証するために iPhone の MAC アドレスまたはデバイス ID を取得したいのですが、どうすればよいですか?
解決
[[UIDevice currentDevice] uniqueIdentifier]
は、各デバイスに一意であることが保証されている。
他のヒント
uniqueIdentifier (iOS 5.0 では非推奨。代わりに、アプリに固有の一意の識別子を作成してください。)
ドキュメントでは、次の使用を推奨しています CFUUID作成 の代わりに [[UIDevice currentDevice] uniqueIdentifier]
アプリ内で一意の ID を生成する方法は次のとおりです
CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
NSString *uuidString = (NSString *)CFUUIDCreateString(NULL,uuidRef);
CFRelease(uuidRef);
同じ uuidString を再度生成することはできないため、uuidString をユーザーのデフォルトまたは他の場所に保存する必要があることに注意してください。
使用できます UIPペーストボード 生成されたuuidを保存します。アプリを削除して再インストールする場合は、UIPasteboard から古い uuid を読み取ることができます。ペーストボードはデバイスが消去されると消去されます。
iOS 6 では、 NSUUIDクラス UUID文字列を作成するように設計されています
iOS 6でも追加されました @property(nonatomic, readonly, retain) NSUUID *identifierForVendor
に UIDデバイス クラス
このプロパティの値は、同じデバイスで実行されている同じベンダーから来るアプリで同じです。異なるベンダーから来る同じデバイスのアプリと、ベンダーに関係なく異なるデバイス上のアプリに対しては、異なる値が返されます。
アプリがバックグラウンドで実行されている場合、ユーザーがデバイスが再起動された後に初めてデバイスのロックを解除する前に、このプロパティの値はゼロになる場合があります。値がゼロの場合は、待って後でもう一度値を取得します。
iOS 6 でも使用できます ASIidentifierManager AdSupport.framework のクラス。そこにあります
@property(nonatomic, readonly) NSUUID *advertisingIdentifier
議論uideviceの識別子vendorプロパティとは異なり、同じ値がすべてのベンダーに返されます。この識別子は、たとえば、ユーザーがデバイスを消去した場合、キャッシュしないでください。
アプリがバックグラウンドで実行されている場合、ユーザーがデバイスが再起動された後に初めてデバイスのロックを解除する前に、このプロパティの値はゼロになる場合があります。値がゼロの場合は、待って後でもう一度値を取得します。
編集:
に注意してください。 advertisingIdentifier
戻るかもしれない
00000000-0000-0000-0000-000000000000
iOSにバグがあるようなので。関連する質問: AdvertisingIdentifier と identifierForVendor は「00000000-0000-0000-0000-000000000000」を返します。
あなたが使用することができますMacの住所については、
#import <Foundation/Foundation.h>
@interface MacAddressHelper : NSObject
+ (NSString *)getMacAddress;
@end
implentation
#import "MacAddressHelper.h"
#import <sys/socket.h>
#import <sys/sysctl.h>
#import <net/if.h>
#import <net/if_dl.h>
@implementation MacAddressHelper
+ (NSString *)getMacAddress
{
int mgmtInfoBase[6];
char *msgBuffer = NULL;
size_t length;
unsigned char macAddress[6];
struct if_msghdr *interfaceMsgStruct;
struct sockaddr_dl *socketStruct;
NSString *errorFlag = NULL;
// Setup the management Information Base (mib)
mgmtInfoBase[0] = CTL_NET; // Request network subsystem
mgmtInfoBase[1] = AF_ROUTE; // Routing table info
mgmtInfoBase[2] = 0;
mgmtInfoBase[3] = AF_LINK; // Request link layer information
mgmtInfoBase[4] = NET_RT_IFLIST; // Request all configured interfaces
// With all configured interfaces requested, get handle index
if ((mgmtInfoBase[5] = if_nametoindex("en0")) == 0)
errorFlag = @"if_nametoindex failure";
else
{
// Get the size of the data available (store in len)
if (sysctl(mgmtInfoBase, 6, NULL, &length, NULL, 0) < 0)
errorFlag = @"sysctl mgmtInfoBase failure";
else
{
// Alloc memory based on above call
if ((msgBuffer = malloc(length)) == NULL)
errorFlag = @"buffer allocation failure";
else
{
// Get system information, store in buffer
if (sysctl(mgmtInfoBase, 6, msgBuffer, &length, NULL, 0) < 0)
errorFlag = @"sysctl msgBuffer failure";
}
}
}
// Befor going any further...
if (errorFlag != NULL)
{
NSLog(@"Error: %@", errorFlag);
return errorFlag;
}
// Map msgbuffer to interface message structure
interfaceMsgStruct = (struct if_msghdr *) msgBuffer;
// Map to link-level socket structure
socketStruct = (struct sockaddr_dl *) (interfaceMsgStruct + 1);
// Copy link layer address data in socket structure to an array
memcpy(&macAddress, socketStruct->sdl_data + socketStruct->sdl_nlen, 6);
// Read from char array into a string object, into traditional Mac address format
NSString *macAddressString = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X",
macAddress[0], macAddress[1], macAddress[2],
macAddress[3], macAddress[4], macAddress[5]];
//NSLog(@"Mac Address: %@", macAddressString);
// Release the buffer memory
free(msgBuffer);
return macAddressString;
}
@end
使用します。
NSLog(@"MAC address: %@",[MacAddressHelper getMacAddress]);
これを使用します:
NSUUID *id = [[UIDevice currentDevice] identifierForVendor];
NSLog(@"ID: %@", id);
IOSで5 [[UIDevice currentDevice] uniqueIdentifier]
が廃止されます。
これは-identifierForVendor
または-identifierForAdvertising
を使用することをお勧めします。
多くの有用な情報はここで見つけることができます:
iOS6 UDID - どのような利点identifierForVendorはidentifierForAdvertisingの上に持っているのでしょうか? の
ここで、私たちは... Asp.net C#のコードを使用してIOSデバイスのMACアドレスを見つけることができます。
.aspx.cs
-
var UserDeviceInfo = HttpContext.Current.Request.UserAgent.ToLower(); // User's Iphone/Ipad Info.
var UserMacAdd = HttpContext.Current.Request.UserHostAddress; // User's Iphone/Ipad Mac Address
GetMacAddressfromIP macadd = new GetMacAddressfromIP();
if (UserDeviceInfo.Contains("iphone;"))
{
// iPhone
Label1.Text = UserDeviceInfo;
Label2.Text = UserMacAdd;
string Getmac = macadd.GetMacAddress(UserMacAdd);
Label3.Text = Getmac;
}
else if (UserDeviceInfo.Contains("ipad;"))
{
// iPad
Label1.Text = UserDeviceInfo;
Label2.Text = UserMacAdd;
string Getmac = macadd.GetMacAddress(UserMacAdd);
Label3.Text = Getmac;
}
else
{
Label1.Text = UserDeviceInfo;
Label2.Text = UserMacAdd;
string Getmac = macadd.GetMacAddress(UserMacAdd);
Label3.Text = Getmac;
}
ファイルの.class
public string GetMacAddress(string ipAddress)
{
string macAddress = string.Empty;
if (!IsHostAccessible(ipAddress)) return null;
try
{
ProcessStartInfo processStartInfo = new ProcessStartInfo();
Process process = new Process();
processStartInfo.FileName = "arp";
processStartInfo.RedirectStandardInput = false;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.Arguments = "-a " + ipAddress;
processStartInfo.UseShellExecute = false;
process = Process.Start(processStartInfo);
int Counter = -1;
while (Counter <= -1)
{
Counter = macAddress.Trim().ToLower().IndexOf("mac address", 0);
if (Counter > -1)
{
break;
}
macAddress = process.StandardOutput.ReadLine();
if (macAddress != "")
{
string[] mac = macAddress.Split(' ');
if (Array.IndexOf(mac, ipAddress) > -1)
{
if (mac[11] != "")
{
macAddress = mac[11].ToString();
break;
}
}
}
}
process.WaitForExit();
macAddress = macAddress.Trim();
}
catch (Exception e)
{
Console.WriteLine("Failed because:" + e.ToString());
}
return macAddress;
}