Skip to content

Instantly share code, notes, and snippets.

@mtsd
Created November 2, 2012 06:00
Show Gist options
  • Select an option

  • Save mtsd/3998976 to your computer and use it in GitHub Desktop.

Select an option

Save mtsd/3998976 to your computer and use it in GitHub Desktop.
UIDevice+deviceToken+UUID (iOS < 6)
#import <Foundation/Foundation.h>
@interface KMDevice : NSObject
@property (nonatomic, readonly) NSString *uuid;
@property (nonatomic, readonly) NSString *deviceToken;
+ (KMDevice *)sharedDevice;
- (void)registerDeviceToken:(NSData *)devToken;
@end
#import "KMDevice.h"
#import "STKeychain.h"
#define APP_RELEASE_SAFELY(__POINTER) { [__POINTER release]; __POINTER = nil; }
static NSString * const UIApplication_UUID_Key = @"UniversallyUniqueIdentifier";
@interface KMDevice ()
- (NSString *)generateUUID;
- (void)loadUUIDFromKeychain;
- (void)saveUUIDToKeychain:(NSString *)uuid;
@end
@implementation KMDevice
@synthesize uuid = _uuid;
+ (KMDevice *)sharedDevice
{
static KMDevice *_instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [[self alloc] init];
[_instance loadUUIDFromKeychain];
});
return _instance;
}
- (void)dealloc
{
APP_RELEASE_SAFELY(_uuid);
APP_RELEASE_SAFELY(_deviceToken);
[super dealloc];
}
- (void)registerDeviceToken:(NSData *)devToken
{
if (_deviceToken) {
APP_RELEASE_SAFELY(_deviceToken);
}
NSString *deviceToken = [[[[devToken description]
stringByReplacingOccurrencesOfString:@"<" withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""]
stringByReplacingOccurrencesOfString:@" " withString: @""];
NSLog(@"deviceToken: %@", deviceToken);
_deviceToken = [deviceToken copy];
}
- (NSString *)uuid
{
if (_uuid && [_uuid length]) return _uuid;
if (_uuid) APP_RELEASE_SAFELY(_uuid);
NSString *gUUID = [self generateUUID];
[self saveUUIDToKeychain:gUUID];
_uuid = [gUUID copy];
return _uuid;
}
- (NSString *)generateUUID
{
// Create universally unique identifier (object)
CFUUIDRef uuidObject = CFUUIDCreate(kCFAllocatorDefault);
// Get the string representation of CFUUID object.
// NSString *uuidStr = [(NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuidObject) autorelease];
CFStringRef uuidString = CFUUIDCreateString( kCFAllocatorDefault, uuidObject );
NSString *uuidStr = (NSString *)CFStringCreateCopy( NULL, uuidString);
// If needed, here is how to get a representation in bytes, returned as a structure
// typedef struct {
// UInt8 byte0;
// UInt8 byte1;
// ...
// UInt8 byte15;
// } CFUUIDBytes;
// CFUUIDBytes bytes = CFUUIDGetUUIDBytes(uuidObject);
CFRelease(uuidObject);
CFRelease(uuidString);
return [uuidStr autorelease];
}
- (void)loadUUIDFromKeychain
{
NSError *error;
NSString *identifier = [NSBundle mainBundle].bundleIdentifier;
NSString *uuid = [STKeychain getPasswordForUsername:UIApplication_UUID_Key andServiceName:identifier error:&error];
if ([uuid length]) {
_uuid = [uuid copy];
}
}
- (void)saveUUIDToKeychain:(NSString *)uuid
{
NSError *error;
NSString *identifier = [NSBundle mainBundle].bundleIdentifier;
[STKeychain storeUsername:UIApplication_UUID_Key andPassword:uuid forServiceName:identifier updateExisting:YES error:&error];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment