2

Hi I am new to Objective C. Please help me how to get the device serial number in iPhone please help me anyone please provide a sample application.

Thanks in advance.

GRaj
  • 31
  • 1
  • 2
  • 10

1 Answers1

7

For iOS:

Apple does not allow developers to access a device serial number on iOS. Alternatively, they provide you with identifierForVendor:

An alphanumeric string that uniquely identifies a device to the app’s vendor. (read-only)

For macOS:

var serialNumber: String? {
  let platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice") )

  guard platformExpert > 0 else {
    return nil
  }

  guard let serialNumber = (IORegistryEntryCreateCFProperty(platformExpert, kIOPlatformSerialNumberKey as CFString, kCFAllocatorDefault, 0).takeUnretainedValue() as? String)?.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) else {
    return nil
  }


  IOObjectRelease(platformExpert)

  return serialNumber
}

Source: https://gist.github.com/leogdion/77f6143ecf793e1ba381917d4b3b286c

meaning-matters
  • 21,929
  • 10
  • 82
  • 142
cyril
  • 3,020
  • 6
  • 36
  • 61
  • Link says "How To Get A Serial Number on macOS in Swift". Does this work in iOS? – meaning-matters Oct 25 '18 at 06:44
  • @meaning-matters Ah good point - hadn't read well. Editing answer. – cyril Oct 25 '18 at 06:46
  • 1
    Be aware, this may not be what you want: https://codeburst.io/unique-identifier-for-the-ios-devices-590bb778290d – meaning-matters Oct 25 '18 at 06:55
  • @meaning-matters good point - although the user does not mention why they require the serial number. Should I edit my answer to include your post? – cyril Oct 25 '18 at 06:57
  • 1
    Be aware that there are two possible memory leaks in the code. 1) If you use `takeUnretainedValue()` you are responsible to release the object (ARC doesn't do it) and 2) `platformExpert` won't be released if `nil` is returned – vadian Oct 25 '18 at 07:02
  • thanks for your replay.@meaning-matters while using "identifierForVendor" I am getting one alphanumeric value but that value is not related to device Serial number. Please help me some other alternative solution – GRaj Oct 25 '18 at 09:35