How to delete iCloud documents?

I am using UIDocument with iCloud. I do not use CoreData. What is the best way to remove UIDocument?

+5
source share
5 answers

To delete a document from iCloud, you must first get the name of the file you want to delete. and then you can delete it using NSFileManager.

NSString *saveFileName = @"Report.pdf";
NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
NSURL *ubiquitousPackage = [[ubiq URLByAppendingPathComponent:@"Documents"] URLByAppendingPathComponent:saveFileName];
NSFileManager *filemgr = [NSFileManager defaultManager];
[filemgr removeItemAtURL:ubiquitousPackage error:nil];

This is the way I used to delete a document, check it out. This is great for me. Thanks

+9
source

Copied from the "Deleting a Document" section in a Document- Based Application for iOS .

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
    NSFileCoordinator* fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
    [fileCoordinator coordinateWritingItemAtURL:fileURL options:NSFileCoordinatorWritingForDeleting
        error:nil byAccessor:^(NSURL* writingURL) {
        NSFileManager* fileManager = [[NSFileManager alloc] init];
        [fileManager removeItemAtURL:writingURL error:nil];
    }];
});

N.B.: " , , UIDocument . , ."

+13

SWIFT 3 came from @AlexChaffee answer

func deleteZipFile(with filePath: String) {
    DispatchQueue.global(qos: .default).async {
        let fileCoordinator = NSFileCoordinator(filePresenter: nil)
        fileCoordinator.coordinate(writingItemAt: URL(fileURLWithPath: filePath), options: NSFileCoordinator.WritingOptions.forDeleting, error: nil) {
            writingURL in
            do {
                try FileManager.default.removeItem(at: writingURL)
            } catch {
                DLog("error: \(error.localizedDescription)")
            }
        }
    }
}
+1
source

I think I found a solution:

[[NSFileManager defaultManager] setUbiquitous:NO itemAtURL:url destinationURL:nil error:nil]

Source: http://oleb.net/blog/2011/11/ios5-tech-talk-michael-jurewitz-on-icloud-storage/

0
source

See Apple Documentation "Document Lifecycle Management" in the "Delete a Document" section.

0
source

All Articles