我在iOS上有一些杂项钥匙串项目(可能是由旧版本的应用程序编写),我需要将其删除。 有没有简单的方法来实现这个?
我在iOS上有一些杂项钥匙串项目(可能是由旧版本的应用程序编写),我需要将其删除。 有没有简单的方法来实现这个?
为所有类执行此操作
Objective-C:
NSArray *secItemClasses = @[(__bridge id)kSecClassGenericPassword,
(__bridge id)kSecClassInternetPassword,
(__bridge id)kSecClassCertificate,
(__bridge id)kSecClassKey,
(__bridge id)kSecClassIdentity];
for (id secItemClass in secItemClasses) {
NSDictionary *spec = @{(__bridge id)kSecClass: secItemClass};
SecItemDelete((__bridge CFDictionaryRef)spec);
}
迅捷:
[kSecClassGenericPassword, kSecClassInternetPassword, kSecClassCertificate, kSecClassKey, kSecClassIdentity].forEach {
let status = SecItemDelete([
kSecClass: $0,
kSecAttrSynchronizable: kSecAttrSynchronizableAny
] as CFDictionary)
if status != errSecSuccess && status != errSecItemNotFound {
//Error while removing class $0
}
}
NSDictionary *spec = @{(__bridge id)kSecClass: secItemClass};
。 - kevbohXamarin iOS版本(MonoTouch)接受的答案关于如何删除应用程序可以访问的所有钥匙串项目如下:
foreach (var recordKind in new []{
SecKind.GenericPassword,
SecKind.Certificate,
SecKind.Identity,
SecKind.InternetPassword,
SecKind.Key,
})
{
SecRecord query = new SecRecord(recordKind);
SecKeyChain.Remove(query);
}
如果您想确保您确实删除了记录,您可以在开发过程中使用以下代码检查特定类型的KeyChain中的项目数量(删除前后):during developmentSecStatusCode scc;
var records = SecKeyChain.QueryAsRecord(new SecRecord(SecKind.GenericPassword), 1000, out scc);
我用Swift重写了Daij-Djan的答案:
let secItemClasses = [kSecClassGenericPassword,
kSecClassInternetPassword,
kSecClassCertificate,
kSecClassKey,
kSecClassIdentity]
for secItemClass in secItemClasses {
let dictionary = [kSecClass as String:secItemClass]
SecItemDelete(dictionary as CFDictionary)
}
public var hashValue: Int {return Int(bitPattern:CFHash(self))}
以确保不会发生溢出。 - Brettlet dictionary = [kSecClass as String : secItemClass as String]
。 - BrandonSwift版本
import Foundation
import Security
public class Keychain: NSObject {
public class func logout() {
let secItemClasses = [
kSecClassGenericPassword,
kSecClassInternetPassword,
kSecClassCertificate,
kSecClassKey,
kSecClassIdentity,
]
for itemClass in secItemClasses {
let spec: NSDictionary = [kSecClass: itemClass]
SecItemDelete(spec)
}
}
}
用法:
Keychain.logout()
for (id secclass in @[
(__bridge id)kSecClassGenericPassword,
(__bridge id)kSecClassInternetPassword,
(__bridge id)kSecClassCertificate,
(__bridge id)kSecClassKey,
(__bridge id)kSecClassIdentity]) {
NSMutableDictionary *query = [NSMutableDictionary dictionaryWithObjectsAndKeys:
secclass, (__bridge id)kSecClass,
nil];
SecItemDelete((__bridge CFDictionaryRef)query);
}
kSecAttrSynchronizable
标志的密钥链条目(通过iCloud允许与其他设备同步)。kSecAttrSynchronizable: kSecAttrSynchronizableAny
:let secItemClasses = [kSecClassGenericPassword,
kSecClassInternetPassword,
kSecClassCertificate,
kSecClassKey,
kSecClassIdentity]
for secItemClass in secItemClasses {
let query: NSDictionary = [
kSecClass as String: secItemClass,
kSecAttrSynchronizable as String: kSecAttrSynchronizableAny
]
SecItemDelete(query)
}
在使用 Mac 作为目标进行单元测试时,谨慎使用 kSecClassCertificate
来清除所有内容,因为这将清除您的开发证书。您可以简单地撤销现有证书,但在 CI/CD 管道中,这种方法行不通。