ios 如何删除应用程序可访问的所有钥匙串项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14086085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:39:46  来源:igfitidea点击:

How to delete all keychain items accessible to an app?

objective-cios

提问by sqreept

I have stray keychain items on iOS (probably written by old version of app) that I need to delete. Is there an easy way to achieve this?

我在 iOS 上有需要删除的杂散钥匙串项目(可能由旧版本的应用程序编写)。有没有简单的方法来实现这一目标?

回答by Daij-Djan

do it for all classes

为所有课程做

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);
}

For Swift:

对于斯威夫特:

    let secItemClasses =  [kSecClassGenericPassword, kSecClassInternetPassword, kSecClassCertificate, kSecClassKey, kSecClassIdentity]
    for itemClass in secItemClasses {
        let spec: NSDictionary = [kSecClass: itemClass]
        SecItemDelete(spec)
    }

回答by Alex Sorokoletov

Xamarin iOS version (MonoTouch) of accepted answeron How to delete all keychain items accessible to an appis below:

关于如何删除应用程序可访问的所有钥匙串项目的已接受答案的Xamarin 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);
    }

If you want to make sure you indeed delete the records, you may during developmentcheck number of items in KeyChain of specific kind before and after with this code:

如果您想确保确实删除了记录,您可以在开发过程中使用以下代码检查特定类型 KeyChain 中的项目数量:

SecStatusCode scc;
var records = SecKeyChain.QueryAsRecord(new SecRecord(SecKind.GenericPassword), 1000, out scc);

回答by Jim Rhoades

I rewrote Daij-Djan's answer in Swift:

我用 Swift 重写了 Daij-Djan 的回答:

let secItemClasses = [kSecClassGenericPassword,
    kSecClassInternetPassword,
    kSecClassCertificate,
    kSecClassKey,
    kSecClassIdentity]
for secItemClass in secItemClasses {
    let dictionary = [kSecClass as String:secItemClass]
    SecItemDelete(dictionary as CFDictionary)
}

回答by ScottyBlades

Swift version

迅捷版

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)
    }
  }
}

usage:

用法:

Keychain.logout()

回答by sqreept

Thanks to Daij-Djan I got to this solution:

感谢 Daij-Djan 我得到了这个解决方案:

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);        
}

回答by AgnosticDev

You could take a look at the KeyChain Access application found in the Utilities folder. If you launch the application and click on "All Items," it should display all the items you have created with this specific computer. The developer ones usually start with com.

您可以查看 Utilities 文件夹中的 KeyChain Access 应用程序。如果您启动应用程序并单击“所有项目”,它应该会显示您使用这台特定计算机创建的所有项目。开发人员通常以 com 开头。