我正在尝试找出如何在我的Sandboxed应用程序的“应用程序支持”文件夹中同步我的.plist文件.我知道我可以使用iCloud密钥值存储,但每个应用程序有64KB的限制,根据用户添加到应用程序的数量,可能会或可能不会被命中!
我尽可能多地阅读了Apple文档,但我仍然很困惑:(
有没有人做过类似的事情?
谢谢
解决方法
您应该创建UIDocument的子类并将其与ubiquity目录一起使用.
有两种方法负责处理读/写.这个在阅读时被调用:
- (BOOL)loadFromContents:(id)contents ofType:(NSString *)typeName error:(NSError **)outError
写这篇文章时:
- (id)contentsForType:(NSString *)typeName error:(NSError **)outError
所有打开/保存操作都会自动调用,您无需执行任何操作.但是,有一些强制打开/保存的方法.打开时调用此方法:
- (void)openWithCompletionHandler:(void (^)(BOOL success))completionHandler /* --- EXAMPLE --- */ MyDocument *doc = [[MyDocument alloc] initWithFileURL:ubiquitousFileURL]; [doc openWithCompletionHandler:^(BOOL success) { if (success) { // do sth } else { // handle error } }];
……保存时这个:
- (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 } }];
网上有很多教程,这里有一些我用来学习的例子:
> Beginning iCloud in iOS 5 Tutorial Part 1
> Beginning iCloud in iOS 5 Tutorial Part 2
UIDocument Class Reference也可以提供帮助.