ios 开发笔记---UIKeyChainStore使用

前言

之前虽然经常有在做笔记的习惯,但是往往写的很零散不便于记忆和回顾,所以想要通过写文章的方式将之前的笔记做一个整理。

第三方框架UIKeyChainStore使用

1.使用场景

在app开发过程中通常会涉及到敏感信息的保存,ios给我们提供了keychain来将数据保存到钥匙串中(例如手游,当把游戏卸载后,我们再次安装的时候我们希望不用输入用户名和密码就能直接登录的时候就可以用到UICKeyChainStore里面的存储方法来进行密码保存)

2.使用前准备

1.pod 导入UICKeyChainStore库 (或者将UICKeyChainStore.h 和UICKeyChainStore.m导入工程)

2.导入 Security.framework库

3.基本用法

1.存储

UICKeyChainStore *keychain = [UICKeyChainStore keyChainStoreWithService:@"通常是bundleid"];

keychain[@"password"] =  @"xxxxxxxxx";

2.取值

UICKeyChainStore *keychain = [UICKeyChainStore keyChainStoreWithService:@"通常是bundleid"];

NSString *password =  [keychainStore stringForKey:@"password"];

4.下载地址

UICKeyChainStore  地址:https://github.com/kishikawakatsumi/UICKeyChainStore

5.多个App之间共享同一个设备唯一标识符

一次性创建CFUUID+持久性存储(用到UIKeyChainStore)

1.设备唯一标识符UUID (ios 6 之后苹果不再提供通过uniqueIdentifier去获取设备的UUID,因此这里我们通过CFUUID作为设备唯一标识符)

FCUUID提供的类方法(使用条件多个App必须是同一个证书发布的)https://github.com/fabiocaccamo/FCUUID

//changes each time (no persistent)(每次运行都会变化)

+(NSString*)uuid;

//changes each time (no persistent), but allows to keep in memory more temporary uuids()

+(NSString*)uuidForKey:(id)key;

//changes each time the app gets launched (persistent to session)每次运行应用都会变

+(NSString*)uuidForSession;

//changes each time the app gets installed (persistent to installation)每次重新安装的时候会变化

+(NSString*)uuidForInstallation;

//changes each time all the apps of the same vendor are uninstalled (this works exactly as identifierForVendor)(卸载重装后会变化)

+(NSString*)uuidForVendor;

//changes only on system reset, this is the best replacement to the good old udid (persistent to device)+(抹掉iphone后会变化适合作为设备唯一标识符)

(NSString*)uuidForDevice;

如何开启keychain sharing(钥匙串共享)

>点击capabilities-->打开keychain sharing-->Xcode会自动帮你生成entitlements文件-->分别在两个不同的项目中的keychain sharing中添加所有App的(开发者id + bundle identifier)->大功告成。通过真机测试,证实该方案可行。


ios 开发笔记---UIKeyChainStore使用_第1张图片

你可能感兴趣的:(ios 开发笔记---UIKeyChainStore使用)