Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: What's a safe way to create a temporary directory?

I read that NSTemporaryDirectory() returns a string for a temporary directory, but may also return nil. Is this the case in iOS? Has anyone experience with NSTemporaryDirectory() returning nil? When does it happen?

If i have to be prepared that NSTemporaryDirectory() may return nil, it does not make sense for me to use it altogether. If i have to provide a fallback mechanism i can use this mechanism in the first place.

So what is a simple and safe way to create a temporary directory?

like image 308
Tomen Avatar asked Dec 22 '10 11:12

Tomen


People also ask

How do I create a temporary working directory?

Use mktemp -d . It creates a temporary directory with a random name and makes sure that file doesn't already exist. You need to remember to delete the directory after using it though.

What is temp directory in iOS?

It can also be the "Cache" directory. Data with short-life time can be stored in the tmp directory of your application. In this case, use of NSTemporaryDirectory() is possible to get the "tmp" directory.

Which directory is used for holding temporary files?

In Unix and Linux, the global temporary directories are /tmp and /var/tmp. Web browsers periodically write data to the tmp directory during page views and downloads. Typically, /var/tmp is for persistent files (as it may be preserved over reboots), and /tmp is for more temporary files.

How do I find temporary files on my 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.


1 Answers

Being realistic, any situation that would lead NSTemporaryDirectory to return nil, would most likely mean that you wouldn't be able to save anything yourself anyway.

i.e.: It's likely to mean that the device is full, or that some form of corruption has occurred.

As such, I suspect that you should stick to using NSTemporaryDirectory (why re-invent the wheel after all) and treat it returning nil as effectively meaning you can't write to local storage.

like image 167
John Parker Avatar answered Oct 09 '22 19:10

John Parker