Does anyone by coincidence know how I can empty the cache memory of the iOS app that I am developing, in the moment it goes to the background (applicationDidEnterBackground)? I have investigated about NSCache but I am still not able to understand how could I retrieve the cache to basically remove/free it?
Empty the cache programmatically in iOS
Is this what you're talking about?
[[NSURLCache sharedURLCache] removeAllCachedResponses];
You can also modify the cache behavior of your requests to selectively cache responses. If you're using AFNetworking
by chance, you can use setCacheResponseBlock
. E.g. in one project I set it to return nil
for all large video and audio files. But allow it to cache smaller image files.
[streamingOperation setCacheResponseBlock:^NSCachedURLResponse *(NSURLConnection *connection, NSCachedURLResponse *cachedResponse) {
return nil; // Ensures we are not unecessarily caching asset data to Cache.db
}];
Hello Alfie! Not sure if this will work to me. The only thing that I need is to clean the cache memory when the user in his/her iPad sends the app to the background. For example, I think the password and certain data always stay somewhere in memory (different from the keychain wrapper) so for security reasons, those data in memory must be removed. What I don't know yet is how to indicate my program by code that I need to retrieve all data in cache and free it. Do I explain myself? Thanks a lot for your interest. –
Ahlgren
Yes this additional info helps me understand your question better. Can you give me a few examples of data that you want to "clear from the in-memory cache" any also why you feel the need to clear it? –
Eleusis
If you're specifically concerned about security of a password while it's in memory, I'm almost positive that there's no way for anyone to access this data from memory (i.e. no need to clear it / not a concern). If you were storing it in NSUserDefaults then it'd be a concern. Also, depending on where your reference to the password exists, you could just set that property to nil upon backgrounding. –
Eleusis
Thanks. I am storing the password in a keychain wrapper, but the requirements say that password should not be deleted from there, but from cache memory. Does this make sense to you Alfie? –
Ahlgren
Can you send me a link to the "requirements"? Doesn't really make sense. The keychain is secure and by default is partitioned to only be accessible to your App. And objects held in memory aren't accessible outside of your code in execution. And outside the realm of NSURLConnection responses I'm not aware of a cache that holds normal @properties you'd have in memory. –
Eleusis
Alfie, please look at the requirement: Create a Keychain wrapper that runs only in the foreground. If an "entering background" notification is found, remove data from memory. The thing with it is, that if I remove the passw from the keychain, the user will be unable to log in into it when the app comes to the foreground again. Or am I interpreting this incorrectly? It also says this: The keychain is protected by iOS. Keychain items can be made accessible only when the application is running in foreground. A possible workaround is locking the keychain while the app is in background or..? Thanks –
Ahlgren
Please send the link to the "requirements" you mention. I think you're misinterpreting something. Info stored in the keychain is secure regardless of whether your App is in the fg or bg. And information currently in memory is not accessible to anyone, it can't be snooped. –
Eleusis
© 2022 - 2024 — McMap. All rights reserved.
[myCache removeAllObjects];
should do the trick. – Isaak@property (strong) NSCache *myCache
and use almost the same asNSMutableDictionary
. Some examples here: https://mcmap.net/q/180514/-how-to-use-nscache – Isaak