Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.54 KB | None | 0 0
  1. #import "UIDevice+IdentifierAddition.h"
  2.  
  3. NSLog(@"%@",[[UIDevice currentDevice] uniqueDeviceIdentifier]);
  4. NSLog(@"%@",[[UIDevice currentDevice] uniqueGlobalDeviceIdentifier]);
  5.  
  6. - (NSString *)createNewUUID
  7. {
  8. CFUUIDRef theUUID = CFUUIDCreate(NULL);
  9. CFStringRef string = CFUUIDCreateString(NULL, theUUID);
  10. CFRelease(theUUID);
  11. return [(NSString *)string autorelease];
  12. }
  13.  
  14. #import "SSKeychain.h"
  15. #import <Security/Security.h>
  16.  
  17. // getting the unique key (if present ) from keychain , assuming "your app identifier" as a key
  18. NSString *retrieveuuid = [SSKeychain passwordForService:@"your app identifier" account:@"user"];
  19. if (retrieveuuid == nil) { // if this is the first time app lunching , create key for device
  20. NSString *uuid = [self createNewUUID];
  21. // save newly created key to Keychain
  22. [SSKeychain setPassword:uuid forService:@"your app identifier" account:@"user"];
  23. // this is the one time process
  24. }
  25.  
  26. NSString *retrieveuuid = [SSKeychain passwordForService:@"your app identifier" account:@"user"];
  27.  
  28. [UIDevice currentDevice].identifierForVendor.UUIDString
  29.  
  30. UIDevice *myDevice=[UIDevice currentDevice];
  31. NSString *UUID = [[myDevice identifierForVendor] UUIDString];
  32.  
  33. #import "UIDevice+IdentifierAddition.h"
  34. #import "NSString+MD5Addition.h"
  35. NSString *iosFiveUDID = [[UIDevice currentDevice] uniqueDeviceIdentifier]
  36.  
  37. #import "UIDevice+IdentifierAddition.h"
  38. #import "NSString+MD5Addition.h"
  39.  
  40. #include <sys/socket.h> // Per msqr
  41. #include <sys/sysctl.h>
  42. #include <net/if.h>
  43. #include <net/if_dl.h>
  44.  
  45. @interface UIDevice(Private)
  46.  
  47. - (NSString *) macaddress;
  48.  
  49. @end
  50.  
  51. @implementation UIDevice (IdentifierAddition)
  52.  
  53. ////////////////////////////////////////////////////////////////////////////////
  54. #pragma mark -
  55. #pragma mark Private Methods
  56.  
  57. // Return the local MAC addy
  58. // Courtesy of FreeBSD hackers email list
  59. // Accidentally munged during previous update. Fixed thanks to erica sadun & mlamb.
  60. - (NSString *) macaddress{
  61.     
  62.     int mib[6];
  63.     size_t len;
  64.     char *buf;
  65.     unsigned char *ptr;
  66.     struct if_msghdr *ifm;
  67.     struct sockaddr_dl *sdl;
  68.     
  69.     mib[0] = CTL_NET;
  70.     mib[1] = AF_ROUTE;
  71.     mib[2] = 0;
  72.     mib[3] = AF_LINK;
  73.     mib[4] = NET_RT_IFLIST;
  74.     
  75.     if ((mib[5] = if_nametoindex("en0")) == 0) {
  76.         printf("Error: if_nametoindex errorn");
  77.         return NULL;
  78.     }
  79.     
  80.     if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
  81.         printf("Error: sysctl, take 1n");
  82.         return NULL;
  83.     }
  84.     
  85.     if ((buf = malloc(len)) == NULL) {
  86.         printf("Could not allocate memory. error!n");
  87.         return NULL;
  88.     }
  89.     
  90.     if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
  91.         printf("Error: sysctl, take 2");
  92.         return NULL;
  93.     }
  94.     
  95.     ifm = (struct if_msghdr *)buf;
  96.     sdl = (struct sockaddr_dl *)(ifm + 1);
  97.     ptr = (unsigned char *)LLADDR(sdl);
  98.     NSString *outstring = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X",
  99.                            *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
  100.     free(buf);
  101.     
  102.     return outstring;
  103. }
  104.  
  105. ////////////////////////////////////////////////////////////////////////////////
  106. #pragma mark -
  107. #pragma mark Public Methods
  108.  
  109. - (NSString *) uniqueDeviceIdentifier{
  110.     NSString *macaddress = [[UIDevice currentDevice] macaddress];
  111.     NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];  
  112.     NSString *stringToHash = [NSString stringWithFormat:@"%@%@",macaddress,bundleIdentifier];
  113.     NSString *uniqueIdentifier = [stringToHash stringFromMD5];  
  114.     return uniqueIdentifier;
  115. }
  116.  
  117. - (NSString *) uniqueGlobalDeviceIdentifier{
  118.     NSString *macaddress = [[UIDevice currentDevice] macaddress];
  119.     NSString *uniqueIdentifier = [macaddress stringFromMD5];    
  120.     return uniqueIdentifier;
  121. }
  122.  
  123. @end
  124.  
  125. #import "NSString+MD5Addition.h"
  126. #import <CommonCrypto/CommonDigest.h>
  127.  
  128. @implementation NSString(MD5Addition)
  129.  
  130. - (NSString *) stringFromMD5{
  131.     
  132.     if(self == nil || [self length] == 0)
  133.         return nil;
  134.     
  135.     const char *value = [self UTF8String];
  136.     
  137.     unsigned char outputBuffer[CC_MD5_DIGEST_LENGTH];
  138.     CC_MD5(value, strlen(value), outputBuffer);
  139.     
  140.     NSMutableString *outputString = [[NSMutableString alloc] initWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
  141.     for(NSInteger count = 0; count < CC_MD5_DIGEST_LENGTH; count++){
  142.         [outputString appendFormat:@"%02x",outputBuffer[count]];
  143.     }
  144.     return [outputString autorelease];
  145. }
  146.  
  147. @end
  148.  
  149. @property(nonatomic, readonly, retain) NSUUID *identifierForVendor
  150.  
  151. +(NSString *) getUUID {
  152.  
  153. //Use the bundle name as the App identifier. No need to get the localized version.
  154.  
  155. NSString *Appname = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
  156.  
  157. //Check if we have UUID already
  158.  
  159. NSString *retrieveuuid = [SSKeychain passwordForService:Appname account:@"user"];
  160.  
  161. if (retrieveuuid == NULL)
  162. {
  163.  
  164. //Create new key for this app/device
  165.  
  166. CFUUIDRef newUniqueId = CFUUIDCreate(kCFAllocatorDefault);
  167.  
  168. retrieveuuid = (__bridge_transfer NSString*)CFUUIDCreateString(kCFAllocatorDefault, newUniqueId);
  169.  
  170. CFRelease(newUniqueId);
  171.  
  172. //Save key to Keychain
  173. [SSKeychain setPassword:retrieveuuid forService:Appname account:@"user"];
  174. }
  175.  
  176. return retrieveuuid;
  177.  
  178. - (NSString *) advertisingIdentifier
  179. {
  180. if (!NSClassFromString(@"ASIdentifierManager")) {
  181. SEL selector = NSSelectorFromString(@"uniqueIdentifier");
  182. if ([[UIDevice currentDevice] respondsToSelector:selector]) {
  183. return [[UIDevice currentDevice] performSelector:selector];
  184. }
  185. }
  186. return [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
  187. }
  188.  
  189. udid = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
  190. NSLog(@"UDID : %@", udid);
  191.  
  192. /* Apple confirmed this bug in their system in response to a Technical Support Incident 
  193.     request. They said that identifierForVendor and advertisingIdentifier sometimes 
  194.     returning all zeros can be seen both in development builds and apps downloaded over the 
  195.     air from the App Store. They have no work around and can't say when the problem will be fixed. */
  196. #define kBuggyASIID @"00000000-0000-0000-0000-000000000000"
  197.  
  198. + (NSString *) getUniqueID {
  199. if (NSClassFromString(@"ASIdentifierManager")) {
  200. NSString * asiID = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
  201. if ([asiID compare:kBuggyASIID] == NSOrderedSame) {
  202. NSLog(@"Error: This device return buggy advertisingIdentifier.");
  203. return [IDManager getUniqueUUID];
  204. } else {
  205. return asiID;
  206. }
  207.  
  208. } else {
  209. return [IDManager getUniqueUUID];
  210. }
  211. }
  212.  
  213.  
  214. + (NSString *) getUniqueUUID {
  215. NSError * error;
  216. NSString * uuid = [KeychainUtils getPasswordForUsername:kBuyassUser andServiceName:kIdOgBetilngService error:&error];
  217. if (error) {
  218. NSLog(@"Error geting unique UUID for this device! %@", [error localizedDescription]);
  219. return nil;
  220. }
  221. if (!uuid) {
  222. DLog(@"No UUID found. Creating a new one.");
  223. uuid = [IDManager GetUUID];
  224. uuid = [Util md5String:uuid];
  225. [KeychainUtils storeUsername:USER_NAME andPassword:uuid forServiceName:SERVICE_NAME updateExisting:YES error:&error];
  226. if (error) {
  227. NSLog(@"Error getting unique UUID for this device! %@", [error localizedDescription]);
  228. return nil;
  229. }
  230. }
  231. return uuid;
  232. }
  233.  
  234. /* NSUUID is after iOS 6. */
  235. + (NSString *)GetUUID
  236. {
  237. CFUUIDRef theUUID = CFUUIDCreate(NULL);
  238. CFStringRef string = CFUUIDCreateString(NULL, theUUID);
  239. CFRelease(theUUID);
  240. return [(NSString *)string autorelease];
  241. }
  242.  
  243. #pragma mark - MAC address
  244. // Return the local MAC addy
  245. // Courtesy of FreeBSD hackers email list
  246. // Last fallback for unique identifier
  247. + (NSString *) getMACAddress
  248. {
  249. int mib[6];
  250. size_t len;
  251. char *buf;
  252. unsigned char *ptr;
  253. struct if_msghdr *ifm;
  254. struct sockaddr_dl *sdl;
  255.  
  256. mib[0] = CTL_NET;
  257. mib[1] = AF_ROUTE;
  258. mib[2] = 0;
  259. mib[3] = AF_LINK;
  260. mib[4] = NET_RT_IFLIST;
  261.  
  262. if ((mib[5] = if_nametoindex("en0")) == 0) {
  263. printf("Error: if_nametoindex errorn");
  264. return NULL;
  265. }
  266.  
  267. if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
  268. printf("Error: sysctl, take 1n");
  269. return NULL;
  270. }
  271.  
  272. if ((buf = malloc(len)) == NULL) {
  273. printf("Error: Memory allocation errorn");
  274. return NULL;
  275. }
  276.  
  277. if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
  278. printf("Error: sysctl, take 2n");
  279. free(buf); // Thanks, Remy "Psy" Demerest
  280. return NULL;
  281. }
  282.  
  283. ifm = (struct if_msghdr *)buf;
  284. sdl = (struct sockaddr_dl *)(ifm + 1);
  285. ptr = (unsigned char *)LLADDR(sdl);
  286. NSString *outstring = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
  287.  
  288. free(buf);
  289. return outstring;
  290. }
  291.  
  292. + (NSString *) getHashedMACAddress
  293. {
  294. NSString * mac = [IDManager getMACAddress];
  295. return [Util md5String:mac];
  296. }
  297.  
  298. + (NSString *)md5String:(NSString *)plainText
  299. {
  300. if(plainText == nil || [plainText length] == 0)
  301. return nil;
  302.  
  303. const char *value = [plainText UTF8String];
  304. unsigned char outputBuffer[CC_MD5_DIGEST_LENGTH];
  305. CC_MD5(value, strlen(value), outputBuffer);
  306.  
  307. NSMutableString *outputString = [[NSMutableString alloc] initWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
  308. for(NSInteger count = 0; count < CC_MD5_DIGEST_LENGTH; count++){
  309. [outputString appendFormat:@"%02x",outputBuffer[count]];
  310. }
  311. NSString * retString = [NSString stringWithString:outputString];
  312. [outputString release];
  313. return retString;
  314. }
  315.  
  316. + (NSString *) getUniqueUUID {
  317. NSError * error;
  318. NSString * uuid = [KeychainUtils getPasswordForUsername:kBuyassUser andServiceName:kIdOgBetilngService error:&error];
  319. if (error) {
  320. NSLog(@"Error geting unique UUID for this device! %@", [error localizedDescription]);
  321. return nil;
  322. }
  323. if (!uuid) {
  324. DLog(@"No UUID found. Creating a new one.");
  325. uuid = [IDManager GetUUID];
  326. uuid = [Util md5String:uuid];
  327. [KeychainUtils storeUsername:USER_NAME andPassword:uuid forServiceName:SERVICE_NAME updateExisting:YES error:&error];
  328. if (error) {
  329. NSLog(@"Error getting unique UUID for this device! %@", [error localizedDescription]);
  330. return nil;
  331. }
  332. }
  333. return uuid;
  334. }
  335.  
  336. -(NSString*)uniqueIDForDevice
  337. {
  338. NSString* uniqueIdentifier = nil;
  339. if( [UIDevice instancesRespondToSelector:@selector(identifierForVendor)] ) { // >=iOS 7
  340. uniqueIdentifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
  341. } else { //<=iOS6, Use UDID of Device
  342. CFUUIDRef uuid = CFUUIDCreate(NULL);
  343. //uniqueIdentifier = ( NSString*)CFUUIDCreateString(NULL, uuid);- for non- ARC
  344. uniqueIdentifier = ( NSString*)CFBridgingRelease(CFUUIDCreateString(NULL, uuid));// for ARC
  345. CFRelease(uuid);
  346. }
  347. }
  348. return uniqueIdentifier;
  349. }
  350.  
  351. 1.) On uninstalling and reinstalling the app identifierForVendor will change.
  352.  
  353. 2.) The value of identifierForVendor remains the same for all the apps installed from the same vendor on the device.
  354.  
  355. 3.) The value of identifierForVendor also changes for all the apps if any of the app (from same vendor) is reinstalled.
  356.  
  357. // Get Bundle Info for Remote Registration (handy if you have more than one app)
  358. NSString *appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
  359. NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
  360.  
  361.  
  362. // Get the users Device Model, Display Name, Unique ID, Token & Version Number
  363. UIDevice *dev = [UIDevice currentDevice];
  364. NSString *deviceUuid=[dev.identifierForVendor UUIDString];
  365.  
  366. NSString *deviceName = dev.name;
  367.  
  368. let strUUID: String = NSUUID().UUIDString
  369.  
  370. let saveSuccessful: Bool = KeychainWrapper.setString("Some String", forKey: "myKey")
  371.  
  372. let retrievedString: String? = KeychainWrapper.stringForKey("myKey")
  373.  
  374. let removeSuccessful: Bool = KeychainWrapper.removeObjectForKey("myKey")
  375.  
  376. import UIKit
  377. import RoutingHTTPServer
  378.  
  379. @UIApplicationMain
  380. class AppDelegate: UIResponder, UIApplicationDelegate {
  381. var bgTask = UIBackgroundTaskInvalid
  382. let server = HTTPServer()
  383.  
  384. func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
  385. application.openURL(NSURL(string: "http://localhost:55555")!)
  386. return true
  387. }
  388.  
  389. func applicationDidEnterBackground(application: UIApplication) {
  390. bgTask = application.beginBackgroundTaskWithExpirationHandler() {
  391. dispatch_async(dispatch_get_main_queue()) {[unowned self] in
  392. application.endBackgroundTask(self.bgTask)
  393. self.bgTask = UIBackgroundTaskInvalid
  394. }
  395. }
  396. }
  397. }
  398.  
  399. class HTTPServer: RoutingHTTPServer {
  400. override init() {
  401. super.init()
  402. setPort(55555)
  403. handleMethod("GET", withPath: "/") {
  404. $1.setHeader("Content-Type", value: "application/x-apple-aspen-config")
  405. $1.respondWithData(NSData(contentsOfFile: NSBundle.mainBundle().pathForResource("udid", ofType: "mobileconfig")!)!)
  406. }
  407. handleMethod("POST", withPath: "/") {
  408. let raw = NSString(data:$0.body(), encoding:NSISOLatin1StringEncoding) as! String
  409. let plistString = raw.substringWithRange(Range(start: raw.rangeOfString("<?xml")!.startIndex,end: raw.rangeOfString("</plist>")!.endIndex))
  410. let plist = NSPropertyListSerialization.propertyListWithData(plistString.dataUsingEncoding(NSISOLatin1StringEncoding)!, options: .allZeros, format: nil, error: nil) as! [String:String]
  411.  
  412. let udid = plist["UDID"]!
  413. println(udid) // Here is your UDID!
  414.  
  415. $1.statusCode = 200
  416. $1.respondWithString("see https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/iPhoneOTAConfiguration/ConfigurationProfileExamples/ConfigurationProfileExamples.html")
  417. }
  418. start(nil)
  419. }
  420. }
  421.  
  422. <?xml version="1.0" encoding="UTF-8"?>
  423. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  424. <plist version="1.0">
  425. <dict>
  426. <key>PayloadContent</key>
  427. <dict>
  428. <key>URL</key>
  429. <string>http://localhost:55555</string>
  430. <key>DeviceAttributes</key>
  431. <array>
  432. <string>IMEI</string>
  433. <string>UDID</string>
  434. <string>PRODUCT</string>
  435. <string>VERSION</string>
  436. <string>SERIAL</string>
  437. </array>
  438. </dict>
  439. <key>PayloadOrganization</key>
  440. <string>udid</string>
  441. <key>PayloadDisplayName</key>
  442. <string>Get Your UDID</string>
  443. <key>PayloadVersion</key>
  444. <integer>1</integer>
  445. <key>PayloadUUID</key>
  446. <string>9CF421B3-9853-9999-BC8A-982CBD3C907C</string>
  447. <key>PayloadIdentifier</key>
  448. <string>udid</string>
  449. <key>PayloadDescription</key>
  450. <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>
  451. <key>PayloadType</key>
  452. <string>Profile Service</string>
  453. </dict>
  454. </plist>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement