Răsfoiți Sursa

Add contains() without authentication API

Kishikawa Katsumi 5 ani în urmă
părinte
comite
a7edf7a124
1 a modificat fișierele cu 15 adăugiri și 5 ștergeri
  1. 15 5
      Lib/KeychainAccess/Keychain.swift

+ 15 - 5
Lib/KeychainAccess/Keychain.swift

@@ -741,17 +741,27 @@ public final class Keychain {
 
     // MARK:
 
-    public func contains(_ key: String) throws -> Bool {
+    public func contains(_ key: String, withoutAuthenticationUI: Bool = false) throws -> Bool {
         var query = options.query()
         query[AttributeAccount] = key
-        if #available(iOS 9.0, OSX 10.11, *) {
-            query[UseAuthenticationUI] = UseAuthenticationUIFail
+
+        if withoutAuthenticationUI {
+            if #available(iOS 9.0, *) {
+                query[UseAuthenticationUI] = UseAuthenticationUIFail
+            } else {
+                query[UseNoAuthenticationUI] = kCFBooleanTrue
+            }
         }
         
         let status = SecItemCopyMatching(query as CFDictionary, nil)
         switch status {
-        case errSecSuccess, errSecInteractionNotAllowed:
-            return true
+        case errSecSuccess:
+                return true
+        case errSecInteractionNotAllowed:
+            if withoutAuthenticationUI {
+                return true
+            }
+            return false
         case errSecItemNotFound:
             return false
         default: