Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import "UIDevice+IdentifierAddition.h"
- NSLog(@"%@",[[UIDevice currentDevice] uniqueDeviceIdentifier]);
- NSLog(@"%@",[[UIDevice currentDevice] uniqueGlobalDeviceIdentifier]);
- - (NSString *)createNewUUID
- {
- CFUUIDRef theUUID = CFUUIDCreate(NULL);
- CFStringRef string = CFUUIDCreateString(NULL, theUUID);
- CFRelease(theUUID);
- return [(NSString *)string autorelease];
- }
- #import "SSKeychain.h"
- #import <Security/Security.h>
- // getting the unique key (if present ) from keychain , assuming "your app identifier" as a key
- NSString *retrieveuuid = [SSKeychain passwordForService:@"your app identifier" account:@"user"];
- if (retrieveuuid == nil) { // if this is the first time app lunching , create key for device
- NSString *uuid = [self createNewUUID];
- // save newly created key to Keychain
- [SSKeychain setPassword:uuid forService:@"your app identifier" account:@"user"];
- // this is the one time process
- }
- NSString *retrieveuuid = [SSKeychain passwordForService:@"your app identifier" account:@"user"];
- [UIDevice currentDevice].identifierForVendor.UUIDString
- UIDevice *myDevice=[UIDevice currentDevice];
- NSString *UUID = [[myDevice identifierForVendor] UUIDString];
- #import "UIDevice+IdentifierAddition.h"
- #import "NSString+MD5Addition.h"
- NSString *iosFiveUDID = [[UIDevice currentDevice] uniqueDeviceIdentifier]
- #import "UIDevice+IdentifierAddition.h"
- #import "NSString+MD5Addition.h"
- #include <sys/socket.h> // Per msqr
- #include <sys/sysctl.h>
- #include <net/if.h>
- #include <net/if_dl.h>
- @interface UIDevice(Private)
- - (NSString *) macaddress;
- @end
- @implementation UIDevice (IdentifierAddition)
- ////////////////////////////////////////////////////////////////////////////////
- #pragma mark -
- #pragma mark Private Methods
- // Return the local MAC addy
- // Courtesy of FreeBSD hackers email list
- // Accidentally munged during previous update. Fixed thanks to erica sadun & mlamb.
- - (NSString *) macaddress{
- int mib[6];
- size_t len;
- char *buf;
- unsigned char *ptr;
- struct if_msghdr *ifm;
- struct sockaddr_dl *sdl;
- mib[0] = CTL_NET;
- mib[1] = AF_ROUTE;
- mib[2] = 0;
- mib[3] = AF_LINK;
- mib[4] = NET_RT_IFLIST;
- if ((mib[5] = if_nametoindex("en0")) == 0) {
- printf("Error: if_nametoindex errorn");
- return NULL;
- }
- if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
- printf("Error: sysctl, take 1n");
- return NULL;
- }
- if ((buf = malloc(len)) == NULL) {
- printf("Could not allocate memory. error!n");
- return NULL;
- }
- if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
- printf("Error: sysctl, take 2");
- return NULL;
- }
- ifm = (struct if_msghdr *)buf;
- sdl = (struct sockaddr_dl *)(ifm + 1);
- ptr = (unsigned char *)LLADDR(sdl);
- NSString *outstring = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X",
- *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
- free(buf);
- return outstring;
- }
- ////////////////////////////////////////////////////////////////////////////////
- #pragma mark -
- #pragma mark Public Methods
- - (NSString *) uniqueDeviceIdentifier{
- NSString *macaddress = [[UIDevice currentDevice] macaddress];
- NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
- NSString *stringToHash = [NSString stringWithFormat:@"%@%@",macaddress,bundleIdentifier];
- NSString *uniqueIdentifier = [stringToHash stringFromMD5];
- return uniqueIdentifier;
- }
- - (NSString *) uniqueGlobalDeviceIdentifier{
- NSString *macaddress = [[UIDevice currentDevice] macaddress];
- NSString *uniqueIdentifier = [macaddress stringFromMD5];
- return uniqueIdentifier;
- }
- @end
- #import "NSString+MD5Addition.h"
- #import <CommonCrypto/CommonDigest.h>
- @implementation NSString(MD5Addition)
- - (NSString *) stringFromMD5{
- if(self == nil || [self length] == 0)
- return nil;
- const char *value = [self UTF8String];
- unsigned char outputBuffer[CC_MD5_DIGEST_LENGTH];
- CC_MD5(value, strlen(value), outputBuffer);
- NSMutableString *outputString = [[NSMutableString alloc] initWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
- for(NSInteger count = 0; count < CC_MD5_DIGEST_LENGTH; count++){
- [outputString appendFormat:@"%02x",outputBuffer[count]];
- }
- return [outputString autorelease];
- }
- @end
- @property(nonatomic, readonly, retain) NSUUID *identifierForVendor
- +(NSString *) getUUID {
- //Use the bundle name as the App identifier. No need to get the localized version.
- NSString *Appname = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
- //Check if we have UUID already
- NSString *retrieveuuid = [SSKeychain passwordForService:Appname account:@"user"];
- if (retrieveuuid == NULL)
- {
- //Create new key for this app/device
- CFUUIDRef newUniqueId = CFUUIDCreate(kCFAllocatorDefault);
- retrieveuuid = (__bridge_transfer NSString*)CFUUIDCreateString(kCFAllocatorDefault, newUniqueId);
- CFRelease(newUniqueId);
- //Save key to Keychain
- [SSKeychain setPassword:retrieveuuid forService:Appname account:@"user"];
- }
- return retrieveuuid;
- - (NSString *) advertisingIdentifier
- {
- if (!NSClassFromString(@"ASIdentifierManager")) {
- SEL selector = NSSelectorFromString(@"uniqueIdentifier");
- if ([[UIDevice currentDevice] respondsToSelector:selector]) {
- return [[UIDevice currentDevice] performSelector:selector];
- }
- }
- return [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
- }
- udid = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
- NSLog(@"UDID : %@", udid);
- /* Apple confirmed this bug in their system in response to a Technical Support Incident
- request. They said that identifierForVendor and advertisingIdentifier sometimes
- returning all zeros can be seen both in development builds and apps downloaded over the
- air from the App Store. They have no work around and can't say when the problem will be fixed. */
- #define kBuggyASIID @"00000000-0000-0000-0000-000000000000"
- + (NSString *) getUniqueID {
- if (NSClassFromString(@"ASIdentifierManager")) {
- NSString * asiID = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
- if ([asiID compare:kBuggyASIID] == NSOrderedSame) {
- NSLog(@"Error: This device return buggy advertisingIdentifier.");
- return [IDManager getUniqueUUID];
- } else {
- return asiID;
- }
- } else {
- return [IDManager getUniqueUUID];
- }
- }
- + (NSString *) getUniqueUUID {
- NSError * error;
- NSString * uuid = [KeychainUtils getPasswordForUsername:kBuyassUser andServiceName:kIdOgBetilngService error:&error];
- if (error) {
- NSLog(@"Error geting unique UUID for this device! %@", [error localizedDescription]);
- return nil;
- }
- if (!uuid) {
- DLog(@"No UUID found. Creating a new one.");
- uuid = [IDManager GetUUID];
- uuid = [Util md5String:uuid];
- [KeychainUtils storeUsername:USER_NAME andPassword:uuid forServiceName:SERVICE_NAME updateExisting:YES error:&error];
- if (error) {
- NSLog(@"Error getting unique UUID for this device! %@", [error localizedDescription]);
- return nil;
- }
- }
- return uuid;
- }
- /* NSUUID is after iOS 6. */
- + (NSString *)GetUUID
- {
- CFUUIDRef theUUID = CFUUIDCreate(NULL);
- CFStringRef string = CFUUIDCreateString(NULL, theUUID);
- CFRelease(theUUID);
- return [(NSString *)string autorelease];
- }
- #pragma mark - MAC address
- // Return the local MAC addy
- // Courtesy of FreeBSD hackers email list
- // Last fallback for unique identifier
- + (NSString *) getMACAddress
- {
- int mib[6];
- size_t len;
- char *buf;
- unsigned char *ptr;
- struct if_msghdr *ifm;
- struct sockaddr_dl *sdl;
- mib[0] = CTL_NET;
- mib[1] = AF_ROUTE;
- mib[2] = 0;
- mib[3] = AF_LINK;
- mib[4] = NET_RT_IFLIST;
- if ((mib[5] = if_nametoindex("en0")) == 0) {
- printf("Error: if_nametoindex errorn");
- return NULL;
- }
- if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
- printf("Error: sysctl, take 1n");
- return NULL;
- }
- if ((buf = malloc(len)) == NULL) {
- printf("Error: Memory allocation errorn");
- return NULL;
- }
- if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
- printf("Error: sysctl, take 2n");
- free(buf); // Thanks, Remy "Psy" Demerest
- return NULL;
- }
- ifm = (struct if_msghdr *)buf;
- sdl = (struct sockaddr_dl *)(ifm + 1);
- ptr = (unsigned char *)LLADDR(sdl);
- NSString *outstring = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
- free(buf);
- return outstring;
- }
- + (NSString *) getHashedMACAddress
- {
- NSString * mac = [IDManager getMACAddress];
- return [Util md5String:mac];
- }
- + (NSString *)md5String:(NSString *)plainText
- {
- if(plainText == nil || [plainText length] == 0)
- return nil;
- const char *value = [plainText UTF8String];
- unsigned char outputBuffer[CC_MD5_DIGEST_LENGTH];
- CC_MD5(value, strlen(value), outputBuffer);
- NSMutableString *outputString = [[NSMutableString alloc] initWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
- for(NSInteger count = 0; count < CC_MD5_DIGEST_LENGTH; count++){
- [outputString appendFormat:@"%02x",outputBuffer[count]];
- }
- NSString * retString = [NSString stringWithString:outputString];
- [outputString release];
- return retString;
- }
- + (NSString *) getUniqueUUID {
- NSError * error;
- NSString * uuid = [KeychainUtils getPasswordForUsername:kBuyassUser andServiceName:kIdOgBetilngService error:&error];
- if (error) {
- NSLog(@"Error geting unique UUID for this device! %@", [error localizedDescription]);
- return nil;
- }
- if (!uuid) {
- DLog(@"No UUID found. Creating a new one.");
- uuid = [IDManager GetUUID];
- uuid = [Util md5String:uuid];
- [KeychainUtils storeUsername:USER_NAME andPassword:uuid forServiceName:SERVICE_NAME updateExisting:YES error:&error];
- if (error) {
- NSLog(@"Error getting unique UUID for this device! %@", [error localizedDescription]);
- return nil;
- }
- }
- return uuid;
- }
- -(NSString*)uniqueIDForDevice
- {
- NSString* uniqueIdentifier = nil;
- if( [UIDevice instancesRespondToSelector:@selector(identifierForVendor)] ) { // >=iOS 7
- uniqueIdentifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
- } else { //<=iOS6, Use UDID of Device
- CFUUIDRef uuid = CFUUIDCreate(NULL);
- //uniqueIdentifier = ( NSString*)CFUUIDCreateString(NULL, uuid);- for non- ARC
- uniqueIdentifier = ( NSString*)CFBridgingRelease(CFUUIDCreateString(NULL, uuid));// for ARC
- CFRelease(uuid);
- }
- }
- return uniqueIdentifier;
- }
- 1.) On uninstalling and reinstalling the app identifierForVendor will change.
- 2.) The value of identifierForVendor remains the same for all the apps installed from the same vendor on the device.
- 3.) The value of identifierForVendor also changes for all the apps if any of the app (from same vendor) is reinstalled.
- // Get Bundle Info for Remote Registration (handy if you have more than one app)
- NSString *appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
- NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
- // Get the users Device Model, Display Name, Unique ID, Token & Version Number
- UIDevice *dev = [UIDevice currentDevice];
- NSString *deviceUuid=[dev.identifierForVendor UUIDString];
- NSString *deviceName = dev.name;
- let strUUID: String = NSUUID().UUIDString
- let saveSuccessful: Bool = KeychainWrapper.setString("Some String", forKey: "myKey")
- let retrievedString: String? = KeychainWrapper.stringForKey("myKey")
- let removeSuccessful: Bool = KeychainWrapper.removeObjectForKey("myKey")
- import UIKit
- import RoutingHTTPServer
- @UIApplicationMain
- class AppDelegate: UIResponder, UIApplicationDelegate {
- var bgTask = UIBackgroundTaskInvalid
- let server = HTTPServer()
- func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
- application.openURL(NSURL(string: "http://localhost:55555")!)
- return true
- }
- func applicationDidEnterBackground(application: UIApplication) {
- bgTask = application.beginBackgroundTaskWithExpirationHandler() {
- dispatch_async(dispatch_get_main_queue()) {[unowned self] in
- application.endBackgroundTask(self.bgTask)
- self.bgTask = UIBackgroundTaskInvalid
- }
- }
- }
- }
- class HTTPServer: RoutingHTTPServer {
- override init() {
- super.init()
- setPort(55555)
- handleMethod("GET", withPath: "/") {
- $1.setHeader("Content-Type", value: "application/x-apple-aspen-config")
- $1.respondWithData(NSData(contentsOfFile: NSBundle.mainBundle().pathForResource("udid", ofType: "mobileconfig")!)!)
- }
- handleMethod("POST", withPath: "/") {
- let raw = NSString(data:$0.body(), encoding:NSISOLatin1StringEncoding) as! String
- let plistString = raw.substringWithRange(Range(start: raw.rangeOfString("<?xml")!.startIndex,end: raw.rangeOfString("</plist>")!.endIndex))
- let plist = NSPropertyListSerialization.propertyListWithData(plistString.dataUsingEncoding(NSISOLatin1StringEncoding)!, options: .allZeros, format: nil, error: nil) as! [String:String]
- let udid = plist["UDID"]!
- println(udid) // Here is your UDID!
- $1.statusCode = 200
- $1.respondWithString("see https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/iPhoneOTAConfiguration/ConfigurationProfileExamples/ConfigurationProfileExamples.html")
- }
- start(nil)
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
- <plist version="1.0">
- <dict>
- <key>PayloadContent</key>
- <dict>
- <key>URL</key>
- <string>http://localhost:55555</string>
- <key>DeviceAttributes</key>
- <array>
- <string>IMEI</string>
- <string>UDID</string>
- <string>PRODUCT</string>
- <string>VERSION</string>
- <string>SERIAL</string>
- </array>
- </dict>
- <key>PayloadOrganization</key>
- <string>udid</string>
- <key>PayloadDisplayName</key>
- <string>Get Your UDID</string>
- <key>PayloadVersion</key>
- <integer>1</integer>
- <key>PayloadUUID</key>
- <string>9CF421B3-9853-9999-BC8A-982CBD3C907C</string>
- <key>PayloadIdentifier</key>
- <string>udid</string>
- <key>PayloadDescription</key>
- <string>Install this temporary profile to find and display your current device's UDID. It is automatically removed from device right after you get your UDID.</string>
- <key>PayloadType</key>
- <string>Profile Service</string>
- </dict>
- </plist>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement