要运行示例项目,克隆仓库,然后首先从 Example 目录运行 pod install
。示例项目仅在 jnViewController 的 viewDidLoad 方法中包含一些关于保存、加载和删除值的示例调用。
NSString *testKey = @"myTestKey";
NSString *testValue = @"myTestValue";
if ([JNKeychain saveValue:testValue forKey:testKey]) {
NSLog(@"Correctly saved value '%@' for key '%@'", testValue, testKey);
} else {
NSLog(@"Failed to save!");
}
NSLog(@"Value for key '%@' is: '%@'", testKey, [JNKeychain loadValueForKey:testKey]);
if ([JNKeychain deleteValueForKey:testKey]) {
NSLog(@"Deleted value for key '%@'. Value is: %@", testKey, [JNKeychain loadValueForKey:testKey]);
} else {
NSLog(@"Failed to delete!");
}
为了在不同的应用程序之间共享密钥链数据,请启用项目功能中的键链共享访问组,将 Security.framework 添加到链接框架和库中,并使用期望访问组的方 USERNAME 法。
NSString *accessGroup = @"demo.JNKeychain.shared";
if ([JNKeychain saveValue:testValue forKey:testKey forAccessGroup:accessGroup]) {
NSLog(@"Correctly saved value '%@' for key '%@'", testValue, testKey);
} else {
NSLog(@"Failed to save!");
}
NSLog(@"Value for key '%@' is: '%@'", testKey, [JNKeychain loadValueForKey:testKey forAccessGroup:accessGroup]);
if ([JNKeychain deleteValueForKey:testKey forAccessGroup:accessGroup]) {
NSLog(@"Deleted value for key '%@'. Value is: %@", testKey, [JNKeychain loadValueForKey:testKey forAccessGroup:accessGroup]);
} else {
NSLog(@"Failed to delete!");
}
如果您不想使用 Cocoapods,只需将 JNKeychain.h 和 JNKeychain.m 文件抓取出来,并将其放置在您的项目中某个位置。
JNKeychain 使用 ARC。
Jeremias Nunez, [email protected] - @jereahrequesi
JNKeychain 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。