Pobieranie identyfikatora urządzenia lub adresu Mac w systemie iOS [duplikat]

To pytanie ma już odpowiedź tutaj:

Mam aplikację, która używa rest do komunikacji z serwerem, chciałbym uzyskać adres mac lub identyfikator urządzenia do weryfikacji unikalności, jak to zrobić?

Author: Vadim Kotov, 2009-07-10

6 answers

[[UIDevice currentDevice] uniqueIdentifier] gwarantuje unikalność każdego urządzenia.

 41
Author: Steven Canfield,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-29 08:38:45

UniqueIdentifier (przestarzały w iOS 5.0. Zamiast tego utwórz unikalny identyfikator specyficzny dla Twojej aplikacji.)

Dokumenty zalecają użycie CFUUIDCreate zamiast [[UIDevice currentDevice] uniqueIdentifier]

Oto jak wygenerować unikalny identyfikator w aplikacji

CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
NSString *uuidString = (NSString *)CFUUIDCreateString(NULL,uuidRef);

CFRelease(uuidRef);

Zauważ, że musisz zapisać uuidString w domyślnych ustawieniach użytkownika lub w innym miejscu, ponieważ nie możesz ponownie wygenerować tego samego uuidString.

Możesz użyć UIPasteboard do przechowywania wygenerowanego identyfikatora uuid. Oraz jeśli aplikacja zostanie usunięta i ponownie zainstalowana, możesz odczytać z UIPasteboard Stary uuid. Płyta pasty zostanie wymazana, gdy urządzenie zostanie usunięte.

W iOS 6 wprowadzono klasę Nsuuid , która została zaprojektowana do tworzenia łańcuchów UUIDs

Ponadto dodali w iOS 6 {[3] } do UIDevice klasy

Wartość tej właściwości jest taka sama dla aplikacji, które pochodzą z ten sam dostawca działający na tym samym urządzeniu. Inną wartością jest zwrócony dla aplikacji na tym samym urządzeniu, które pochodzą od różnych dostawców, oraz dla aplikacje na różnych urządzeniach niezależnie od dostawcy.

Wartość tej właściwości może być zerowa, jeśli aplikacja jest uruchomiona w tle, zanim użytkownik odblokuje urządzenie za pierwszym razem po ponownym uruchomieniu urządzenia. Jeśli wartość jest zerowa, poczekaj i pobierz wartość ponownie później.

Również w iOS 6 możesz użyć klasy asidentifiermanager z adsupport.ramy. Proszę. have

@property(nonatomic, readonly) NSUUID *advertisingIdentifier

Dyskusja, ta sama wartość jest zwracana wszystkim dostawcom. Identyfikator ten może zmienić-np. jeśli użytkownik usunie urządzenie-więc nie należy przechowuj.

Wartość tej właściwości może być zerowa, jeśli aplikacja jest uruchomiona w tle, zanim użytkownik odblokuje urządzenie za pierwszym razem po ponownym uruchomieniu urządzenia. Jeśli wartość jest zerowa, poczekaj i pobierz znowu wartość później.

Edit:

Zwróć uwagę, że advertisingIdentifier może powrócić

00000000-0000-0000-0000-000000000000

Ponieważ wydaje się, że w iOS jest błąd. Podobne pytanie: reklamaidentifier i identifierForVendor return "00000000-0000-0000-0000-000000000000"
 40
Author: Alex Terente,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:53:24

Dla adresu Mac, którego możesz użyć

#import <Foundation/Foundation.h>

@interface MacAddressHelper : NSObject

+ (NSString *)getMacAddress;

@end

Implantacja

#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

Użycie:

NSLog(@"MAC address: %@",[MacAddressHelper getMacAddress]);
 19
Author: Blazer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-05-12 10:40:24

Użyj tego:

NSUUID *id = [[UIDevice currentDevice] identifierForVendor];
NSLog(@"ID: %@", id);
 5
Author: Alexander Volkov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-14 11:05:16

W IOS 5 [[UIDevice currentDevice] uniqueIdentifier] jest przestarzały.

Lepiej użyć -identifierForVendor lub -identifierForAdvertising.

Wiele przydatnych informacji można znaleźć tutaj:

IOS6 UDID - jakie zalety ma identifierForVendor nad identifierForAdvertising?

 4
Author: Andrey,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:51:26

Tutaj możemy znaleźć adres mac dla Urządzenia z systemem IOS za pomocą Asp.net kod C#...

.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 File

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;

    }
 -9
Author: Nikunj Patel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-23 09:14:08