如何清除SFSafariViewController的凭据?

7

我正在使用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并重新启动它。

问题:如何以编程方式清除凭据?

1个回答

14

所以我在苹果公司提出了一个“技术支持事件”来解决这个问题。以下是他们的回答概述:SFSafariViewController在我的应用程序进程之外运行,为了保证安全性,我的应用程序无法修改SFSafariViewController的状态。换句话说,我的应用程序无法清除由SFSafariViewController存储的凭据。


1
感谢分享。我正需要完全相同的东西。 - smat88dd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接