我在 Swift 2 中使用 SFSafariViewController 在带有 ios 9.1 (13B143) 的 iPad Air 2 上显示网页。每个网页都需要用户提供凭据。但是,当用户按下注销按钮时,我需要清除这些凭据。我尝试过使用以下内容:
let allCreds = NSURLCredentialStorage.sharedCredentialStorage().allCredentials
for (protectionSpace, userCredsDict) in allCreds {
for (_, cred) in userCredsDict {
print("DELETING CREDENTIAL")
NSURLCredentialStorage.sharedCredentialStorage().removeCredential(cred, forProtectionSpace: protectionSpace, options: ["NSURLCredentialStorageRemoveSynchronizableCredentials" : true])
}
}
// Clear cookies
if let cookies = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies {
for (cookie) in cookies {
print("DELETING COOKIE")
NSHTTPCookieStorage.sharedHTTPCookieStorage().deleteCookie(cookie)
}
}
// Clear history
print("CLEARING URL HISTORY")
NSURLCache.sharedURLCache().removeAllCachedResponses()
NSUserDefaults.standardUserDefaults().synchronize()
但它不起作用。事实上,“DELETING CREDENTIAL”和“DELETING COOKIE”永远不会被打印出来。此外,我可以从 iPad 上完全删除该应用程序并重新安装它,当我导航回 Web url 时,凭据仍会被缓存。我发现清除凭据的唯一方法是关闭 iPad 并重新打开电源。
问题:如何以编程方式清除凭据?