I'm using a UIDocument with iCloud. I'm not using CoreData. What's the best way to delete a UIDocument?
5 Answers
Copied from the "Deleting a Document" section of the Document-Based App Programming Guide 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.: "When you delete a document from storage, your code should approximate what UIDocument does for reading and writing operations. It should perform the deletion asynchronously on a background queue, and it should use file coordination."
-
2If you are using a
UIDocument
there is no need to implement your own NSFileCoordinator it is already baked intoUIDocument
. Using your method is expensive results in multipleNSFileCoordinator
instances. See: developer.apple.com/library/ios/documentation/FileManagement/…– JosephCommented Mar 11, 2014 at 9:42 -
1
To delete the document from iCloud, first you have to get the filename 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, which i used to delete document, Check it out. It is woking great for me. Thanks
-
4According to the docs you have to delete stuff async on a background queue. developer.apple.com/library/ios/documentation/DataManagement/…– JonnyCommented Jun 25, 2012 at 7:09
-
Where this works it does not take advantage of
UIDocument
's implementation ofNSFileCoordinator
Adrian Sarli's answer is better.– JosephCommented Mar 11, 2014 at 9:43 -
1For reference, please note that
removeItemAtURL:
will not delete the document if it has not yet been downloaded from iCloud to the device.– MarkCommented Feb 12, 2015 at 8:45 -
I have used this solution in the past but iCloud works like crap on iOS and this solution will fail constantly. AlexChaffee is the only that works for me 100% of the time.– DuckCommented Jan 27, 2016 at 19:10
-
@Mark I too found that
removeItemAtURL:
didn't work while evicted, buttrashItemAtURL:resultingItemURL:error:
does. Commented Mar 26, 2019 at 22:17
SWIFT 3 come from @AlexChaffee 's 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)")
}
}
}
}
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/
-
1That doesn't work- iCloud now ignores this call with a nil-value destinationURL. Commented Jul 3, 2014 at 12:06
See Apple documentation of "Managing the Life-Cyle of a Document" under 'Deleting a Document."
-
1To avoid excessive google usage, here's direct link: developer.apple.com/library/archive/documentation/…– oskuCommented Dec 29, 2018 at 11:39