2
votes

I am trying to upload audio file like session1.mp3 from document directory to iCloud using the following code

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");

}

The file i am trying to upload exists (the "File found" is printed), but the uploading it to iCloud generates the following error

Error while uploading the file: Error Domain=NSCocoaErrorDomain Code=513 "The operation couldn’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 "The operation couldn’t be completed. Operation not permitted"}

2
Your error message does not appear to come from the code that you posted. In your code you're working on a file named session1.mp3 that's in the documents directory. But your error message complains about a file named test.png that's located in the app bundle. Could you resolve this conflict? For one of these files the problem is obvious, but in the other it's difficult to tell.Tom Harrington
I have use this code for upload video file from document directory, and successfully uploaded it but this file become remove from document directory why?Ravi Ojha

2 Answers

0
votes

Nothing inherently wrong with your code.

Are you checking the ubiquityIdentityToken anywhere to make sure it is available?

[ [ NSFileManager defaultManager ] ubiquityIdentityToken ];

Also, in your logging of a successful setUbiquitous, "upload successful" is not exact: the item was simply moved to the ubiquity container, and will be uploaded when the demon feels like it. You can check on the upload status through NSMetadataQuery.

-1
votes

You are not supposed to use iCloud to store this media.

Please review the Guidelines given by Apple.