【问题标题】:Upload audio file from document directory to iCloud将音频文件从文档目录上传到 iCloud
【发布时间】:2013-08-04 14:22:55
【问题描述】:

我正在尝试使用以下代码将 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 会产生以下错误

上传文件时出错:Error Domain=NSCocoaErrorDomain Code=513 “无法完成操作。(Cocoa 错误 513。)” UserInfo=0x1f03d9f0 {NSURL=file://localhost/var/mobile /Applications/20D82CDA-021E-4067-B9AB-C0197A6FA834/dox.app/session1.mp3, NSUnderlyingError=0x1f03d990 "操作无法完成。操作不允许"}

【问题讨论】:

  • 您的错误消息似乎并非来自您发布的代码。在您的代码中,您正在处理位于文档目录中的名为session1.mp3 的文件。但是您的错误消息抱怨位于应用程序包中的名为 test.png 的文件。你能解决这个冲突吗?其中一个文件的问题很明显,但在另一个文件中则很难判断。
  • 我已经使用此代码从文档目录上传视频文件,并成功上传,但为什么该文件从文档目录中删除?

标签: iphone ios icloud


【解决方案1】:

您的代码本身没有任何问题。

您是否在任何地方检查 ubiquityIdentityToken 以确保它可用?

[ [ NSFileManager defaultManager ] ubiquityIdentityToken ];

此外,在您记录成功的 setUbiquitous 时,“上传成功”并不准确:该项目只是被移动到 ubiquity 容器中,并且会在恶魔喜欢时上传。您可以通过NSMetadataQuery查看上传状态。

【讨论】:

    【解决方案2】:

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

    请查看 Apple 提供的Guidelines

    【讨论】:

    • 我可以知道原因吗?是否可以将图像上传到 icloud
    • 你可能知道原因。阅读上面提到的人机界面指南。
    • 那些指南实际上并没有这样说。
    • @TomHarrington 实际上,他们确实如此。查看“尊重用户的 iCloud 空间”。和“确定要在 iCloud 中存储哪些类型的信息。”
    • 即使您的回答是准确的,也无法回答上传过程失败的原因。即使禁止上传媒体或图像(它们不是被禁止的),也不会自动检查文件类型。问题中的错误不是由于尝试上传媒体文件造成的。
    猜你喜欢
    • 2016-10-28
    • 2019-03-25
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-28
    • 1970-01-01
    相关资源
    最近更新 更多