Adding item to keychain using Swift

前端 未结 7 1959
栀梦
栀梦 2020-12-28 08:45

I\'m trying to add an item to the iOS keychain using Swift but can\'t figure out how to type cast properly. From WWDC 2013 session 709, given the following Objective-C code:

7条回答
  •  情深已故
    2020-12-28 09:17

    Swift 3

    let kSecClassKey = String(kSecClass)
    let kSecAttrAccountKey = String(kSecAttrAccount)
    let kSecValueDataKey = String(kSecValueData)
    let kSecClassGenericPasswordKey = String(kSecClassGenericPassword)
    let kSecAttrServiceKey = String(kSecAttrService)
    let kSecMatchLimitKey = String(kSecMatchLimit)
    let kSecReturnDataKey = String(kSecReturnData)
    let kSecMatchLimitOneKey = String(kSecMatchLimitOne)
    

    you can also do it inside the dictionary itself alá:

    var query: [String: Any] = [
        String(kSecClass): kSecClassGenericPassword,
        String(kSecAttrService): "MyService",
        String(kSecAttrAccount): "Some account",
        String(kSecValueData): secret
    ]
    

    however, this is more expensive for the compiler, even more so since you're probably using the query in multiple places.

提交回复
热议问题