简体   繁体   中英

“The operation couldn’t be completed. Operation not permitted” error when simply sending image to icloud

Im simply trying to upload this image to iCloud. It keeps giving me the error "The operation couldn't be completed. Operation not permitted". I took this code right out of the Document Based App Programming Guide and I believe I've set all my certificates, identifiers, profiles, and entitlements correctly. Any help would be much appreciated. This is insanely frustrating.

#import "docx.h"

@implementation docx

-(IBAction)test:(id)sender{

    NSURL *src = [NSURL URLWithString:@"/Users/rjm226/Desktop/jh.jpg"];
        NSLog(@"%@", src);

    NSURL *dest = NULL;

    NSURL *ubiquityContainerURL = [[[NSFileManager defaultManager]
                                    URLForUbiquityContainerIdentifier:nil]
                                   URLByAppendingPathComponent:@"Documents"];

    if (ubiquityContainerURL == nil) {
        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: NSLocalizedString(@"iCloud does not appear to be configured.", @""),
                              NSLocalizedFailureReasonErrorKey, nil];

        NSError *error = [NSError errorWithDomain:@"Application" code:404
                                         userInfo:dict];

        [self presentError:error modalForWindow:[self windowForSheet] delegate:nil didPresentSelector:NULL contextInfo:NULL];

        return;
    }

    dest = [ubiquityContainerURL URLByAppendingPathComponent:
            [src lastPathComponent]];

    //Move file to iCloud

    dispatch_queue_t globalQueue =
    dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    dispatch_async(globalQueue, ^(void) {

        NSFileManager *fileManager = [[NSFileManager alloc] init];

        NSError *error = nil;
        // Move the file.

        BOOL success = [fileManager setUbiquitous:YES itemAtURL:src
                                   destinationURL:dest error:&error];

        dispatch_async(dispatch_get_main_queue(), ^(void) {
            if (! success) {
                [self presentError:error modalForWindow:[self windowForSheet]
                          delegate:nil didPresentSelector:NULL contextInfo:NULL];
            }
        });
    });
}

@end

This is insanely frustrating.

Welcome to iCloud. You'll get used to that feeling after a while. In the meantime though, you have other problems.

NSURL *src = [NSURL URLWithString:@"/Users/rjm226/Desktop/jh.jpg"];

This is not going to give you a valid URL, which is going to be a problem. What you need is:

NSURL *src = [NSURL fileURLWithPath:@"/Users/rjm226/Desktop/jh.jpg"];

That will give you a valid file:// URL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM