如何清除 objective C 中的应用程序缓存?
How to clear cache of app in objective C?
我正在构建一个 iOS 应用程序,我在其中实现了 Fabric 并使用 Digits login.When 登录成功会话保持打开状态。
我遇到了一个问题,我想清除应用程序缓存以重置会话,以便用户可以使用新的 phone number.But 再次登录,但这并没有发生。
我用来清除应用缓存的代码:
- (void)viewDidLoad {
[super viewDidLoad];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
}
感谢帮助!
由于您的问题的某些关键部分缺失(例如您使用的身份验证方法),我将在此处 post 一个可能会解决您的问题的方便的片段。
但请注意:这将删除所有 cookie 和所有缓存的响应以及所有 NSURLCredentials(只要它们未持久化)。
- (void)removeAllStoredCredentials
{
// Delete any cached URLrequests!
NSURLCache *sharedCache = [NSURLCache sharedURLCache];
[sharedCache removeAllCachedResponses];
// Also delete all stored cookies!
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *cookies = [cookieStorage cookies];
id cookie;
for (cookie in cookies) {
[cookieStorage deleteCookie:cookie];
}
NSDictionary *credentialsDict = [[NSURLCredentialStorage sharedCredentialStorage] allCredentials];
if ([credentialsDict count] > 0) {
// the credentialsDict has NSURLProtectionSpace objs as keys and dicts of userName => NSURLCredential
NSEnumerator *protectionSpaceEnumerator = [credentialsDict keyEnumerator];
id urlProtectionSpace;
// iterate over all NSURLProtectionSpaces
while (urlProtectionSpace = [protectionSpaceEnumerator nextObject]) {
NSEnumerator *userNameEnumerator = [[credentialsDict objectForKey:urlProtectionSpace] keyEnumerator];
id userName;
// iterate over all usernames for this protectionspace, which are the keys for the actual NSURLCredentials
while (userName = [userNameEnumerator nextObject]) {
NSURLCredential *cred = [[credentialsDict objectForKey:urlProtectionSpace] objectForKey:userName];
//NSLog(@"credentials to be removed: %@", cred);
[[NSURLCredentialStorage sharedCredentialStorage] removeCredential:cred forProtectionSpace:urlProtectionSpace];
}
}
}
}
你可以使用你的库自己的高级机制,而不是使用低级和混乱的 NSURLCache:
[[Digits sharedInstance] logOut]
我正在构建一个 iOS 应用程序,我在其中实现了 Fabric 并使用 Digits login.When 登录成功会话保持打开状态。
我遇到了一个问题,我想清除应用程序缓存以重置会话,以便用户可以使用新的 phone number.But 再次登录,但这并没有发生。
我用来清除应用缓存的代码:
- (void)viewDidLoad {
[super viewDidLoad];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
}
感谢帮助!
由于您的问题的某些关键部分缺失(例如您使用的身份验证方法),我将在此处 post 一个可能会解决您的问题的方便的片段。
但请注意:这将删除所有 cookie 和所有缓存的响应以及所有 NSURLCredentials(只要它们未持久化)。
- (void)removeAllStoredCredentials
{
// Delete any cached URLrequests!
NSURLCache *sharedCache = [NSURLCache sharedURLCache];
[sharedCache removeAllCachedResponses];
// Also delete all stored cookies!
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *cookies = [cookieStorage cookies];
id cookie;
for (cookie in cookies) {
[cookieStorage deleteCookie:cookie];
}
NSDictionary *credentialsDict = [[NSURLCredentialStorage sharedCredentialStorage] allCredentials];
if ([credentialsDict count] > 0) {
// the credentialsDict has NSURLProtectionSpace objs as keys and dicts of userName => NSURLCredential
NSEnumerator *protectionSpaceEnumerator = [credentialsDict keyEnumerator];
id urlProtectionSpace;
// iterate over all NSURLProtectionSpaces
while (urlProtectionSpace = [protectionSpaceEnumerator nextObject]) {
NSEnumerator *userNameEnumerator = [[credentialsDict objectForKey:urlProtectionSpace] keyEnumerator];
id userName;
// iterate over all usernames for this protectionspace, which are the keys for the actual NSURLCredentials
while (userName = [userNameEnumerator nextObject]) {
NSURLCredential *cred = [[credentialsDict objectForKey:urlProtectionSpace] objectForKey:userName];
//NSLog(@"credentials to be removed: %@", cred);
[[NSURLCredentialStorage sharedCredentialStorage] removeCredential:cred forProtectionSpace:urlProtectionSpace];
}
}
}
}
你可以使用你的库自己的高级机制,而不是使用低级和混乱的 NSURLCache:
[[Digits sharedInstance] logOut]