iOS获取UUID,并使用keychain存储

(绝对有用)iOS获取UUID,并使用keychain存储

 
 
UDID被弃用,使用UUID来做为设备的惟一标识。获取到UUID后,若是用NSUserDefaults存储,当程序被卸载后重装时,再得到的UUID和以前就不一样了。使用keychain存储能够保证程序卸载重装时,UUID不变。但当刷机或者升级系统后,UUID仍是会改变的。但这还是目前为止最佳的解决办法了,若是有更好的解决办法,欢迎留言。
(我整理的解决办法的参考来源:http://blog.k-res.net/archives/1081.html)
 

首先对于以前写的文章本身没有用真机测试,以及这段时间你们加个人QQ或者私信我也好,有的甚至找到了个人旺旺上,可是我没有给你们及时回复很抱歉。html

 

进入正题,我以后又试了下本身写的方法,发现用模拟器能够,可是真机不能够。app

真机卸载后和以前获取的uuid不一样,究其缘由就是以前生成的uuid并无被keychain成功存储。学习

 

因此问题在keychain.我以前给你们的KeychainItemWrapper类是苹果官方的,可是有些朋友反映运行时会崩溃。测试

除此以外,Code Signing Entitlements的建立方法也不够严谨。ui

 

因此,请你们从新跟我走一遍吧。url

 

 

1.新建一个工程,看一下本身的Bundle Id.这个Bundle Id 要和你用真机测试时的证书上面的Bundle Id相匹配。spa

(绝对有用)iOS获取UUID,并使用keychain存储

好比个人是 house.xianrou.xianrou.net

 

2.Target - Capabilities - Keychain Sharing - ONcode

 

(绝对有用)iOS获取UUID,并使用keychain存储

 

 
(绝对有用)iOS获取UUID,并使用keychain存储

这步主要目的是打开Keychain Sharing,将它由灰色状态的OFF改成蓝色状态的ON。orm

打开以后的变化以下:

 

(绝对有用)iOS获取UUID,并使用keychain存储

 

 

(绝对有用)iOS获取UUID,并使用keychain存储

左侧的目录会自动生成Entitlements文件,不须要本身建立了。

 

也就是说,Bundle Identifier、Keychain Sharing的Keychain Groups、Entitlements文件的Keychain Access Groups的第一个元素,它们要保持上图所示的一致性。

设置好了之后能够运行下程序,没问题能够进行下一步。

 

3.传说中的uuid类和keychain类来啦

既然苹果的keychain方法会崩溃并且有些复杂,咱们只保存一个uuid的话能够用下面的简单方法:

(这也是我本身百度的keychain拷贝别人的,而后改改)

UUID.h

#import  尖括号(Foundation/Foundation.h)

 

@interface UUID : NSObject

 

+(NSString *)getUUID;

 

 

@end

 

UUID.m

 

#import "UUID.h"

#import "KeyChainStore.h"

 

 

@implementation UUID

 

+(NSString *)getUUID

{

    NSString * strUUID = (NSString *)[KeyChainStore load:@"com.company.app.usernamepassword"];

    

    //首次执行该方法时,uuid为空

    if ([strUUID isEqualToString:@""] || !strUUID)

    {

        //生成一个uuid的方法

        CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);

        

        strUUID = (NSString *)CFBridgingRelease(CFUUIDCreateString (kCFAllocatorDefault,uuidRef));

   

        //将该uuid保存到keychain

        [KeyChainStore save:KEY_USERNAME_PASSWORD data:strUUID];

        

    }

    return strUUID;

}

 

@end

 

KeyChainStore.h

 

#import 尖括号(Foundation/Foundation.h)

 

@interface KeyChainStore : NSObject

 

+ (void)save:(NSString *)service data:(id)data;

+ (id)load:(NSString *)service;

+ (void)deleteKeyData:(NSString *)service;

 

@end

 

KeyChainStore.m

#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];

}

 

+ (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

 
将这两个类添加到工程中
 
4.新建一个pch文件,而后pch文件的内容以下:

 

#ifndef PrefixHeader_pch

#define PrefixHeader_pch

 

#define  KEY_USERNAME_PASSWORD @"com.company.app.usernamepassword"

#define  KEY_USERNAME @"com.company.app.username"

#define  KEY_PASSWORD @"com.company.app.password"

 

#endif

 
pch文件的建立方法可参考:http://blog.csdn.net/huang2009303513/article/details/40375235
有可能会在填Prefix Header 即pch文件的路径那里报错,最近又学习到一种更好的方式$(SRCROOT)/$(PROJECT_NAME)/PrefixHeader.pch,其中$(PROJECT_NAME)是相对工程名,比上面的方法更便捷.

 

5.在viewcontroller.m里面执行以下代码

  NSString * uuid= [UUID getUUID];

  NSLog(@"uuid=%@",uuid);

  获得的uuid相似于这种
   uuid=19AAB430-9CB8-4325-ACC5-D7D386B68960
  
而后卸载掉,再从新运行,看先后获得的uuid是否是同样吧!

本文来源于:森女钟溪妍的博客

相关文章
相关标签/搜索