首页 文章

将音频文件从文档目录上传到 iCloud

提问于
浏览
2

我正在尝试使用以下代码将文档目录中的 session1.mp3 等音频文件上传到 iCloud

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“无法完成操作.(Cocoa error 513.)”UserInfo=0x1f03d9f0 {NSURL=file://localhost/var/mobile/Applications/20D82CDA-021E-4067-B9AB-C0197A6FA834/dox.app/session1.mp3,NSUnderlyingError=0x1f03d990“操作无法完成.操作不允许”}

2 回答

  • 0

    您的代码没有任何内在错误。

    你在任何地方检查 ubiquityIdentityToken 以确保它可用吗?

    [ [ NSFileManager defaultManager ] ubiquityIdentityToken ];
    

    此外,在您成功设置的记录中,“上传成功”并不准确:该项目只是移动到普遍容器,并将在恶魔感觉时上传。您可以通过NSMetadataQuery查看上传状态。

  • -1

    您不应该使用 iCloud 存储此媒体。

    请查看 Apple 提供的方针

相关问题