2017-10-02 14 views
5

Staram się dowiedzieć, jak zmienić nazwę instancji podklasy UIDocument w folderze iCloud. Próbowałem zapisać dokument z nowym adresem ...Zmienianie nazwy dokumentu UID w usłudze iCloud

func renameDocument(to name: String) { 

    let targetURL = document.fileURL.deletingLastPathComponent().appendingPathComponent(name) 
     .appendingPathExtension("<extension>") 

    document.save(to: targetURL, for: .forCreating) { success in 
     guard success else { 
      // This always fails 
      return 
     } 
     // Success 
    } 
} 

... ale to nie z ...

Error Domain=NSCocoaErrorDomain Code=513 "“<new-file-name>” couldn’t be moved because you don’t have permission to access “<folder>”." UserInfo={NSSourceFilePathErrorKey=/private/var/mobile/Containers/Data/Application/1A9ACC2B-81EF-4EC9-940E-1C129BDB1914/tmp/(A Document Being Saved By My App)/<new-file-name>, NSUserStringVariant=( Move ), NSDestinationFilePath=/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs/<folder>/<new-file-name>, NSFilePath=/private/var/mobile/Containers/Data/Application/1A9ACC2B-81EF-4EC9-940E-1C129BDB1914/tmp/(A Document Being Saved By My App)/<new-file-name>, NSUnderlyingError=0x1c4e54280 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

... i tylko prosty ruch ...

func renameDocument(to name: String) { 
    let targetURL = document.fileURL.deletingLastPathComponent().appendingPathComponent(name) 
     .appendingPathExtension("<extension>") 

    do { 
     try FileManager.default.moveItem(at: document.fileURL, to: targetURL) 
    } catch { 
     // This always fails 
    }   
    // Success 
} 

... co nie powiedzie się w ith ...

Error Domain=NSCocoaErrorDomain Code=513 "“<old-file-name>” couldn’t be moved because you don’t have permission to access “<folder>”." UserInfo={NSSourceFilePathErrorKey=/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs/<folder>/<old-file-name>, NSUserStringVariant=( Move ), NSDestinationFilePath=/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs/<folder>/<new-file-name>, NSFilePath=/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs/<folder>/<old-file-name>, NSUnderlyingError=0x1c4c4d8c0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

Obie te grzywny pracy dla plików lokalnych i zmiany nazw plików iCloud działa OK w widoku kontrolera UIDocumentBrowserViewController korzeniowego.

Zgaduję, że w pewnym miejscu brakuje zezwolenia, które pozwala aplikacji pisać w folderach iCloud.

Dla informacji, Info.plist zawiera wszystkie następujące klucze ...

  • LSSupportsOpeningDocumentsInPlace
  • NSExtensionFileProviderSupportsEnumeration
  • UISupportsDocumentBrowser
+0

Czy dokument jest otwarty, gdy trzeba zmienić jego nazwę? – theMikeSwan

+0

Nie ma znaczenia, czy jest otwarta, czy zamknięta. –

+0

Zobacz, https://developer.apple.com/documentation/foundation/filemanager/1413989-setubiquitous, czy to pomaga. Jestem pewien, że niektóre z normalnych metod zarządzania plikami nie działają w kontenerze iCloud (chociaż iOS 11 mógł zmienić pewne rzeczy ...) – theMikeSwan

Odpowiedz

1

Robisz to w kontekście NSFileCoordinator? Jest to wymagane. Nie powinieneś potrzebować żadnych ustawień info.plist poza NSUbiquitousContainers.

Tu jest mój kod do zmiany nazw dokumentów iCloud:

/// 
/// move cloudFile within same store - show errors 
/// 
- (void)moveCloudFile:(NSURL *)url toUrl:(NSURL *)newURL 
{ 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), 
    ^{ 
     NSError *coordinationError; 
     NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil]; 

     [coordinator coordinateWritingItemAtURL:url options:NSFileCoordinatorWritingForMoving 
           writingItemAtURL:newURL options:NSFileCoordinatorWritingForReplacing 
                 error:&coordinationError 
            byAccessor: 
     ^(NSURL *readingURL, NSURL *writingURL){ 
      if ([self moveFile:readingURL toUrl:writingURL]) 
       [coordinator itemAtURL:readingURL didMoveToURL:writingURL]; 
     }]; 
     if (coordinationError) { 
      MRLOG(@"Coordination error: %@", coordinationError); 
      [(SSApplication *)SSApplication.sharedApplication fileErrorAlert:coordinationError]; 
     } 
    }); 
} 

/// 
/// 
/// move file within same store - show errors 
/// 
- (BOOL)moveFile:(NSURL *)url toUrl:(NSURL *)newURL 
{ 
    NSFileManager *manager = NSFileManager.defaultManager; 
    NSError *error; 

    if ([manager fileExistsAtPath:newURL.path]) 
     [self removeFile:newURL]; 

    if ([manager moveItemAtURL:url toURL:newURL error:&error] == NO) { 
     MRLOG(@"Move failed: %@", error); 
     [(SSApplication *)SSApplication.sharedApplication fileErrorAlert:error]; 
     return NO; 
    } 
    return YES; 
} 
+0

Miło zobaczyć, które opcje ktoś wcześniej użył. Znacznie lepiej niż to, z czego korzystałem. Ale myślałem, że nie używałeś domyślnego menedżera plików FileManager w Koordynatorze. – Sojourner9

Powiązane problemy