问题
I am trying to work out how to sync a .plist file I have in the "Application Support" folder in my Sandboxed app for the Mac. I know I could use the iCloud key value store, but there is a limit of 64KB per app, which may or may not be hit depending on how many thing the user adds to the app!
I have read as much of the Apple documentation as possible, but I am still rather confused :(
Has anyone does something similar to this?
Thanks
回答1:
You should create a subclass of UIDocument
and use it with ubiquity directories.
There are 2 methods responsible for handling read/write. This one is called when reading:
- (BOOL)loadFromContents:(id)contents ofType:(NSString *)typeName error:(NSError **)outError
And this one when writing:
- (id)contentsForType:(NSString *)typeName error:(NSError **)outError
All open/save actions are called automatically, you don't have to do anything. Howewer, there are methods that force open/save. Call this when opening:
- (void)openWithCompletionHandler:(void (^)(BOOL success))completionHandler
/* --- EXAMPLE --- */
MyDocument *doc = [[MyDocument alloc] initWithFileURL:ubiquitousFileURL];
[doc openWithCompletionHandler:^(BOOL success) {
if (success) {
// do sth
} else {
// handle error
}
}];
... and this when saving:
- (void)saveToURL:(NSURL *)url forSaveOperation:(UIDocumentSaveOperation)saveOperation completionHandler:(void (^)(BOOL success))completionHandler
/* --- EXAMPLE --- */
MyDocument *doc = [[MyDocument alloc] initWithFileURL:ubiquitousPackage];
[doc saveToURL:[doc fileURL] forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
if (success) {
// do sth
} else {
// handle error
}
}];
There are many tutorials on the web, here are some examples that I used for learning:
- Beginning iCloud in iOS 5 Tutorial Part 1
- Beginning iCloud in iOS 5 Tutorial Part 2
UIDocument Class Reference may also help.
来源:https://stackoverflow.com/questions/8630361/syncing-a-plist-file-with-icloud