Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS temporary folder location

My app has just been rejected by Apple because it was storing temporary or cache files in the documents directory. Right. Their rejection message states:

Temporary files used by your app should only be stored in the /tmp directory

I suppose it is that besides the Documents and Library in the Application's folder.

I am now trying to debug this issue in the iPhone Simulator, and when I use NSTemporaryDirectory(), the value I get in the Xcode debugger is /var/folders/yj/gnz1c7156c7d6d4fj429yms40000gn/T/tempzip.zip, and not /Users/me/Library/Application Support/iPhone Simulator/5.1/Applications/8F71AB72-598C-427A-A116-36833D3209F7/tmp/tempzip.zip.

So: is NSTemporaryDirectory() having a different behaviour using the iPhone Simulator, and, is it possible to track the application's temporary directory at debug time ?

like image 294
philippe Avatar asked Aug 10 '12 08:08

philippe


People also ask

Where do temporary files go on iPhone?

To view usage on your device, go to Settings > General > [Device] Storage. Your device automatically deletes cached files and temporary files when it needs more space. You don't need to delete them yourself. In short --> Restart the device.

Where do I find temporary folder?

Open Windows Explorer; or Computer/This PC in Windows 10, Windows 8 and Windows 7. Browse to C:\WINDOWS\Temp. This should display the contents of the Temp folder.

Are there temporary files on iPhone?

The iPhone automatically creates hidden files during day-to-day use that are stored in a temporary area of the iPhone's memory called a cache. Clearing this data can free up storage space or speed up your device.


3 Answers

iOS 9 or later • Swift 3 or later

let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory())

iOS 10.0+Beta, macOS 10.12+, tvOS 10.0+Beta & watchOS 3.0+ • Xcode 8 • Swift 3 or later

let tmpDirURL = FileManager.default.temporaryDirectory
like image 54
Leo Dabus Avatar answered Oct 08 '22 19:10

Leo Dabus


I have tested this on a real device, and it returned : "/private/var/mobile/Applications/C82383-EBD6-4F72-BC16-A865478D27/tmp/tempzip.zip"

So overall, using NSTemporaryDirectory() is the correct way of finding the path to the temporary directory, and that if you want to debug and view what is done within, you need to find it manually in the Finder if you are using the iPhone Simulator.

Check newer answer below (this one is deprecated)

like image 22
philippe Avatar answered Oct 08 '22 20:10

philippe


According to the docs, you should avoid NSTemporaryDirectory() in favour of this approach

- (NSURL)URLForTemporaryFolder
{
    // Get a parent folder, trying user folder (fails iOS) and falling back to AppSupport and Docs
    NSURL *parentFolderURL = [NSURL URLForDirectory:NSUserDirectory domainMask:NSUserDomainMask];
    if (!parentFolderURL) parentFolderURL = [NSURL URLForDirectory:NSApplicationSupportDirectory domainMask:NSUserDomainMask];
    if (!parentFolderURL) parentFolderURL = [NSURL URLForDirectory:NSDocumentDirectory domainMask:NSUserDomainMask];

    // Get the temp folder URL using approach outlined in the docs
    NSURL *tmpURL = [[NSFileManager defaultManager]
     URLForDirectory:NSItemReplacementDirectory
     inDomain:NSUserDomainMask
     appropriateForURL:parentFolderURL
     create:YES
     error:NULL];

    return tmpURL;
}

Be aware that this creates a new unique temp folder each time you call it and it's up to you to clean it up.

like image 9
Hari Honor Avatar answered Oct 08 '22 21:10

Hari Honor