Created
February 14, 2012 08:40
-
-
Save ishinao/1824855 to your computer and use it in GitHub Desktop.
UIDevice.uniqueIdentifier replace to Application UUID
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
// | |
// UDIDReplace.h | |
// | |
// Created by ishinao on 12/02/14. | |
// | |
// fork from | |
// https://gist.github.com/1161447 | |
// https://gist.github.com/1743326 | |
// | |
#import <Foundation/Foundation.h> | |
#import <Security/Security.h> | |
@interface UIDevice (UDDIReplace) | |
-(NSString*)uniqueIdentifier; | |
-(void)resetUniqueIdentifier; | |
@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
// | |
// UDIDReplace.m | |
// | |
// Created by ishinao on 12/02/14. | |
// | |
// fork from | |
// https://gist.github.com/1161447 | |
// https://gist.github.com/1743326 | |
// | |
#import "UDIDReplace.h" | |
static NSString * const UIDIReplace_Key = @"uniqueInstallationIdentifier"; | |
@implementation UIDevice (UDIDReplace) | |
-(NSString*)uniqueIdentifier { | |
NSString *uuidString = nil; | |
NSDictionary *query = [NSDictionary dictionaryWithObjectsAndKeys: | |
(id)kSecClassGenericPassword, (id)kSecClass, | |
UIDIReplace_Key, (id)kSecAttrGeneric, | |
UIDIReplace_Key, (id)kSecAttrAccount, | |
[[NSBundle mainBundle] bundleIdentifier],(id)kSecAttrService, | |
(id)kSecMatchLimitOne, (id)kSecMatchLimit, | |
(id)kCFBooleanTrue, (id)kSecReturnAttributes, | |
nil]; | |
NSDictionary *attributes = nil; | |
OSStatus result = SecItemCopyMatching((CFDictionaryRef)query, (CFTypeRef *)&attributes); | |
if (result == noErr) { | |
NSMutableDictionary *valueQuery = [NSMutableDictionary dictionaryWithDictionary:attributes]; | |
[attributes release]; | |
[valueQuery setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass]; | |
[valueQuery setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData]; | |
NSData *passwordData = nil; | |
OSStatus result = SecItemCopyMatching((CFDictionaryRef)valueQuery, (CFTypeRef *)&passwordData); | |
if (result == noErr) { | |
uuidString = [[[NSString alloc] initWithBytes:[passwordData bytes] | |
length:[passwordData length] | |
encoding:NSUTF8StringEncoding] autorelease]; | |
[passwordData release]; | |
} | |
} | |
if (uuidString == nil) { | |
CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault); | |
uuidString = (NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuidRef); | |
[uuidString autorelease]; | |
CFRelease(uuidRef); | |
NSMutableDictionary *query = [NSMutableDictionary dictionaryWithObjectsAndKeys: | |
(id)kSecClassGenericPassword, (id)kSecClass, | |
UIDIReplace_Key, (id)kSecAttrGeneric, | |
UIDIReplace_Key, (id)kSecAttrAccount, | |
[[NSBundle mainBundle] bundleIdentifier], (id)kSecAttrService, | |
@"", (id)kSecAttrLabel, | |
@"", (id)kSecAttrDescription, | |
nil]; | |
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4) { | |
[query setObject:(id)kSecAttrAccessibleAfterFirstUnlock forKey:(id)kSecAttrAccessible]; | |
} | |
[query setObject:[uuidString dataUsingEncoding:NSUTF8StringEncoding] forKey:(id)kSecValueData]; | |
OSStatus result = SecItemAdd((CFDictionaryRef)query, NULL); | |
if (result != noErr) { | |
NSLog(@"[ERROR] Couldn't add the Keychain Item. result = %ld query = %@", result, query); | |
return nil; | |
} | |
} | |
return uuidString; | |
} | |
-(void)resetUniqueIdentifier { | |
NSDictionary *query = [NSDictionary dictionaryWithObjectsAndKeys: | |
(id)kSecClassGenericPassword, (id)kSecClass, | |
UIDIReplace_Key, (id)kSecAttrGeneric, | |
UIDIReplace_Key, (id)kSecAttrAccount, | |
[[NSBundle mainBundle] bundleIdentifier],(id)kSecAttrService, | |
nil]; | |
OSStatus result = SecItemDelete((CFDictionaryRef)query); | |
if (result == noErr) { | |
NSLog(@"[INFO} Unique Installation Identifier is successfully reset."); | |
} else if (result == errSecItemNotFound) { | |
NSLog(@"[INFO} Unique Installation Identifier is successfully reset."); | |
} else { | |
NSLog(@"[ERROR] Coudn't delete the Keychain Item. result = %ld query = %@", result, query); | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment