iOS에서 장치 ID 또는 MAC 주소 가져 오기 [중복
-
12-09-2019 - |
문제
이 질문은 이미 여기에 답이 있습니다.
REST를 사용하여 서버에 통신하는 응용 프로그램이 있습니다. 독창성 검증을 위해 Mac 주소 또는 장치 ID를 IPHONE을 얻고 싶습니다. 어떻게이를 수행 할 수 있습니까?
해결책
[[UIDevice currentDevice] uniqueIdentifier]
각 장치마다 고유 한 것으로 보장됩니다.
다른 팁
고유 한 식별기 (iOS 5.0에서는 더 이상 사용되지 않았습니다. 대신 앱에 맞는 고유 식별자를 만듭니다.)
문서는 사용 권장합니다 cfuuidcreate 대신에 [[UIDevice currentDevice] uniqueIdentifier]
앱에서 고유 한 ID를 생성하는 방법은 다음과 같습니다.
CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
NSString *uuidString = (NSString *)CFUUIDCreateString(NULL,uuidRef);
CFRelease(uuidRef);
동일한 UUIDString을 다시 생성 할 수 없으므로 사용자 기본값이나 다른 장소에서 UUIDString을 저장해야합니다.
당신이 사용할 수있는 uipasteboard 생성 된 UUID를 저장합니다. 앱이 삭제되고 다시 설치되면 Uipasteboard에서 이전 UUID를 읽을 수 있습니다. 장치가 지워질 때 페이스트 보드가 삭제됩니다.
iOS 6에서 그들은 그것을 소개했다 nsuuid 클래스 UUIDS 스트링을 만들도록 설계되었습니다
또한 iOS 6에 추가되었습니다 @property(nonatomic, readonly, retain) NSUUID *identifierForVendor
~로 uidevice 수업
이 속성의 값은 동일한 장치에서 실행되는 동일한 공급 업체에서 나온 앱의 경우 동일합니다. 다른 공급 업체에서 나오는 동일한 장치의 앱과 공급 업체에 관계없이 다른 장치의 앱에 대해 다른 값이 반환됩니다.
앱이 백그라운드에서 실행중인 경우이 속성의 값은 장치를 다시 시작한 후 처음으로 장치를 잠금 해제하기 전에 앱이 백그라운드에서 실행될 수 있습니다. 값이 nil이면 기다렸다가 나중에 다시 값을 얻으십시오.
또한 iOS 6에서는 사용할 수 있습니다 Asidentifiermanager adsupport.framework의 클래스. 거기에 있습니다
@property(nonatomic, readonly) NSUUID *advertisingIdentifier
토론 UIDEVICE의 식별자 인력 속성과 달리 동일한 값이 모든 공급 업체에 반환됩니다. 이 식별자는 예를 들어 사용자가 장치를 지우는 경우 변경 될 수 있으므로 캐시하지 않아야합니다.
앱이 백그라운드에서 실행중인 경우이 속성의 값은 장치를 다시 시작한 후 처음으로 장치를 잠금 해제하기 전에 앱이 백그라운드에서 실행될 수 있습니다. 값이 nil이면 기다렸다가 나중에 다시 값을 얻으십시오.
편집하다:
주의를 기울이십시오 advertisingIdentifier
돌아올 수 있습니다
00000000-0000-0000-0000-000000000000
iOS에는 버그가있는 것 같습니다. 관련 질문 : 광고 제정기 및 식별자 인포 벤더 리턴 "00000000-0000-0000-0000-0000000000"
MAC 신분을 위해 사용할 수 있습니다
#import <Foundation/Foundation.h>
@interface MacAddressHelper : NSObject
+ (NSString *)getMacAddress;
@end
이판
#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
.
많은 유용한 정보가 여기에서 찾을 수 있습니다.
여기에서는 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;
}