我能够存储和检索凭据,但我无法删除它们。我在这里创建了一个简单的包装器,但是clear方法不起作用。在我打电话给清楚后,凭证似乎仍然存在。我需要做些什么才能清除凭证?
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)
}
}
}
答案 0 :(得分:0)
获胜:
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)
}
答案 1 :(得分:0)
要删除可同步的凭据,您必须在删除功能中提供一个选项。
def repeat(w,n):
skip = max(range(len(w)),key=lambda i:i*(w[:i]==w[-i:]))
return w + (n-1)*w[skip:]
这删除了我没有删除的凭据。