Is it possible to change the protection level of folders and files while app is in background and device is locked?

104 Views Asked by At

I need to make some folder / file copy for a local backup, the app gives protection to files and folders, while in background I create, copy delete, zip and encrypt folders and files. while app is active, or if it is in background mode but device still unlocked, code works. But if device is locked I got some errors, sometimes "operation not allowed" some times cannot find my folders. Again, my question: Is there some change when device is locked at "thread management level" ? or is not allowed to change protection level of folder to none if device is locked and app in background? the most of my work is inside document folder and tmp folder. I cannot give snippet of code, but app is too large, my question is about general permissions about iOS fileSystem or threads

1

There are 1 best solutions below

2
Rob Napier On

I don't know what "thread management level" means. But, no, you cannot change the protection of something you cannot currently access (that would defeat the point of protection). So if something is set to "complete," you cannot access it when the device is locked, and so you cannot change its protection. The default value for files you create is "Complete until first user authentication," which allows access once the device has been unlocked at least one time since rebooting. But this default can be overridden in your app's settings.

The typical approach is to accept that data cannot be accessed until that first unlock. UIApplication.isProtectedDataAvailable will let you know when you can access it. You should avoid turning off encrypted unless the use case absolutely requires it and you know for certain that the data will never be sensitive. If you are creating new files in the background, or only need to access files that were opened while in the foreground, "complete unless opened" can be useful (but is a somewhat niche use case).

See Encryption Your App's Files for details and examples. You may also find Practical Security, starting at 5:52, useful as an overview.