HomeiOS Developmentios - Tips on how to rename an open doc in a...

ios – Tips on 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

   @most important
   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 presently open doc programmatically from inside the app. When trying 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 method – Leads to permission error:

    attempt FileManager.default.moveItem(at: currentURL, to: newURL)
    
  2. NSFileCoordinator – Hangs indefinitely, possible 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. Atmosphere rename motion – Returns nil since we’re not utilizing the system navigation:

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

Query

How can I programmatically rename the presently 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 presently open and being edited

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

Atmosphere: iOS 17+, SwiftUI, DocumentGroup structure

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments