我可以存储和检索凭据,但无法删除它们。我在这里创建了一个简单的包装器,但清除方法不起作用。在我打电话清除后,凭证似乎仍然存在。我需要做什么来清除凭据?
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)
}
}
}
最佳答案
为了胜利:
func clear(for userID: String) {
guard let creds = URLCredentialStorage.shared.credentials(for: protectionSpace) else { return }
guard let cred = creds[userID] else { return }
URLCredentialStorage.shared.remove(cred, for: protectionSpace)
}
关于ios - 如何从 URLCredentialStorage 中删除凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49827490/