iOS7 Programming Cookbook 第八章学习笔记

####Deleting Exiting Values in Keychain

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#import <Security/Security.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

NSString *key = @"Full Name";
//bundleID
NSString *service = [[NSBundle mainBundle] bundleIdentifier];

/*
{
acct = "Full Name";
class = genp;
svce = "com.pixolity.ios.cookbook.SecurityApp";
}
*/

NSDictionary *query = @{
(__bridge id)kSecClass : (__bridge id)kSecClassGenericPassword,
(__bridge id)kSecAttrService : service,
(__bridge id)kSecAttrAccount : key
};
//OSStatus :signed long
//SecItemCopyMatching 返回一个或多个密钥链项匹配的搜索查询,或复制属性的特定的密钥链项。
/*
__bridge
为了解决这一问题,我们使用 __bridge 关键字来实现id类型与void*类型的相互转换。看下面的例子。
id obj = [[NSObject alloc] init];
void *p = (__bridge void *)obj;
id o = (__bridge id)p;
将Objective-C的对象类型用 __bridge 转换为 void* 类型和使用 __unsafe_unretained 关键字修饰的变量是一样的。被代入对象的所有者需要明确对象生命周期的管理,不要出现异常访问的问题。
*/

OSStatus foundExisting = SecItemCopyMatching((__bridge CFDictionaryRef)query, NULL);

//errSecSuccess :No error
if (foundExisting == errSecSuccess){
OSStatus deleted = SecItemDelete((__bridge CFDictionaryRef)query);
if (deleted == errSecSuccess){
NSLog(@"成功删除项");
} else {
NSLog(@"删除失败.");
}
} else {
NSLog(@"没有找到现有值.");
}

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}

Reference

坚持原创技术分享,您的支持将鼓励我继续创作!
0%