博客> iOS UUID
iOS UUID
1小时前 评论:0 阅读:200 易_君
ios oc UUID UDID替代UUID

一、UDID (Unique Device Identifier) UDID的全称是Unique Device Identifier,顾名思义,它就是苹果IOS设备的唯一识别码,它由40个字符的字母和数字组成。在很多需要限制一台设备一个账号的应用中经常会用到。在iOS5中可以获取到设备的UDID,后来被苹果禁止了。 二、UUID(Universally Unique Identifier) UUID是Universally Unique Identifier的缩写,中文意思是通用唯一识别码。它是让分布式系统中的所有元素,都能有唯一的辨识资讯,而不需要透过中央控制端来做辨识资讯的指定。这样,每个人都可以建立不与其它人冲突的 UUID。在此情况下,就不需考虑数据库建立时的名称重复问题。苹果公司建议使用UUID为应用生成唯一标识字符串。

开发者可以在应用第一次启动时调用一 次,然后将该串存储起来,替代UDID来使用。但是,如果用户删除该应用再次安装时,又会生成新的字符串,所以不能保证唯一识别该设备。使用UUID,就要考虑应用被删除后再重新安装时的处理。一个解决的办法是:UUID一般只生成一次,保存在iOS系统里面,如果应用删除了,重装应用之后它的UUID还是一样的,除非系统重置 。但是不能保证在以后的系统升级后还能用(如果系统保存了该信息就能用)。 // // KeyChainStore.h // UDID替代 // // Created by 易元江 on 2017/7/14. // Copyright © 2017年 com.jianzhen.zj. All rights reserved. //

import

@interface KeyChainStore : NSObject

  • (void)save:(NSString *)service data:(id)data;
  • (id)load:(NSString *)service;
  • (void)deleteKeyData:(NSString *)service;

@end

// // KeyChainStore.m // UDID替代 // // Created by 易元江 on 2017/7/14. // Copyright © 2017年 com.jianzhen.zj. All rights reserved. //

import "KeyChainStore.h"

@implementation KeyChainStore

  • (NSMutableDictionary )getKeychainQuery:(NSString )service {

    return [NSMutableDictionary dictionaryWithObjectsAndKeys: (id)kSecClassGenericPassword,(id)kSecClass, service, (id)kSecAttrService, service, (id)kSecAttrAccount, (id)kSecAttrAccessibleAfterFirstUnlock,(id)kSecAttrAccessible, nil,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)deleteKeyData:(NSString *)service {

    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service]; SecItemDelete((CFDictionaryRef)keychainQuery); }

@end

/**/ // // UUID.h // UDID替代 // // Created by 易元江 on 2017/7/14. // Copyright © 2017年 com.jianzhen.zj. All rights reserved. //

import

@interface UUID : NSObject /* 获取UUID / +(NSString *)getUUID;

/ 保存UUID / +(void)saveUUID:(NSString )uuid; / 获取UUID */ +(id)readUUID; /* 删除 / +(void)deleteUUID;

@end

// // UUID.m // UDID替代 // // Created by 易元江 on 2017/7/14. // Copyright © 2017年 com.jianzhen.zj. All rights reserved. //

import "UUID.h"

import "KeyChainStore.h"

//项目的bundleId static NSString const KEY_IN_KEYCHAIN = @"com.jianzhen.portservice.Service.Service"; //项目的bundleId.uuid static NSString const KEY_UUID = @"com.jianzhen.portservice.Service.Service.uuid";

@implementation UUID

  • (void)saveUUID:(NSString )uuid { NSMutableDictionary usernameUuidPairs = [NSMutableDictionary dictionary]; [usernameUuidPairs setObject:uuid forKey:KEY_UUID]; [KeyChainStore save:KEY_IN_KEYCHAIN data:usernameUuidPairs]; }

  • (id)readUUID { NSMutableDictionary usernameUuidPairs = (NSMutableDictionary )[KeyChainStore load:KEY_IN_KEYCHAIN]; return [usernameUuidPairs objectForKey:KEY_UUID]; }

  • (void)deleteUUID { [KeyChainStore deleteKeyData:KEY_IN_KEYCHAIN]; }

  • (NSString )getUUID { //获取项目的bundle ID NSString bundleId = [[NSBundle mainBundle] bundleIdentifier]; //根据bundle ID拼接一个自定义的key用来作为keychain里面的唯一标示 //NSString *keyUUid = [NSString stringWithFormat:@"%@.uuid",bundleId]; //将bundle ID作为唯一key在keychain里面获取保存的uuid

    NSString strUUID = (NSString )[KeyChainStore load:bundleId];

    //首次执行该方法时,uuid为空 if ([strUUID isEqualToString:@""] || !strUUID) { //生成一个uuid的方法 CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);

    strUUID = (NSString *)CFBridgingRelease(CFUUIDCreateString (kCFAllocatorDefault,uuidRef));
    
    //将该uuid保存到keychain
    [KeyChainStore save:bundleId data:strUUID];

    } return strUUID; }

@end

收藏
0
sina weixin mail 回到顶部