3

i used code for remove Temporary Directory files when using device.

-(void) clearAllTempFiles {
    NSString *path = NSTemporaryDirectory();
    if ([path length] > 0)
    {
        NSError *error = nil;  
        NSFileManager *fileManager = [NSFileManager defaultManager];

        BOOL deleted = [fileManager removeItemAtPath:path error:&error];

        if (deleted != YES || error != nil)
        {

        }
        else{
            // Recreate the Documents directory
            [fileManager createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:&error];
        }

    }
}

it's not working fine? this is right code for deleting files from Temporary Directory?

pls help me?

sreenivas
  • 399
  • 2
  • 5
  • 20

2 Answers2

6

You can get the tmp directory name on your mac by using this in your code:

Code:

(void)cacheDirectory {

    NSString *tempPath = NSTemporaryDirectory();

    NSLog(@"Temp Value = %@", items);
}

Call the method from wherever you want.

This will return the tmp folder name, then in finder do (cmd-shift-G) and paste the response you got from the console window.

The following will clear the TMP directory used by the Simulator.

Code:

NSString *tempPath = NSTemporaryDirectory();
NSArray *dirContents = [[NSFileManager defaultManager] directoryContentsAtPath:tempPath];
NSArray *onlyJPGs = [dirContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.jpg'"]];

NSFileManager *fileManager = [NSFileManager defaultManager];

if (onlyJPGs) { 
    for (int i = 0; i < [onlyJPGs count]; i++) {
    NSLog(@"Directory Count: %i", [onlyJPGs count]);
    NSString *contentsOnly = [NSString stringWithFormat:@"%@%@", tempPath, [onlyJPGs objectAtIndex:i]];
    [fileManager removeItemAtPath:contentsOnly error:nil];
}

The above code clears only JPGs from the directory, so if you want to clear anything else then amend it.

  • thats happens because your application is running in background process. if you do not want to run application in background in info.plist file set "Application does not run in background"=YES – hchouhan02 Mar 12 '12 at 06:47
  • my problem is: I have 3 pages application when i stay on one page(loginscreen) in on page and i press home button. Again i press my application icon on springboard now i don't want show loginscreen i want to go another screen (secondscreen). But i got first screen(loginscreen) is appear on background screen ofter that second screen is getting for this purpose i used cleanallTempdirectoryfile? – sreenivas Mar 12 '12 at 06:51
5

I found simple one

NSArray* tmpDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:NULL];
for (NSString *file in tmpDirectory) {
    [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), file] error:NULL];
}
Sukeshj
  • 1,503
  • 1
  • 19
  • 25