Skip to content

Instantly share code, notes, and snippets.

@Frulko
Forked from anthonya1999/UIDevice+Extensions.h
Created May 23, 2016 06:55
Show Gist options
  • Save Frulko/6bfe664454583059e250d671b3e254fc to your computer and use it in GitHub Desktop.
Save Frulko/6bfe664454583059e250d671b3e254fc to your computer and use it in GitHub Desktop.
Identify iOS devices by a unique identifier (works on latest iOS version)
#import <UIKit/UIKit.h>
#include <dlfcn.h>
static const CFStringRef kMGDieID = CFSTR("DieId");
@interface UIDevice (Extensions)
- (NSString *)dieID;
- (NSString *)batteryID;
@end
#import "UIDevice+Extensions.h"
@implementation UIDevice (Extensions)
- (NSString *)dieID {
void *libMobileGestalt = dlopen("/usr/lib/libMobileGestalt.dylib", RTLD_LAZY);
NSParameterAssert(libMobileGestalt);
CFStringRef (*MGCopyAnswer)(CFStringRef string) = dlsym(libMobileGestalt, "MGCopyAnswer");
NSParameterAssert(MGCopyAnswer);
CFStringRef dieID = MGCopyAnswer(kMGDieID);
NSString *dieIDString = (__bridge NSString *)dieID;
CFRelease(dieID);
dlclose(libMobileGestalt);
return dieIDString;
}
/* Only available on iOS 8+ */
- (NSString *)batteryID {
void *IOKit = dlopen("/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit", RTLD_LAZY);
NSParameterAssert(IOKit);
mach_port_t *kIOMasterPortDefault = dlsym(IOKit, "kIOMasterPortDefault");
NSParameterAssert(kIOMasterPortDefault);
CFMutableDictionaryRef (*IOServiceNameMatching)(const char *name) = dlsym(IOKit, "IOServiceNameMatching");
NSParameterAssert(IOServiceNameMatching);
mach_port_t (*IOServiceGetMatchingService)(mach_port_t masterPort, CFDictionaryRef matching) = dlsym(IOKit, "IOServiceGetMatchingService");
NSParameterAssert(IOServiceGetMatchingService);
kern_return_t (*IORegistryEntryCreateCFProperties)(mach_port_t entry, CFMutableDictionaryRef *properties, CFAllocatorRef allocator, UInt32 options) = dlsym(IOKit, "IORegistryEntryCreateCFProperties");
NSParameterAssert(IORegistryEntryCreateCFProperties);
kern_return_t (*IOObjectRelease)(mach_port_t object) = dlsym(IOKit, "IOObjectRelease");
NSParameterAssert(IOObjectRelease);
CFMutableDictionaryRef properties = NULL;
mach_port_t service = IOServiceGetMatchingService(*kIOMasterPortDefault, IOServiceNameMatching("charger"));
IORegistryEntryCreateCFProperties(service, &properties, kCFAllocatorDefault, 0);
IOObjectRelease(service);
NSDictionary *dictionary = (__bridge NSDictionary *)properties;
NSData *batteryIDData = [dictionary objectForKey:@"battery-id"];
CFRelease(properties);
properties = NULL;
dlclose(IOKit);
return [NSString stringWithUTF8String:[batteryIDData bytes]];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment