-
-
Save Frulko/6bfe664454583059e250d671b3e254fc to your computer and use it in GitHub Desktop.
Identify iOS devices by a unique identifier (works on latest iOS version)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
#include <dlfcn.h> | |
static const CFStringRef kMGDieID = CFSTR("DieId"); | |
@interface UIDevice (Extensions) | |
- (NSString *)dieID; | |
- (NSString *)batteryID; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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