Skip to content

Instantly share code, notes, and snippets.

@kgleong
Created February 9, 2018 21:53
Show Gist options
  • Save kgleong/d948c4a53ff7e81795228dee22b40ff2 to your computer and use it in GitHub Desktop.
Save kgleong/d948c4a53ff7e81795228dee22b40ff2 to your computer and use it in GitHub Desktop.
Reproducing the Error
/** Create a Data object **/
let dataBlob = "foo".data(using: .utf8)

let existingEntryData: [String: Any] = [
    kSecAttrService as String!: Bundle.main.bundleIdentifier as Any,
    // Use Data object for kSecAttrAccount, which expects a String
    kSecAttrAccount as String!: dataBlob as Any, 
    kSecClass as String!: kSecClassGenericPassword as Any,
    kSecValueData as String!: dataBlob as Any
]

// Insert the non-conforming data into the keychain
let status = SecItemAdd(existingEntryData as CFDictionary, nil)
print(String(describing: status)) // prints “0” indicating a successful insert

let valet = VALValet(
    identifier: Bundle.main.bundleIdentifier!, accessibility: .whenUnlocked
)

let query: [String: Any] = [
    kSecAttrService as String!: Bundle.main.bundleIdentifier as Any,
    kSecClass as String!: kSecClassGenericPassword as Any
]

/**
  Attempt to migrate the existing non-conforming entry so Valet can manage it.
  
  #### THIS THROWS THE EXPECTED EXCEPTION!!! ####
  */
valet?.migrateObjects(matchingQuery: query, removeOnCompletion: true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment