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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With