macos - Mac 应用程序上的 Apple iCloud 实现

标签 macos icloud

我知道 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 :
  • 检查是否在开发者证书实用程序中激活了 iCloud 支持
  • 在开发者证书实用程序中创建一个普遍存在的容器 ID
  • Ubiquity 容器 ID 以您的团队 ID/个人 ID 开头(请参阅成员(member)中心的帐户选项卡)
  • 在 Xcode 中启用权利
  • 将您的 ubiquity 容器 ID 添加到权利文件(如此处“为 iCloud 存储请求权利”中所述。)
  • 我的 plist Bundle ID 必须匹配 ubiquity 容器 ID(团队 ID 除外)
  • 我无法添加后缀(例如,“app.example.osx”、“app.example.ipad”、...如上述文档中所建议的那样)
  • 创建配置文件
  • 确保配置文件已安装在您的开发机器上并显示在 Xcode 和系统偏好设置
  • 在您的应用build设置中启用代码签名
  • 关于macos - Mac 应用程序上的 Apple iCloud 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8375891/

    相关文章:

    macos - Swift 中的授权

    python - python 的多个错误解释器问题(即使使用 virtualenv)

    ios - 如何在使用照片框架从照片中选取图像时隐藏选取器中的 iCloud 图像

    ios - NSUbiquitousKeyValueStore 同步问题

    ios - 将应用程序恢复到新设备时,iOS 钥匙串(keychain)存储是否会持续存在?

    macos - "GNU Make not found. Try setting the environment variable GNUMAKE."

    macos - Firefox:哪个选项卡占用了 CPU

    objective-c - 如何在 cocoa macosx 中以编程方式创建 View

    ios - iCloud:在 iOS 和 tvOS 之间同步键值存储

    ios - 如何处理(MyApp保存的文档)文件?