如何在 iPhone 上以编程方式清除浏览器缓存?

如何在 iPhone 上以编程方式清除浏览器缓存?

问题描述:

我正在为我的 iOS 大本营客户端应用程序使用新的 Basecamp API.我希望用户能够注销和切换帐户.但是我不能,因为每次请求授权时都会使用存储在浏览器缓存中的帐户凭据.我发现我需要刷新浏览器缓存才能做到这一点.如何清除浏览器缓存?

I am using the new Basecamp API for my iOS basecamp client app. I want the user to be able to logout and switch accounts. But I can't as the account credentials stored in the browser cache are used every time I request authorization. I figured out I would need to flush browser cache to do this. How do I clear the browser cache?

[[NSURLCache sharedURLCache] removeAllCachedResponses];

之后,您可以在 UIWebView 中删除任何关联的 cookie:

After that, you can deleting any associated cookies with in the UIWebView:

for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {

    if([[cookie domain] isEqualToString:someNSStringUrlDomain]) {

        [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
    }
}