我知道 iOS 有 API 可以为应用程序集成 iCloud。
我也可以将 iCloud 集成到 Mac 应用程序中吗?
Mac 应用程序集成 iCloud 的实现会有所不同吗?
如果是,是否有任何教程等或引用网站?
最佳答案
是的。 iCloud 在 Mac 上可用。
但是 Apple 关于这个主题的文档仍然不是很完整。我能在 WWDC 2011 session 107 video 找到的唯一官方资源以及 "What's new in Mac OS X" 中的一些注释
当 Lion 和 iCloud 仍处于 NDA 之下时,我在 Apple 的开发论坛上发布了我的发现。
这是 this post 的编辑版本:
我正在使用 WWDC 2011 Session 107 代码的修改版本。 (转录自视频)
我必须删除 NSFileCoordinator 的手动实例化才能使示例正常工作(演讲者提到“将来可能不需要协调器”):
- (IBAction)moveToOrFromCloud:(id)sender
{
NSFileManager* fm = [NSFileManager defaultManager];
NSURL* fileURL = [[self document] fileURL];
BOOL shouldMakeUbiquitous = [sender tag] == 1;
NSURL* destinationURL;
if(shouldMakeUbiquitous)
{
NSURL* rootURL = [fm URLForUbiquityContainerIdentifier:@"app.example"];
NSURL* directoryURL = [rootURL URLByAppendingPathComponent:@"Documents"];
[fm createDirectoryAtURL:directoryURL withIntermediateDirectories:NO attributes:nil error:NULL];
destinationURL = [directoryURL URLByAppendingPathComponent:[fileURL lastPathComponent]];
}
else
{
destinationURL = [[[fm URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] objectAtIndex:0] URLByAppendingPathComponent:[fileURL lastPathComponent]];
}
NSError* error;
if(![fm setUbiquitous:shouldMakeUbiquitous itemAtURL:fileURL destinationURL:destinationURL error:&error])
{
[[self document] presentError:error modalForWindow:[[self document] windowForSheet] delegate:nil didPresentSelector:NULL contextInfo:NULL];
}
else
{
[[self document] setFileURL:destinationURL];
[[self document] setFileModificationDate:nil];
}
}
上面的 IBAction 连接到一个 NSMenuItem 来检查文档是否已经在 iCloud 中或者是否需要上传:
- (BOOL)validateMenuItem:(NSMenuItem*)item
{
SEL action = [item action];
if (action == @selector(moveToOrFromCloud:))
{
BOOL isUbiquitous = [[NSFileManager defaultManager] isUbiquitousItemAtURL:[[self document] fileURL]];
[item setTitle:isUbiquitous ? @"Remove from Cloud": "Move to Cloud"];
[item setTag:isUbiquitous?0:1];
return [self.document fileURL] != nil;
}
return YES;
}
使 iCloud 文档存储正常工作所需的非代码任务 list :
关于macos - Mac 应用程序上的 Apple iCloud 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8375891/