我在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并重新打开电源。
问题:如何以编程方式清除凭据?
因此,我为此开了一个苹果“技术支持事件”。以下是其答案的摘要:SFSafariViewController在我的App进程之外运行,并且为了安全起见,我的App无法修改SFSafariViewController的状态。换句话说,我的应用程序无法清除SFSafariViewController存储的凭据。