passforios/passKit/Helpers/Utils.swift
Bob Sun 653ce515ab
Delete the removeFileIfExists funtion
I have no idea why I did write this helper function at the begining.
2018-11-16 22:49:41 -08:00

81 lines
3 KiB
Swift

//
// Utils.swift
// pass
//
// Created by Mingshen Sun on 8/2/2017.
// Copyright © 2017 Bob Sun. All rights reserved.
//
import Foundation
import SwiftyUserDefaults
import KeychainAccess
public class Utils {
public static func getPasswordFromKeychain(name: String) -> String? {
let keychain = Keychain(service: Globals.bundleIdentifier, accessGroup: Globals.groupIdentifier)
do {
return try keychain.getString(name)
} catch {
print(error)
}
return nil
}
public static func addPasswordToKeychain(name: String, password: String?) {
let keychain = Keychain(service: Globals.bundleIdentifier, accessGroup: Globals.groupIdentifier)
keychain[name] = password
}
public static func removeKeychain(name: String) {
let keychain = Keychain(service: Globals.bundleIdentifier, accessGroup: Globals.groupIdentifier)
do {
try keychain.remove(name)
} catch {
print(error)
}
}
public static func removeAllKeychain() {
let keychain = Keychain(service: Globals.bundleIdentifier, accessGroup: Globals.groupIdentifier)
do {
try keychain.removeAll()
} catch {
print(error)
}
}
public static func copyToPasteboard(textToCopy: String?) {
guard textToCopy != nil else {
return
}
UIPasteboard.general.string = textToCopy
}
public static func attributedPassword(plainPassword: String) -> NSAttributedString{
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() {
var charColor = UIColor.darkText
if NSCharacterSet.decimalDigits.contains(element) {
charColor = Globals.digitColor
} else if !NSCharacterSet.letters.contains(element) {
charColor = Globals.symbolColor
} else {
charColor = Globals.letterColor
}
attributedPassword.addAttribute(NSAttributedStringKey.foregroundColor, value: charColor, range: NSRange(location: index, length: 1))
}
return attributedPassword
}
public static func initDefaultKeys() {
if SharedDefaults[.passwordGeneratorFlavor] == "" {
SharedDefaults[.passwordGeneratorFlavor] = "Random"
}
}
public static func alert(title: String, message: String, controller: UIViewController, handler: ((UIAlertAction) -> Void)? = nil, completion: (() -> Void)? = nil) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: handler))
controller.present(alert, animated: true, completion: completion)
}
}