UDID

UDID (即Unique Device Identifier)是一个由子母和数字组成的 40 个字符串的序号,用来区别包括 iPhones, iPads, 以及 iPod Touches等iOS设备,这些编码看起来是随机的,实际上是跟硬件设备特点相联系的。

使用keyChain存储UUID替代UDID

  • 导入Security.framework

  • 创建工具类”JPKeyChain”

JPKeychain.h
//
//  JPKeychain.h
//  IFood517
//
//  Created by YYQ on 14/11/13.
//  Copyright (c) 2014年 YYQ. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <Security/Security.h>

@interface JPKeychain : NSObject
+ (void)save:(NSString *)service data:(id)data;
+ (id)load:(NSString *)service;
+ (void)delete:(NSString *)service;
@end  
JPKeychain.m
//
//  JPKeychain.m
//  IFood517
//
//  Created by YYQ on 14/11/13.
//  Copyright (c) 2014年 YYQ. All rights reserved.
//

#import "JPKeychain.h"

@implementation JPKeychain
+ (NSMutableDictionary *)getKeychainQuery:(NSString *)service {
    return [NSMutableDictionary dictionaryWithObjectsAndKeys:
            (id)kSecClassGenericPassword,(id)kSecClass,
            service, (id)kSecAttrService,
            service, (id)kSecAttrAccount,
            (id)kSecAttrAccessibleAfterFirstUnlock,(id)kSecAttrAccessible,
            nil];
}

+ (void)save:(NSString *)service data:(id)data {
    //Get search dictionary
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
    //Delete old item before add new item
    SecItemDelete((CFDictionaryRef)keychainQuery);
    //Add new object to search dictionary(Attention:the data format)
    [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(id)kSecValueData];
    //Add item to keychain with the search dictionary
    SecItemAdd((CFDictionaryRef)keychainQuery, NULL);
}

+ (id)load:(NSString *)service {
    id ret = nil;
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
    //Configure the search setting
    //Since in our simple case we are expecting only a single attribute to be returned (the password) we can set the attribute kSecReturnData to kCFBooleanTrue
    [keychainQuery setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
    [keychainQuery setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];
    CFDataRef keyData = NULL;
    if (SecItemCopyMatching((CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {
        @try {
            ret = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData *)keyData];
        } @catch (NSException *e) {
            NSLog(@"Unarchive of %@ failed: %@", service, e);
        } @finally {
        }
    }
    if (keyData)
        CFRelease(keyData);
    return ret;
}

+ (void)delete:(NSString *)service {
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
    SecItemDelete((CFDictionaryRef)keychainQuery);
}

@end
  • 有了这个工具类,我们就可以在keyChain中存储一些关键数据,比如用户名,密码之类,那么我们同样也就可以把UUID存进去替代UDID了

使用

#import "CommonCrypto/CommonDigest.h"
#import "JPKeychain.h"


NSString * const KEY_UDID_INSTEAD = @"com.jpgk.app.udid.instead.test";

#pragma mark - Keychain 获取UUID
+(NSString *)getDeviceIDInKeychain
{
    NSString *getUDIDInKeychain = (NSString *)[JPKeychain load:KEY_UDID_INSTEAD];
    NSLog(@"从keychain中获取到的 UDID_INSTEAD %@",getUDIDInKeychain);
    if (!getUDIDInKeychain ||[getUDIDInKeychain isEqualToString:@""]||[getUDIDInKeychain isKindOfClass:[NSNull class]]) {
        CFUUIDRef puuid = CFUUIDCreate( nil );
        CFStringRef uuidString = CFUUIDCreateString( nil, puuid );
        NSString * result = (NSString *)CFBridgingRelease(CFStringCreateCopy( NULL, uuidString));
        CFRelease(puuid);
        CFRelease(uuidString);
        [JPKeychain save:KEY_UDID_INSTEAD data:result];
        getUDIDInKeychain = (NSString *)[JPKeychain load:KEY_UDID_INSTEAD];
    }
    NSLog(@"最终 ———— UDID_INSTEAD %@",getUDIDInKeychain);
    return getUDIDInKeychain;
}

#更新 ————

pod 'KeyChain-UDID'
pod install
  • 在需要使用的类中
#import <YYQKeyChain.h>
...
...
 NSString *udid = [YYQKeyChain getUDIDWithUniqueKey:@"com.comname.app.udid.instead"];
NSLog(@"udid from keyChain %@", udid);

本文地址: http://yongqianvip.github.io/2014/11/13/getUDID/