简体   繁体   中英

iOS - Reserve a minimum amount of free disk space for an app

My app has to make sure that there is always a minimum amount (maybe 5-10 MB) of free disk space available, for it to operate correctly. I have to save important data to file, otherwise everything is going to be corrupted.

The main problem is: What if the user downloads my app and he had EXACTLY that amount of disk space free that he needed for the app (without my on first startup created data)

When he's now starting the app I try to do some initial setup stuff - here I have to write around 2-3 MB to file and this space must be free.

What can I do to achieve this?

You can do two things:

First, when your application starts, you can find out the available space using NSFileManager.attributesForFilesystemofPath: . If the amount of space is low (and 5 MB free is really low , you can warn the user and maybe even refuse to start actions in your app that require this available space.

Second, you could create a scratch file. If you know you need 5MB storage, then maybe you can allocate a 5MB file and use that file for your storage.

You don't specify what you need to store on disk but if it were for example one file then you could write this data over your 5MB scratch file instead of opening a new file.

This requires some extra bookkeeping, but you do get guaranteed space for your data.

In short, you can't. You will need to write your code to have an initial startup that checks to see if there is enough disk space to do your initial setup, and display an error if not. I haven't tried to do this on iOS before so I'm not sure what the sandbox allows you to do off the top of my head. I'd have to go read up in the iOS file system docs. That's what I suggest you do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM