我有一个iOS应用程序,它将数据存储在NSUserDefults
中,并且许多其他数据由于Web视图加载,社交媒体签名等而设置在缓存中。 我想从应用程序创建的缓存中删除所有数据。有什么方法可以清除应用程序安装中存储的所有数据?
有没有办法在iOS中以编程方式执行此操作?
我有一个iOS应用程序,它将数据存储在NSUserDefults
中,并且许多其他数据由于Web视图加载,社交媒体签名等而设置在缓存中。 我想从应用程序创建的缓存中删除所有数据。有什么方法可以清除应用程序安装中存储的所有数据?
有没有办法在iOS中以编程方式执行此操作?
您可以尝试使用[[NSURLCache sharedURLCache] removeAllCachedResponses];
有关详细信息,请参阅链接http://iphonedevsdk.com/forum/iphone-sdk-development/104198-programmatically-clearing-app-cache.html
如果使用NSUserDefaults
,您可以尝试this。
就其他缓存文件而言,我认为您应该将它们全部保留在一个文件夹中,以便通过删除整个缓存文件夹来轻松删除它们。
你可以试试这个
NSUserDefaults的
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
或
NSUserDefaults * defs = [NSUserDefaults standardUserDefaults];
NSDictionary * dict = [defs dictionaryRepresentation];
for (id key in dict) {
[defs removeObjectForKey:key];
}
[defs synchronize];
UIWebView的缓存
[NSURLCache sharedURLCache] removeAllCachedResponses];
//删除cookie
for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
if([[cookie domain] isEqualToString:someNSStringUrlDomain]) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
}
尝试
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
或
[[NSUserDefaults standardUserDefaults] setPersistentDomain:[NSDictionary dictionary] forName:[[NSBundle mainBundle] bundleIdentifier]];
尝试此,
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"defunctPreference"];
一些高速缓存都与其他应用的连接Informa公司灰。删除它们时应该小心。只是提醒你在操作之前检查。我曾犯过类似的愚蠢错误。如果你找到了一个好工具。请分享!谢谢。
此链接已经涵盖了您的问题:[链接](http://stackoverflow.com/questions/10460690/can-i-programmatically-wipe-the-application-data-in-applicationdidfinishlaunchin) – 2014-11-25 09:43:37