How to clear SFSafariViewController credentials?

2019-01-23 21:45发布

问题:

I'm using SFSafariViewController in Swift 2 to display web pages on an iPad Air 2 with ios 9.1 (13B143). Each of the web pages requires credentials from the user. However, when the user presses a logout button, I need to clear those credentials. I've tried using the following:

  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()

but it doesn't work. In fact, "DELETING CREDENTIAL" and "DELETING COOKIE" never gets printed. Moreover, I can entirely delete the app off of the iPad and reinstall it and the credentials are still cached when I navigate back to the web url. The only way I have found to clear the credentials is to shut down the iPad and power it back on.

Question: How can I programmatically clear the credentials?

回答1:

So I opened an Apple 'Technical Support Incident' on this. The following is summary of their answer: SFSafariViewController runs outside of my App's process and in order to be secure my App can not modify SFSafariViewController's state. In other words, my App can not clear credentials stored by SFSafariViewController.