Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added containsAnymethod #493

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions Lib/KeychainAccess/Keychain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -835,9 +835,9 @@ public final class Keychain {

// MARK:

public func contains(_ key: String, withoutAuthenticationUI: Bool = false) throws -> Bool {
private func contains(query queryAttributes: [String:Any], withoutAuthenticationUI: Bool = false) throws -> Bool {
var query = options.query()
query[AttributeAccount] = key
queryAttributes.forEach { query.updateValue($1, forKey: $0) }

if withoutAuthenticationUI {
#if os(iOS) || os(watchOS) || os(tvOS)
Expand Down Expand Up @@ -884,7 +884,15 @@ public final class Keychain {
throw securityError(status: status)
}
}


public func contains(_ key: String, withoutAuthenticationUI: Bool = false) throws -> Bool {
return try self.contains(query:[AttributeAccount:key])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nd-net you haven't passed withoutAuthenticationUI into base function

}

public func containsAny(withoutAuthenticationUI: Bool = false) throws -> Bool {
return try self.contains(query:[:])
}

// MARK:

public class func allKeys(_ itemClass: ItemClass) -> [(String, String)] {
Expand Down
9 changes: 9 additions & 0 deletions Lib/KeychainAccessTests/KeychainAccessTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,15 @@ class KeychainAccessTests: XCTestCase {
XCTAssertTrue(try! keychain.contains("username"), "stored username")
XCTAssertTrue(try! keychain.contains("password"), "stored password")
}

func testContainsAny() {
let keychain = Keychain(service: "Twitter")

XCTAssertFalse(try! keychain.containsAny(), "not stored username")

do { try keychain.set("kishikawakatsumi", key: "username") } catch {}
XCTAssertTrue(try! keychain.containsAny(), "stored username")
}

// MARK:

Expand Down