Put Keychain related methods to separate class
This commit is contained in:
parent
e4e4c6daff
commit
441a7f1e9b
5 changed files with 76 additions and 52 deletions
40
passKit/Helpers/AppKeychain.swift
Normal file
40
passKit/Helpers/AppKeychain.swift
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
//
|
||||
// AppKeychain.swift
|
||||
// passKit
|
||||
//
|
||||
// Created by Danny Moesch on 25.06.19.
|
||||
// Copyright © 2019 Bob Sun. All rights reserved.
|
||||
//
|
||||
|
||||
import KeychainAccess
|
||||
|
||||
public class AppKeychain {
|
||||
|
||||
private static let keychain = Keychain(service: Globals.bundleIdentifier, accessGroup: Globals.groupIdentifier)
|
||||
.accessibility(.whenUnlockedThisDeviceOnly)
|
||||
.synchronizable(false)
|
||||
|
||||
public static func add(data: Data, for key: String) {
|
||||
keychain[data: key] = data
|
||||
}
|
||||
|
||||
public static func add(string: String, for key: String) {
|
||||
keychain[key] = string
|
||||
}
|
||||
|
||||
public static func get(for key: String) -> Data? {
|
||||
return try? keychain.getData(key)
|
||||
}
|
||||
|
||||
public static func get(for key: String) -> String? {
|
||||
return try? keychain.getString(key)
|
||||
}
|
||||
|
||||
public static func removeContent(for key: String) {
|
||||
try? keychain.remove(key)
|
||||
}
|
||||
|
||||
public static func removeAllContent() {
|
||||
try? keychain.removeAll()
|
||||
}
|
||||
}
|
||||
|
|
@ -6,40 +6,8 @@
|
|||
// Copyright © 2017 Bob Sun. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftyUserDefaults
|
||||
import KeychainAccess
|
||||
|
||||
public class Utils {
|
||||
|
||||
private static let keychain = Keychain(service: Globals.bundleIdentifier, accessGroup: Globals.groupIdentifier)
|
||||
.accessibility(.whenUnlockedThisDeviceOnly)
|
||||
.synchronizable(false)
|
||||
|
||||
public static func getPasswordFromKeychain(name: String) -> String? {
|
||||
return try? keychain.getString(name)
|
||||
}
|
||||
|
||||
public static func addPasswordToKeychain(name: String, password: String?) {
|
||||
keychain[name] = password
|
||||
}
|
||||
|
||||
public static func removeKeychain(name: String) {
|
||||
try? keychain.remove(name)
|
||||
}
|
||||
|
||||
public static func removeAllKeychain() {
|
||||
try? keychain.removeAll()
|
||||
}
|
||||
|
||||
public static func addDataToKeychain(key: String, data: Data) {
|
||||
keychain[data: key] = data
|
||||
}
|
||||
|
||||
public static func getDataFromKeychain(for key: String) -> Data? {
|
||||
return try? keychain.getData(key)
|
||||
}
|
||||
|
||||
public static func copyToPasteboard(textToCopy: String?) {
|
||||
guard textToCopy != nil else {
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue