Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa error 262, Can't upload file to iCloud

Tags:

ios

iphone

icloud

I am trying to use following lines of code to upload a large file to iCloud:

-(void)uploadFileWithFilePath:(NSString *)Path toFileURLInCloud:(NSURL *)destURL{
    NSURL * filePath = [NSURL URLWithString:Path];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void)
    {
        NSFileCoordinator* fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
        [fileCoordinator coordinateReadingItemAtURL:filePath
                            options:NSFileCoordinatorReadingWithoutChanges
                              error:nil
                         byAccessor:^(NSURL *newURL)
        {
            NSFileManager * fileManager = [[NSFileManager alloc] init];
            NSError * error;
            BOOL success = [fileManager copyItemAtURL:filePath toURL:destURL error:&error];
            if (success) {
                NSLog(@"Copied %@ to %@", filePath, destURL);
            }
            else {
                NSLog(@"Failed to copy %@ to %@: %@", filePath, destURL, error.localizedDescription);
            }
        }];
    });
}

I get this error

The operation couldn’t be completed. (Cocoa error 262.)

What could be the problem here?

like image 435
aakpro Avatar asked Dec 01 '22 20:12

aakpro


1 Answers

Well, the web seems to think that this is commonly caused by initializing the url with a string path as you are. I advise changing this:

NSURL * filePath = [NSURL URLWithString:Path];

To this:

NSURL * filePath = [NSURL fileURLWithPath:Path];
like image 103
Mick MacCallum Avatar answered Dec 07 '22 23:12

Mick MacCallum