Syncing a .plist file with iCloud

拜拜、爱过 提交于 2020-01-01 12:10:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!