passforios/passKit/Helpers/Utils.swift

64 lines
2.6 KiB
Swift
Raw Normal View History

2017-02-08 19:27:57 +08:00
//
// Utils.swift
// pass
//
// Created by Mingshen Sun on 8/2/2017.
// Copyright © 2017 Bob Sun. All rights reserved.
//
import Foundation
import SwiftyUserDefaults
import KeychainAccess
2017-02-08 19:27:57 +08:00
public class Utils {
public static func getPasswordFromKeychain(name: String) -> String? {
let keychain = Keychain(service: Globals.bundleIdentifier, accessGroup: Globals.groupIdentifier)
2019-05-01 17:49:27 +02:00
return try? keychain.getString(name)
}
2018-12-09 16:59:07 -08:00
public static func addPasswordToKeychain(name: String, password: String?) {
let keychain = Keychain(service: Globals.bundleIdentifier, accessGroup: Globals.groupIdentifier)
keychain[name] = password
}
2018-12-09 16:59:07 -08:00
public static func removeKeychain(name: String) {
let keychain = Keychain(service: Globals.bundleIdentifier, accessGroup: Globals.groupIdentifier)
2018-11-17 21:41:28 -08:00
try? keychain.remove(name)
}
2018-12-09 16:59:07 -08:00
public static func removeAllKeychain() {
let keychain = Keychain(service: Globals.bundleIdentifier, accessGroup: Globals.groupIdentifier)
2018-11-17 21:41:28 -08:00
try? keychain.removeAll()
}
public static func copyToPasteboard(textToCopy: String?) {
2017-02-23 17:56:12 +08:00
guard textToCopy != nil else {
return
}
UIPasteboard.general.string = textToCopy
}
public static func attributedPassword(plainPassword: String) -> NSAttributedString{
2017-02-26 00:58:18 +08:00
let attributedPassword = NSMutableAttributedString.init(string: plainPassword)
// draw all digits in the password into red
// draw all punctuation characters in the password into blue
for (index, element) in plainPassword.unicodeScalars.enumerated() {
2017-07-07 11:32:03 +08:00
var charColor = UIColor.darkText
2017-02-26 00:58:18 +08:00
if NSCharacterSet.decimalDigits.contains(element) {
charColor = Globals.digitColor
} else if !NSCharacterSet.letters.contains(element) {
2017-10-07 23:05:26 -07:00
charColor = Globals.symbolColor
} else {
charColor = Globals.letterColor
2017-02-26 00:58:18 +08:00
}
2019-05-01 17:49:27 +02:00
attributedPassword.addAttribute(NSAttributedString.Key.foregroundColor, value: charColor, range: NSRange(location: index, length: 1))
2017-02-26 00:58:18 +08:00
}
return attributedPassword
}
2018-12-09 16:59:07 -08:00
2018-09-23 22:16:13 +08:00
public static func alert(title: String, message: String, controller: UIViewController, handler: ((UIAlertAction) -> Void)? = nil, completion: (() -> Void)? = nil) {
2019-05-01 17:49:27 +02:00
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Ok".localize(), style: UIAlertAction.Style.default, handler: handler))
2018-09-23 22:16:13 +08:00
controller.present(alert, animated: true, completion: completion)
}
2017-02-08 19:27:57 +08:00
}