Created
June 14, 2015 12:28
-
-
Save chrishulbert/460c09a07b3f225a960e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import Security | |
struct SecItemWrapper { | |
static func matching(query: NSDictionary) -> (status: Status, result: AnyObject?) { | |
var rawResult: Unmanaged<AnyObject>? | |
let rawStatus = SecItemCopyMatching(query, &rawResult) | |
let result: AnyObject? = rawResult?.takeRetainedValue() | |
let status = Status.fromOSStatus(rawStatus) | |
return (status, result) | |
} | |
static func add(attributes: NSDictionary) -> (status: Status, result: AnyObject?) { | |
var rawResult: Unmanaged<AnyObject>? | |
let rawStatus = SecItemAdd(attributes, &rawResult) | |
let result: AnyObject? = rawResult?.takeRetainedValue() | |
let status = Status.fromOSStatus(rawStatus) | |
return (status, result) | |
} | |
static func update(query: NSDictionary, attributesToUpdate: NSDictionary) -> Status { | |
let rawStatus = SecItemUpdate(query, attributesToUpdate) | |
return Status.fromOSStatus(rawStatus) | |
} | |
static func delete(query: NSDictionary) -> Status { | |
let rawStatus = SecItemDelete(query) | |
return Status.fromOSStatus(rawStatus) | |
} | |
enum Status: Int32 { | |
// Swift won't allow you to specify a non-literal value below, hence the literal numbers to match errSec*. | |
case Success = 0 // errSecSuccess | |
case Unimplemented = -4 // errSecUnimplemented | |
case Param = -50 // errSecParam | |
case Allocate = -108 // errSecAllocate | |
case NotAvailable = -25291 // errSecNotAvailable | |
case AuthFailed = -25293 // errSecAuthFailed | |
case DuplicateItem = -25299 // errSecDuplicateItem | |
case ItemNotFound = -25300 // errSecItemNotFound | |
case InteractionNotAllowed = -25308 // errSecInteractionNotAllowed | |
case Decode = -26275 // errSecDecode | |
case Other = -30000 | |
static func fromOSStatus(rawStatus: OSStatus) -> Status { | |
return Status(rawValue: rawStatus) ?? .Other | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment