Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class PasswordManager {
- static let shared = PasswordManager() // singleton instance
- private lazy var protectionSpace: URLProtectionSpace = {
- return URLProtectionSpace(host: "somehost.com",
- port: 0,
- protocol: "http",
- realm: nil,
- authenticationMethod: nil)
- }()
- private init() { }
- func password(for userID: String) -> String? {
- guard let credentials = URLCredentialStorage.shared.credentials(for: protectionSpace) else { return nil }
- return credentials[userID]?.password
- }
- func set(password: String, for userID: String) {
- let credential = URLCredential(user: userID, password: password, persistence: .permanent)
- URLCredentialStorage.shared.set(credential, for: protectionSpace)
- }
- func clear(for userID: String) {
- if let password = password(for: userID) {
- let credential = URLCredential(user: userID, password: password, persistence: .permanent)
- URLCredentialStorage.shared.remove(credential, for: protectionSpace)
- }
- }
- }
Add Comment
Please, Sign In to add comment