2017-10-02 18 views
5

iCloudフォルダのUIDocumentサブクラスのインスタンスの名前を変更する方法を理解するのが難しいです。私は...新しいURLで文書を保存iCloudでのUIDocumentの名前変更

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 
    } 
} 

を試してみた...しかし、これは

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"}}

...と、単純な移動...

...で失敗します
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 
} 

...が失敗するi番目...

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"}}

これらの両方は、ローカルファイルに対して正常に動作し、iCloudのファイルの名前を変更することはUIDocumentBrowserViewControllerルートビューコントローラでOK動作します。

私の推測によれば、アプリがiCloudフォルダに書き込むことができるように、どこかに不足している権限があります。

情報については、のInfo.plistは、以下のすべてのキーが含まれています...

  • LSSupportsOpeningDocumentsInPlace
  • NSExtensionFileProviderSupportsEnumeration
  • UISupportsDocumentBrowser
+0

は、文書のオープンですか? – theMikeSwan

+0

開いているか閉じていても違いはありません。 –

+0

https://developer.apple.com/documentation/foundation/filemanager/1413989-setubiquitousをご覧ください。私は、通常のファイルマネージャメソッドのいくつかがiCloudコンテナで動作しないと確信しています(ただし、iOS 11はものを変更している可能性があります...) – theMikeSwan

答えて

1

あなたはNSFileCoordinatorの文脈でこれをやっていますか?これは必須です。 NSUbiquitousContainers以外のinfo.plist設定は必要ありません。ここで

は、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

誰かがこれを行うのにどのオプションを使用しているのかよく分かります。私が使っていたものよりずっと優れています。しかし、コーディネーターでデフォルトのFileManagerを使用しないと思っていました。 – Sojourner9

関連する問題