ios – Learn how to rename an open doc in a SwiftUI DocumentGroup app with customized navigation?

0
1
ios – Learn how to rename an open doc in a SwiftUI DocumentGroup app with customized navigation?


I am constructing a document-based SwiftUI app following Apple’s Constructing a Doc-Based mostly App with SwiftUI information. The app makes use of DocumentGroup and shops paperwork in iCloud Drive.

Necessary context: We’re utilizing a customized navigation bar as an alternative of the system’s default navigation bar, which implies we do not have entry to the built-in rename performance that comes with the usual doc title bar.

Present Setup

   @principal
   struct MyApp: App {
       var physique: some Scene {
           DocumentGroup(newDocument: MyDocument()) { file in
               ContentView(doc: file.$doc)
                   .navigationBarHidden(true) // Utilizing customized navigation
           }
       }
   }

The Drawback

I must rename the at present open doc programmatically from inside the app. When making an attempt to make use of FileManager.moveItem(at:to:), I get this error:

Error Area=NSCocoaErrorDomain Code=513 “”Untitled 142” could not be moved as a result of
you do not have permission to entry “CamPlan”.”
UserInfo={NSUnderlyingError=0x140b62310 {Error Area=NSPOSIXErrorDomain Code=1 “Operation not permitted”}}

The file path is in iCloud: /personal/var/cell/Library/Cell Paperwork/com~apple~CloudDocs/CamPlan/Untitled 142.cplan

What I’ve Tried

  1. Direct FileManager strategy – Ends in permission error:

    strive FileManager.default.moveItem(at: currentURL, to: newURL)
    
  2. NSFileCoordinator – Hangs indefinitely, doubtless as a result of doc already being coordinated:

    let coordinator = NSFileCoordinator(filePresenter: nil)
    coordinator.coordinate(writingItemAt: currentURL, choices: .forMoving, error: &error) { url in
        // This block by no means executes - coordination hangs
    }
    
  3. Surroundings rename motion – Returns nil since we’re not utilizing the system navigation:

    @Surroundings(.rename) personal var rename  // rename is nil
    
  4. Copy and delete strategy – Additionally fails with permission errors in iCloud listing

Query

How can I programmatically rename the at present open doc in a DocumentGroup-based SwiftUI app when:

  • The doc is saved in iCloud Drive
  • We’re utilizing a customized navigation bar (not the system one)
  • The doc is at present open and being edited

Is there a correct API or methodology to set off a doc rename that works with DocumentGroup’s file coordination and iCloud’s necessities? Or do I want to shut the doc first, rename it, after which reopen it?

Surroundings: iOS 17+, SwiftUI, DocumentGroup structure

LEAVE A REPLY

Please enter your comment!
Please enter your name here