Hi there,
I've encountered a file permission bug in iOS 18.1.1 when using FileManager.default.copyItem(at:to:)
to copy files from an iCloud shared folder to the app sandbox. This issue occurs under the following conditions:
- The source file resides in an iCloud shared folder.
- The iCloud shared folder is owned by another iCloud user and shared with read-only permissions.
- The app copies the file to its sandbox using the copyItem(at:to:) method.
Observed Behavior:
After copying the file to the app sandbox, the original file's read-only permissions are propagated to the copied file. This results in an inability to delete the copied file from the app sandbox, with the following error message:
NSCocoaErrorDomain, Code 513: "The file couldn’t be removed because you don’t have permission to access it."
Steps to Reproduce:
- Access a shared iCloud folder owned by another user with read-only permissions.
- Copy a file from this folder to the app sandbox using FileManager.default.copyItem(at:to:).
- Attempt to delete the copied file within the app sandbox.
Workaround: Until this issue is resolved, the bug can be avoided by initializing the UIDocumentPickerViewController with the asCopy: true parameter:
UIDocumentPickerViewController(forOpeningContentTypes: contentTypes, asCopy: true)
This ensures that the copied file does not inherit the original permissions from the shared source file.
Example Project: To reproduce the issue and observe the error, I’ve created a sample project hosted on GitHub: https://github.com/giomurru/FileDeletePermissionBug
This project provides step-by-step instructions for testing and reproducing the bug.
Environment:
iOS/iPadOS Version: 18.1.1 Devices: [iPhone 15, iPad 9th Gen] Development Tool: Xcode 16.1
I hope this helps Apple engineers and other developers experiencing the same issue. Feedback or additional insights would be appreciated.
Giovanni
I've encountered a file permission bug in iOS 18.1.1 when using FileManager.default.copyItem(at:to:) to copy files from an iCloud shared folder to the app sandbox.
This isn't a bug. By design, whenever possible, NSFileManager attempts to preserve the permissions of the files it copies and that's what's happened here. However, what you've overlooked here is the permissions don't have have to STAY that way. You have write access to the containing directory (otherwise, you wouldn't have been able to copy the item at all). You can change the permission to whatever you want with setAttributes(_:ofItemAtPath:) or with chmod().
__
Kevin Elliott
DTS Engineer, CoreOS/Hardware