Syncing a .plist file with iCloud

只愿长相守 提交于 2019-12-04 10:18:17

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:

UIDocument Class Reference may also help.

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