Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Renaming and moving a file #11

Closed
wordmagicsoft opened this issue Oct 4, 2013 · 1 comment
Closed

Renaming and moving a file #11

wordmagicsoft opened this issue Oct 4, 2013 · 1 comment

Comments

@wordmagicsoft
Copy link

First of all, thanks for the great work! It really simplifies the use of iCloud in XCode.

I believe you don't have any methods for moving or renaming iCloud documents, right? It would be great to add them. I have the code here:

Renaming Files

- (void)renameIcloudFileWithURL:(NSURL*)sourceURL destURL:(NSURL*)destinationURL success:(void (^)())successBlock failure:(void (^)(NSError *))failureBlock {
    NSParameterAssert(sourceURL);
    NSParameterAssert(destinationURL);
    NSParameterAssert(successBlock);
    NSParameterAssert(failureBlock);

    // Do the actual renaming
    __block NSError *moveError = nil;
    __block BOOL moveSuccess = NO;
    void (^accessor)(NSURL*, NSURL*) = ^(NSURL *newURL1, NSURL *newURL2) {
        NSFileManager *fileManager = [[NSFileManager alloc] init];
        moveSuccess = [fileManager moveItemAtURL:sourceURL toURL:destinationURL error:&moveError];
    };

    // Coordinate renaming
    NSError *coordinatorError = nil;
    NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
    [coordinator coordinateWritingItemAtURL:sourceURL
                              options:NSFileCoordinatorWritingForMoving
                     writingItemAtURL:destinationURL
                              options:NSFileCoordinatorWritingForReplacing
                                error:&coordinatorError
                           byAccessor:accessor];

    if (moveSuccess) {
        successBlock();
        return;
    }

    if (moveError) {
       failureBlock(moveError);
       return;
    }

    if (coordinatorError) {
       failureBlock(coordinatorError);
       return;
    }

    NSAssert(NO, @"should not happen");
}

Moving Files

- (void)_moveURL:(NSURL*)sourceURL destURL:(NSURL*)destinationURL success:(void (^)())successBlock failure:(void (^)(NSError *))failureBlock {
    NSParameterAssert(sourceURL);
    NSParameterAssert(destinationURL);
    NSParameterAssert(successBlock);
    NSParameterAssert(failureBlock);

    // Do the actual renaming
    __block NSError *moveError = nil;
    __block BOOL moveSuccess = NO;
    void (^accessor)(NSURL*, NSURL*) = ^(NSURL *newURL1, NSURL *newURL2) {
        NSFileManager *fileManager = [[NSFileManager alloc] init];
        moveSuccess = [fileManager moveItemAtURL:sourceURL toURL:destinationURL error:&moveError];
    };

    // Coordinate renaming
    NSError *coordinatorError = nil;
    NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
    [coordinator coordinateWritingItemAtURL:sourceURL
                              options:NSFileCoordinatorWritingForMoving
                     writingItemAtURL:destinationURL
                              options:NSFileCoordinatorWritingForReplacing
                                error:&coordinatorError
                           byAccessor:accessor];

    if (moveSuccess) {
        successBlock();
        return;
    }

    if (moveError) {
        failureBlock(moveError);
        return;
    }

    if (coordinatorError) {
        failureBlock(coordinatorError);
        return;
    }

    NSAssert(NO, @"should not happen");
}

Hope this helps! And thanks again...

@Sam-Spencer
Copy link
Member

@wordmagicsoft Thanks for the contribution! This has been added to the version 7.0 milestone. If you'd like to contribute to the repo please Fork it, make the changes, and then submit a Pull Request. In the future, create a Pull Request for this kind of thing instead of an issue. If you need instructions for contributing to the repo, let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants