ksnowlv

回顾过去,总结以往;立足现在,铭记当下;技术为主,笔记而已.

iOS之Keychain

| Comments

iOS之Keychain用途:提供了一种安全的保存私密信息的方式,每个ios程序都有一个独立的keychain存储,它保存的信息不会因App被删除而丢失,也可用于跨App共享keychain信息。见苹果开发文档Keychain Services Programming Guide

Keychain Services Reference关键的方法如下

  • SecItemCopyMatching
  • SecItemAdd
  • SecItemUpdate
  • SecItemDelete

苹果官方提供了的demo程序GenericKeychain,使用苹果封装的KeychainItemWrapper.hKeychainItemWrapper.m提供的接口即可。

代码示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    KeychainItemWrapper *uuid = [[KeychainItemWrapper alloc]
                                         initWithIdentifier:@"UUID"
                                         accessGroup:@"com.taobao.userinfo"];
    NSString *strUUID = [uuid objectForKey:(__bridge id)kSecValueData];

    if (strUUID.length == 0 )
    {
        CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
        strUUID =  CFBridgingRelease(CFUUIDCreateString (kCFAllocatorDefault,uuidRef));

        [uuid setObject:strUUID forKey:(__bridge id)kSecValueData];

        NSLog(@"create uuid");
        CFRelease(uuidRef);
    }

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

可参考源代码,该工程中的KeychainItemWrapper.hKeychainItemWrapper.m是支持ARC的。

Comments

comments powered by Disqus
Included file 'custom/after_footer.html' not found in _includes directory