如何使用 iCloud 将 NSDocument 从 Mac osx 同步到 iPad/iPhone
我已经看到了 iOS 的 iCloud Document 示例代码,我用它来同步 uidocument 与 iCloud 之间,现在我正在尝试将 iCloud 与 ns文档在没有 UIDocument
的 Mac OSX 应用程序上。
我尝试将 UIDocument
更改为 NSDocument
但与 icloud 是不同的。除了 中的文档之外,我还没有找到任何示例代码或教程apple 这非常令人困惑并且写得不好。
例如,以下方法,来自 iOS 上的 UIDocument
,在 OS X 上的 NSDocument
中不存在:
//doc is an instance of subclassed UIDocument
[doc openWithCompletionHandler:nil];
Apple 文档为 OS X 提供了此代码:
- (void)checkIfCloudAvaliable {
NSURL *ubiquityContainerURL = [[[NSFileManager defaultManager]
URLForUbiquityContainerIdentifier:nil]
URLByAppendingPathComponent:@"Documents"];
if (ubiquityContainerURL == nil) {
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
NSLocalizedString(@"iCloud does not appear to be configured.", @""),
NSLocalizedFailureReasonErrorKey, nil];
NSError *error = [NSError errorWithDomain:@"Application" code:404
userInfo:dict];
[self presentError:error modalForWindow:[self windowForSheet] delegate:nil
didPresentSelector:NULL contextInfo:NULL];
return;
}
dest = [ubiquityContainerURL URLByAppendingPathComponent:
[[self fileURL] lastPathComponent]];
}
- (void)moveToOrFromCloud {
dispatch_queue_t globalQueue =
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(globalQueue, ^(void) {
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSError *error = nil;
// Move the file.
BOOL success = [fileManager setUbiquitous:YES itemAtURL:[self fileURL]
destinationURL:dest error:&error];
dispatch_async(dispatch_get_main_queue(), ^(void) {
if (! success) {
[self presentError:error modalForWindow:[self windowForSheet]
delegate:nil didPresentSelector:NULL contextInfo:NULL];
}
});
});
[self setFileURL:dest];
[self setFileModificationDate:nil];
}
如何在 iOS 之间同步和 OS X(因为 iOS 上不存在 NSDocument
,并且 OS X 上不存在 UIDocument
)?有谁知道我在哪里可以找到 Mac OSX 的示例(NSDocument
同步)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我设法让它工作!这是我在 OS X 上子类 nsdocument 文件中的代码:
头文件:
实现文件:
以及AppDelegate.m文件中:
我唯一没有完成的事情是,如果我更改iPad上文档的数据,Mac应用程序不会调用
readFromData
方法要从 iCloud 更新,有人知道我缺少什么吗?在 iOS 上,每当 iCloud 中
UIDocument
发生更改时,都会自动调用等效方法loadFromContents
。在 OS X 上,readFromData
在加载时调用一次,但不会再次调用。希望我的代码能有所帮助,对我来说,它是从 Mac 到 iPad 的一种工作方式。
I managed to get it working! Here's my code from the subclassed nsdocument file on OS X:
Header file:
Implementation file:
and in the AppDelegate.m file:
The only thing that I haven't got working is that if I change the data of the document on the iPad, the Mac app doesn't call the
readFromData
method for to update from iCloud, does anyone know what I am missing?On iOS, the equivalent method,
loadFromContents
, is called automatically on every change of theUIDocument
in iCloud. On OS X thereadFromData
is called once on load but never called again.Hope my code can help, for me it is working one way from Mac to iPad.
我认为
NSMetadataQueryDidUpdateNotification
是您正在寻找的来检测文档更新的内容。这可以像 NSMetadataQueryDidFinishGatheringNotification 一样使用。
I think
NSMetadataQueryDidUpdateNotification
is what you are looking for to detect document update.This can be used just like
NSMetadataQueryDidFinishGatheringNotification
.如果您要处理除核心数据之外的文件。这是一个更好的教程
http://samvermette.com/312
If you are dealing with files in addition to core data. Here is a better tutorial
http://samvermette.com/312