I have an iPad app which is trying to support document renaming in the title bar. For IOS 17+ I set the renameDelegate to the document instance and it works fine. For IOS 16 I need to create an actual delegate, but no matter how I structure the code it fails with a permission error:
Rename failed: “original_file_name” couldn’t be moved because you don’t have permission to access “Desktop”.
It seems to always happen accessing the parent directory. I have tried using the file coordinator as well with the same result. It seems impossible to implement unless the callback contains a security permissioned url for the parent directory.
Is there anyway to make this work in IOS 16 in the sandbox? Do I have to create my own rename functionality using a FilePicker? Seems like this should be built in like it is in MacOS, or even IOS17+
Here is the code:
extension DocumentWindow : UINavigationItemRenameDelegate {
func navigationItem(_ navigationItem: UINavigationItem, didEndRenamingWith title: String) {
guard let doc = document else { return }
let oldURL = doc.fileURL
let newURL = oldURL.deletingLastPathComponent()
.appendingPathComponent(title)
.appendingPathExtension(oldURL.pathExtension)
if newURL == oldURL { return }
let access = oldURL.startAccessingSecurityScopedResource()
defer { if access { oldURL.stopAccessingSecurityScopedResource() }}
do {
try FileManager.default.moveItem(at: oldURL, to: newURL)
} catch {
print("Rename failed: \(error.localizedDescription)")
}
//
// // 1. Jump to a background queue to avoid the deadlock
// DispatchQueue.global(qos: .userInitiated).async {
// let coordinator = NSFileCoordinator(filePresenter: doc)
// var error: NSError?
//
//
// coordinator.coordinate(writingItemAt: oldURL, error: &error) { outOld in
// do {
// // 2. Perform the actual rename
// try FileManager.default.moveItem(at: outOLD, to: newURL)
// } catch {
// print("Rename failed: \(error.localizedDescription)")
// }
// }
//
// if let error = error {
// print("Coordination error: \(error.localizedDescription)")
// }
// }
}
// 2. Optional: Validation (e.g., prevent empty names)
func navigationItem(_ navigationItem: UINavigationItem, shouldEndRenamingWith title: String) -> Bool {
return !title.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
}
}
0
0
15