-
Notifications
You must be signed in to change notification settings - Fork 1
/
Keychain.swift
47 lines (39 loc) · 1.35 KB
/
Keychain.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//
// Keychain.swift
// ChungChul_iOS
//
// Created by ParkSungJoon on 19/11/2018.
// Copyright © 2018 Park Sung Joon. All rights reserved.
//
import UIKit
import Security
class Keychain {
// Save at the Keychain
class func save(key: String, keystoreData: Data) -> Bool {
let query = [
kSecClass as String : kSecClassGenericPassword as String,
kSecAttrAccount as String : key,
kSecValueData as String : keystoreData
] as [String : Any]
SecItemDelete(query as CFDictionary)
let status: OSStatus = SecItemAdd(query as CFDictionary, nil)
return status == noErr
}
// Load at the Keychain
class func load(key: String) -> Data? {
let query = [
kSecClass as String : kSecClassGenericPassword,
kSecAttrAccount as String : key,
kSecReturnData as String : kCFBooleanTrue,
kSecMatchLimit as String : kSecMatchLimitOne
] as [String : Any]
var dataTypeRef: AnyObject?
let status = withUnsafeMutablePointer(to: &dataTypeRef) { (SecItemCopyMatching(query as CFDictionary, UnsafeMutablePointer($0))) }
if status == errSecSuccess {
if let data = dataTypeRef as! Data? {
return data
}
}
return nil
}
}