Domanda

    

Questa domanda ha già una risposta qui:

         

Ho un'applicazione che utilizza riposo per comunicare con un server, vorrei ottenere l'iPhone sia indirizzo MAC o ID del dispositivo per la convalida l'unicità, come può essere fatto?

È stato utile?

Soluzione

[[UIDevice currentDevice] uniqueIdentifier] è garantito per essere univoco per ogni dispositivo.

Altri suggerimenti

  

uniqueIdentifier (deprecato in iOS 5.0. Invece, creare un identificatore univoco specifico per la tua app.)

La documentazione raccomandano uso di CFUUIDCreate invece di [[UIDevice currentDevice] uniqueIdentifier]

Quindi, ecco come si genera un id unico nella vostra app

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

CFRelease(uuidRef);

Si noti che si deve salvare l'uuidString in impostazioni predefinite dell'utente o in altro luogo, perché non è possibile generare nuovamente lo stesso uuidString.

È possibile utilizzare UIPasteboard per memorizzare il vostro UUID generato. E se l'applicazione verrà eliminato e reinstallato potete leggere dal UIPasteboard vecchio uuid. La scheda di pasta sarà spazzato via quando il dispositivo verrà cancellata.

In iOS 6 hanno introdotto il NSUUID Classe che è stato progettato per creare stringhe UUID

Inoltre hanno aggiunto in iOS 6 @property(nonatomic, readonly, retain) NSUUID *identifierForVendor al UIDevice classe

  

Il valore di questa proprietà è la stessa per le applicazioni che vengono dal   stesso fornitore esecuzione sullo stesso dispositivo. Un valore diverso viene restituito   per le applicazioni sullo stesso dispositivo che provengono da diversi fornitori, e per   applicazioni su diversi dispositivi indipendentemente dal vendor.

     

Il valore di questa proprietà può essere pari a zero se l'applicazione è in esecuzione in   sfondo, prima che l'utente ha sbloccato il dispositivo per la prima volta   dopo che il dispositivo è stato riavviato. Se il valore è pari a zero, aspettare e ottenere   il valore più tardi.

Anche in iOS 6 è possibile utilizzare ASIdentifierManager classe da AdSupport.framework. Ci avete

@property(nonatomic, readonly) NSUUID *advertisingIdentifier
  

Discussione A differenza della proprietà identifierForVendor del UIDevice,   lo stesso valore viene restituito a tutti i fornitori. Questo identificatore può   modificare, ad esempio, se l'utente cancella il dispositivo in modo da non dovrebbe   la cache di esso.

     

Il valore di questa proprietà può essere pari a zero se l'applicazione è in esecuzione in   sfondo, prima che l'utente ha sbloccato il dispositivo per la prima volta   dopo che il dispositivo è stato riavviato. Se il valore è pari a zero, aspettare e ottenere   il valore più tardi.

Modifica:

Fare attenzione che il advertisingIdentifier può restituire

  

00000000-0000-0000-0000-000000000000

, perché sembra che ci sia un bug in iOS. questione connessa: Il ritorno advertisingIdentifier e identifierForVendor "00000000-0000-0000 -0000-000000000000"

Per un Mac Adress si potrebbe usare

#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

Usa:

NSLog(@"MAC address: %@",[MacAddressHelper getMacAddress]);

Utilizzare questa:

NSUUID *id = [[UIDevice currentDevice] identifierForVendor];
NSLog(@"ID: %@", id);

In IOS 5 [[UIDevice currentDevice] uniqueIdentifier] è deprecato.

E 'meglio usare -identifierForVendor o -identifierForAdvertising.

Un sacco di informazioni utili si possono trovare qui:

iOS6 UDID - Quali sono i vantaggi identifierForVendor hanno più identifierForAdvertising?

Qui, si può trovare l'indirizzo MAC per il dispositivo IOS con Asp.net codice 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;

    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top