iOS7 Programming Cookbook 第八章学习笔记 发表于 2014-05-27 | 分类于 NSBundle , Security , GCD , __bridge | 阅读次数: 字数统计: 323 字 | 阅读时长 ≈ 2 分钟 ####Deleting Exiting Values in Keychain 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849#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 O’Reilly Shop iOS 7 Programming Cookbook 坚持原创技术分享,您的支持将鼓励我继续创作! 打赏 支付宝