iOS 获取唯一标识

目前获取唯一标识一般采用UUID+keychain来使用,实测可以做到,不过还没上架AppStore不确定能不能通过审核。


NSString *SERVICE_NAME = @"com.xinliao.dongneng";//最好用程序的bundle id

NSString * str =  [SFHFKeychainUtils getPasswordForUsername:@"UUID" andServiceName:SERVICE_NAME error:nil];  // 从keychain获取数据

if ([str length]<=0)

{

str  = [[[UIDevice currentDevice] identifierForVendor] UUIDString];  // 保存UUID作为手机唯一标识符

[SFHFKeychainUtils storeUsername:@"UUID"

andPassword:str

forServiceName:SERVICE_NAME

updateExisting:1

error:nil];  // 往keychain添加数据

}




创建文件 SFHFKeychainUtils


//.h文件

#import@interface SFHFKeychainUtils : NSObject {

}

+ (NSString *) getPasswordForUsername: (NSString *) username andServiceName: (NSString *) serviceName error: (NSError **) error;

+ (BOOL) storeUsername: (NSString *) username andPassword: (NSString *) password forServiceName: (NSString *) serviceName updateExisting: (BOOL) updateExisting error: (NSError **) error;

+ (BOOL) deleteItemForUsername: (NSString *) username andServiceName: (NSString *) serviceName error: (NSError **) error;

@end



//.m文件

////  SFHFKeychainUtils.m////  Created by Buzz Andersen on 10/20/08.//  Based partly on code by Jonathan Wight, Jon Crosby, and Mike Malone.//  Copyright 2008 Sci-Fi Hi-Fi. All rights reserved.////  Permission is hereby granted, free of charge, to any person//  obtaining a copy of this software and associated documentation//  files (the "Software"), to deal in the Software without//  restriction, including without limitation the rights to use,//  copy, modify, merge, publish, distribute, sublicense, and/or sell//  copies of the Software, and to permit persons to whom the//  Software is furnished to do so, subject to the following//  conditions:////  The above copyright notice and this permission notice shall be//  included in all copies or substantial portions of the Software.////  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,//  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES//  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND//  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT//  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,//  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING//  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR//  OTHER DEALINGS IN THE SOFTWARE.//#import "SFHFKeychainUtils.h"#importstatic NSString *SFHFKeychainUtilsErrorDomain = @"SFHFKeychainUtilsErrorDomain";

#if __IPHONE_OS_VERSION_MIN_REQUIRED < 30000 && TARGET_IPHONE_SIMULATOR

@interface SFHFKeychainUtils (PrivateMethods)

+ (SecKeychainItemRef) getKeychainItemReferenceForUsername: (NSString *) username andServiceName: (NSString *) serviceName error: (NSError **) error;

@end

#endif

@implementation SFHFKeychainUtils

#if __IPHONE_OS_VERSION_MIN_REQUIRED < 30000 && TARGET_IPHONE_SIMULATOR

+ (NSString *) getPasswordForUsername: (NSString *) username andServiceName: (NSString *) serviceName error: (NSError **) error {

if (!username || !serviceName) {

*error = [NSError errorWithDomain: SFHFKeychainUtilsErrorDomain code: -2000 userInfo: nil];

return nil;

}

SecKeychainItemRef item = [SFHFKeychainUtils getKeychainItemReferenceForUsername: username andServiceName: serviceName error: error];

if (*error || !item) {

return nil;

}

// from Advanced Mac OS X Programming, ch. 16

UInt32 length;

char *password;

SecKeychainAttribute attributes[8];

SecKeychainAttributeList list;

attributes[0].tag = kSecAccountItemAttr;

attributes[1].tag = kSecDescriptionItemAttr;

attributes[2].tag = kSecLabelItemAttr;

attributes[3].tag = kSecModDateItemAttr;

list.count = 4;

list.attr = attributes;

OSStatus status = SecKeychainItemCopyContent(item, NULL, &list, &length, (void **)&password);

if (status != noErr) {

*error = [NSError errorWithDomain: SFHFKeychainUtilsErrorDomain code: status userInfo: nil];

return nil;

}

NSString *passwordString = nil;

if (password != NULL) {

char passwordBuffer[1024];

if (length > 1023) {

length = 1023;

}

strncpy(passwordBuffer, password, length);

passwordBuffer[length] = '\0';

passwordString = [NSString stringWithCString:passwordBuffer];

}

SecKeychainItemFreeContent(&list, password);

CFRelease(item);

return passwordString;

}

+ (void) storeUsername: (NSString *) username andPassword: (NSString *) password forServiceName: (NSString *) serviceName updateExisting: (BOOL) updateExisting error: (NSError **) error {

if (!username || !password || !serviceName) {

*error = [NSError errorWithDomain: SFHFKeychainUtilsErrorDomain code: -2000 userInfo: nil];

return;

}

OSStatus status = noErr;

SecKeychainItemRef item = [SFHFKeychainUtils getKeychainItemReferenceForUsername: username andServiceName: serviceName error: error];

if (*error && [*error code] != noErr) {

return;

}

*error = nil;

if (item) {

status = SecKeychainItemModifyAttributesAndData(item,

NULL,

strlen([password UTF8String]),

[password UTF8String]);

CFRelease(item);

}

else {

status = SecKeychainAddGenericPassword(NULL,

strlen([serviceName UTF8String]),

[serviceName UTF8String],

strlen([username UTF8String]),

[username UTF8String],

strlen([password UTF8String]),

[password UTF8String],

NULL);

}

if (status != noErr) {

*error = [NSError errorWithDomain: SFHFKeychainUtilsErrorDomain code: status userInfo: nil];

}

}

+ (void) deleteItemForUsername: (NSString *) username andServiceName: (NSString *) serviceName error: (NSError **) error {

if (!username || !serviceName) {

*error = [NSError errorWithDomain: SFHFKeychainUtilsErrorDomain code: 2000 userInfo: nil];

return;

}

*error = nil;

SecKeychainItemRef item = [SFHFKeychainUtils getKeychainItemReferenceForUsername: username andServiceName: serviceName error: error];

if (*error && [*error code] != noErr) {

return;

}

OSStatus status;

if (item) {

status = SecKeychainItemDelete(item);

CFRelease(item);

}

if (status != noErr) {

*error = [NSError errorWithDomain: SFHFKeychainUtilsErrorDomain code: status userInfo: nil];

}

}

+ (SecKeychainItemRef) getKeychainItemReferenceForUsername: (NSString *) username andServiceName: (NSString *) serviceName error: (NSError **) error {

if (!username || !serviceName) {

*error = [NSError errorWithDomain: SFHFKeychainUtilsErrorDomain code: -2000 userInfo: nil];

return nil;

}

*error = nil;

SecKeychainItemRef item;

OSStatus status = SecKeychainFindGenericPassword(NULL,

strlen([serviceName UTF8String]),

[serviceName UTF8String],

strlen([username UTF8String]),

[username UTF8String],

NULL,

NULL,

&item);

if (status != noErr) {

if (status != errSecItemNotFound) {

*error = [NSError errorWithDomain: SFHFKeychainUtilsErrorDomain code: status userInfo: nil];

}

return nil;

}

return item;

}

#else

+ (NSString *) getPasswordForUsername: (NSString *) username andServiceName: (NSString *) serviceName error: (NSError **) error {

if (!username || !serviceName) {

if (error != nil) {

*error = [NSError errorWithDomain: SFHFKeychainUtilsErrorDomain code: -2000 userInfo: nil];

}

return nil;

}

if (error != nil) {

*error = nil;

}

// Set up a query dictionary with the base query attributes: item type (generic), username, and service

NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass, kSecAttrAccount, kSecAttrService, nil] autorelease];

NSArray *objects = [[[NSArray alloc] initWithObjects: (NSString *) kSecClassGenericPassword, username, serviceName, nil] autorelease];

NSMutableDictionary *query = [[[NSMutableDictionary alloc] initWithObjects: objects forKeys: keys] autorelease];

// First do a query for attributes, in case we already have a Keychain item with no password data set.

// One likely way such an incorrect item could have come about is due to the previous (incorrect)

// version of this code (which set the password as a generic attribute instead of password data).

NSDictionary *attributeResult = NULL;

NSMutableDictionary *attributeQuery = [query mutableCopy];

[attributeQuery setObject: (id) kCFBooleanTrue forKey:(id) kSecReturnAttributes];

OSStatus status = SecItemCopyMatching((CFDictionaryRef) attributeQuery, (CFTypeRef *) &attributeResult);

[attributeResult release];

[attributeQuery release];

if (status != noErr) {

// No existing item found--simply return nil for the password

if (error != nil && status != errSecItemNotFound) {

//Only return an error if a real exception happened--not simply for "not found."

*error = [NSError errorWithDomain: SFHFKeychainUtilsErrorDomain code: status userInfo: nil];

}

return nil;

}

// We have an existing item, now query for the password data associated with it.

NSData *resultData = nil;

NSMutableDictionary *passwordQuery = [query mutableCopy];

[passwordQuery setObject: (id) kCFBooleanTrue forKey: (id) kSecReturnData];

status = SecItemCopyMatching((CFDictionaryRef) passwordQuery, (CFTypeRef *) &resultData);

[resultData autorelease];

[passwordQuery release];

if (status != noErr) {

if (status == errSecItemNotFound) {

// We found attributes for the item previously, but no password now, so return a special error.

// Users of this API will probably want to detect this error and prompt the user to

// re-enter their credentials.  When you attempt to store the re-entered credentials

// using storeUsername:andPassword:forServiceName:updateExisting:error

// the old, incorrect entry will be deleted and a new one with a properly encrypted

// password will be added.

if (error != nil) {

*error = [NSError errorWithDomain: SFHFKeychainUtilsErrorDomain code: -1999 userInfo: nil];

}

}

else {

// Something else went wrong. Simply return the normal Keychain API error code.

if (error != nil) {

*error = [NSError errorWithDomain: SFHFKeychainUtilsErrorDomain code: status userInfo: nil];

}

}

return nil;

}

NSString *password = nil;

if (resultData) {

password = [[NSString alloc] initWithData: resultData encoding: NSUTF8StringEncoding];

}

else {

// There is an existing item, but we weren't able to get password data for it for some reason,

// Possibly as a result of an item being incorrectly entered by the previous code.

// Set the -1999 error so the code above us can prompt the user again.

if (error != nil) {

*error = [NSError errorWithDomain: SFHFKeychainUtilsErrorDomain code: -1999 userInfo: nil];

}

}

return [password autorelease];

}

+ (BOOL) storeUsername: (NSString *) username andPassword: (NSString *) password forServiceName: (NSString *) serviceName updateExisting: (BOOL) updateExisting error: (NSError **) error

{

if (!username || !password || !serviceName)

{

if (error != nil)

{

*error = [NSError errorWithDomain: SFHFKeychainUtilsErrorDomain code: -2000 userInfo: nil];

}

return NO;

}

// See if we already have a password entered for these credentials.

NSError *getError = nil;

NSString *existingPassword = [SFHFKeychainUtils getPasswordForUsername: username andServiceName: serviceName error:&getError];

if ([getError code] == -1999)

{

// There is an existing entry without a password properly stored (possibly as a result of the previous incorrect version of this code.

// Delete the existing item before moving on entering a correct one.

getError = nil;

[self deleteItemForUsername: username andServiceName: serviceName error: &getError];

if ([getError code] != noErr)

{

if (error != nil)

{

*error = getError;

}

return NO;

}

}

else if ([getError code] != noErr)

{

if (error != nil)

{

*error = getError;

}

return NO;

}

if (error != nil)

{

*error = nil;

}

OSStatus status = noErr;

if (existingPassword)

{

// We have an existing, properly entered item with a password.

// Update the existing item.

if (![existingPassword isEqualToString:password] && updateExisting)

{

//Only update if we're allowed to update existing.  If not, simply do nothing.

NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass,

kSecAttrService,

kSecAttrLabel,

kSecAttrAccount,

nil] autorelease];

NSArray *objects = [[[NSArray alloc] initWithObjects: (NSString *) kSecClassGenericPassword,

serviceName,

serviceName,

username,

nil] autorelease];

NSDictionary *query = [[[NSDictionary alloc] initWithObjects: objects forKeys: keys] autorelease];

status = SecItemUpdate((CFDictionaryRef) query, (CFDictionaryRef) [NSDictionary dictionaryWithObject: [password dataUsingEncoding: NSUTF8StringEncoding] forKey: (NSString *) kSecValueData]);

}

}

else

{

// No existing entry (or an existing, improperly entered, and therefore now

// deleted, entry).  Create a new entry.

NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass,

kSecAttrService,

kSecAttrLabel,

kSecAttrAccount,

kSecValueData,

nil] autorelease];

NSArray *objects = [[[NSArray alloc] initWithObjects: (NSString *) kSecClassGenericPassword,

serviceName,

serviceName,

username,

[password dataUsingEncoding: NSUTF8StringEncoding],

nil] autorelease];

NSDictionary *query = [[[NSDictionary alloc] initWithObjects: objects forKeys: keys] autorelease];

status = SecItemAdd((CFDictionaryRef) query, NULL);

}

if (status != noErr)

{

// Something went wrong with adding the new item. Return the Keychain error code.

if (error != nil) {

*error = [NSError errorWithDomain: SFHFKeychainUtilsErrorDomain code: status userInfo: nil];

}

return NO;

}

return YES;

}

+ (BOOL) deleteItemForUsername: (NSString *) username andServiceName: (NSString *) serviceName error: (NSError **) error

{

if (!username || !serviceName)

{

if (error != nil)

{

*error = [NSError errorWithDomain: SFHFKeychainUtilsErrorDomain code: -2000 userInfo: nil];

}

return NO;

}

if (error != nil)

{

*error = nil;

}

NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass, kSecAttrAccount, kSecAttrService, kSecReturnAttributes, nil] autorelease];

NSArray *objects = [[[NSArray alloc] initWithObjects: (NSString *) kSecClassGenericPassword, username, serviceName, kCFBooleanTrue, nil] autorelease];

NSDictionary *query = [[[NSDictionary alloc] initWithObjects: objects forKeys: keys] autorelease];

OSStatus status = SecItemDelete((CFDictionaryRef) query);

if (status != noErr)

{

if (error != nil) {

*error = [NSError errorWithDomain: SFHFKeychainUtilsErrorDomain code: status userInfo: nil];

}

return NO;

}

return YES;

}

#endif

@end





最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,222评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,455评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,720评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,568评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,696评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,879评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,028评论 3 409
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,773评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,220评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,550评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,697评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,360评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,002评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,782评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,010评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,433评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,587评论 2 350

推荐阅读更多精彩内容

  • 最近搞环信聊天,需求是游客身份也可以进行聊天,当用户注册了我们的APP后也需要把游客身份切换过来进行聊天,首先我们...
    EncourageMan阅读 1,941评论 2 3
  • /**ios常见的几种加密方法: 普通的加密方法是讲密码进行加密后保存到用户偏好设置( [NSUserDefaul...
    彬至睢阳阅读 2,919评论 0 7
  • iOS开发系列--网络开发 概览 大部分应用程序都或多或少会牵扯到网络开发,例如说新浪微博、微信等,这些应用本身可...
    lichengjin阅读 3,648评论 2 7
  • 苹果官方文档地址 keychain保护 /* 设置keychain项保护等级 */ NSDictionary *q...
    GeniusLi阅读 1,110评论 0 0
  • 今天天气晴好,早上上班便从家里步行到单位,途中,小区一位邻居开电动车从我身边经过,停下来问:要带你吗?如今出入都...
    趣趣1阅读 345评论 1 3