2

次のコードを使用して、ドキュメント ディレクトリから iCloud に session1.mp3 のようなオーディオ ファイルをアップロードしようとしています。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"session1.mp3"];
NSURL* sourceURL = [[NSURL alloc] initFileURLWithPath:path];

NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:path]) {
    NSLog(@"File found!");
}
else
{
    NSLog(@"File not found!");
}



NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
if (ubiq)
{
    NSLog(@"iCloud access at %@", ubiq);
    NSError* error = nil;
    NSURL *destinationURL = [[ubiq URLByAppendingPathComponent:@"Documents"] URLByAppendingPathComponent:@"session1.mp3"];
    [[NSFileManager defaultManager] setUbiquitous:YES
                                        itemAtURL:sourceURL
                                   destinationURL:destinationURL
                                            error:&error];

    if(error != nil)
    {
        NSLog(@"Error while uploading the file: %@", error);
    }
    else
    {
        NSLog(@"File ulpoaded successfully");
    }
}
else
{
    NSLog(@"No iCloud access");

}

アップロードしようとしているファイルは存在しますが (「ファイルが見つかりました」と表示されます)、iCloud にアップロードすると次のエラーが発生します。

ファイルのアップロード中にエラーが発生しました: エラー Domain=NSCocoaErrorDomain Code=513 "The operation could't be completed. (Cocoa error 513.)" UserInfo=0x1f03d9f0 {NSURL=file://localhost/var/mobile/Applications/20D82CDA-021E -4067-B9AB-C0197A6FA834/dox.app/session1.mp3, NSUnderlyingError=0x1f03d990 "操作を完了できませんでした。操作は許可されていません"}

4

2 に答える 2

0

コードに本質的な問題はありません。

どこでも ubiquityIdentityToken をチェックして、それが利用可能であることを確認していますか?

[ [ NSFileManager defaultManager ] ubiquityIdentityToken ];

また、成功した setUbiquitous のログでは、「アップロード成功」は正確ではありません。アイテムは単純にユビキタス コンテナに移動され、悪魔が気が向いたときにアップロードされます。NSMetadataQueryでアップロードのステータスを確認できます。

于 2015-02-12T10:53:55.527 に答える