programmatically clearing secondary cache on iPhon

2019-07-27 16:07发布

I have created an application , where i ll be downloading the images from server and storing it locally on the iPhone's file system. Its happening fine. Now the problem is , i want to clear the locally cached images on iPhone when ever i quit my application.

How do i delete those cached images on iPhone. it's using Secondary memory on iPhone, how do i access it programmatically ?

Thank You in advance. Suse.

2条回答
smile是对你的礼貌
2楼-- · 2019-07-27 16:24

Clean them up in your applicationWillTerminate method.

- (void)applicationWillTerminate:(UIApplication *)application
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *imagesFiles = [fileManager contentsOfDirectoryAtPath:saveDirectory error:error];
    for (NSString *file in imagesFiles) {
        error = nil;
        [fileManager removeItemAtPath:[saveDirectory stringByAppendingPathComponent:file] error:error];
        /* do error handling here */
    }
}
查看更多
别忘想泡老子
3楼-- · 2019-07-27 16:24

My answer would be as per Kirky Todds, but don't use applicationWillTerminate -- it's not a good strategy on iOS4+ with multitasking, because this method often won't get called -- an app will be background, and then can be dumped from memory if other apps are run (or the phone is turned off/restarted), without applicationWillTerminate getting called. (OR it will be foregrounded again. Either way, your cache doesn't get cleared.)

Instead, consider either clearing at startup (applicationDidFinishLaunching), or in applicationWillEnterForeground. The latter will tend to get called more frequently, because the former is only called on an actual proper app lauch (and not on a resume from being backgrounded).

For more info on backgrounding and the events generated, see:

http://www.cocoanetics.com/2010/07/understanding-ios-4-backgrounding-and-delegate-messaging/

查看更多
登录 后发表回答