我正在使用Swift 2中的SFSafariViewController在iPad Air 2上显示网页,iOS版本为9.1(13B143)。每个网页都需要用户提供凭证。然而,当用户按下注销按钮时,我需要清除这些凭证。我尝试使用以下代码:
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()
但它不起作用。实际上,“删除凭据”和“删除Cookie”从未被打印。此外,即使我将应用程序完全从iPad上删除并重新安装它,当我返回到Web URL时,凭据仍然被缓存。我发现清除凭据的唯一方法是关闭iPad并重新启动它。
问题:如何以编程方式清除凭据?