首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >苹果iCloud在苹果应用上的实现

苹果iCloud在苹果应用上的实现
EN

Stack Overflow用户
提问于 2011-12-04 22:15:00
回答 2查看 2.4K关注 0票数 6

我知道iOS有应用程序集成iCloud的应用程序接口。我可以在苹果应用程序中集成iCloud吗?Mac应用程序集成iCloud的实现方式会有所不同吗?如果是,是否有任何教程等或参考网站?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-12-04 23:18:28

是。iCloud可以在苹果电脑上使用。

但是苹果关于这个主题的文档仍然不是很完整。我能找到的唯一官方资源是WWDC2011 session 107 video"What's new in Mac OS X"中的一些注释

当Lion & iCloud还在保密协议下的时候,我把我的发现发布在了苹果的开发论坛上。

这是this post的编辑版本

我使用的是WWDC 2011会话107代码的修改版本。(从视频中转录)我不得不删除NSFileCoordinator的手动实例化以使样本正常工作(演讲者提到协调员“未来可能不再需要”):

代码语言:javascript
复制
- (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中,或者是否需要上载:

代码语言:javascript
复制
- (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文档存储正常工作所需的非代码任务的检查表:

  • 检查是否在开发人员证书实用程序中激活了iCloud支持
  • 在开发人员证书实用程序中创建一个普遍容器ID
  • 普遍容器ID以您的团队ID /个人ID开头(请参阅Xcode中的成员center)
  • Enable权利文件中的帐户选项卡
  • 将您的普遍容器ID添加到权利文件中(如此处所述“请求普遍容器的权利iCloud Storage.”)
  • My plist捆绑包ID必须与普遍容器ID匹配(团队ID除外)
  • 我无法添加后缀(例如"app.example.osx","app.example.ipad",...按照文档above)
  • Create a provisioning profile
  • 中的建议,请确保该配置文件已安装在您的开发计算机上,并显示在应用程序构建设置

中的Xcode & System code signing中

票数 4
EN

Stack Overflow用户

发布于 2012-01-31 07:03:58

有一份苹果公司的文档在all aspects http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/iCloud/iCloud.html中有非常详细的介绍

这里发布的代码的一个重要问题是,即使在检查表中提到了URLForUbiquityContainerIdentifier,它在给定的标识符中也没有团队,让它完全为空,从授权中自动填充似乎是最好的方法。

就我个人而言,为了让iCloud在我的应用程序中运行,我必须做的唯一改变是:

我的应用程序id的开发者网站上的

  1. 选中"use iCloud“按钮
  2. 下载该应用程序id的重新生成的配置
  3. 在xcode

中选中"enable enable entitlements”

以上就是所有内容,下面是一个更清晰的示例代码(应该可以同时适用于iOS和OSX):

代码语言:javascript
复制
NSURL *url = [self getiCloudURLFor:@"foo.bar" containerID:nil]; //leaving nil so it is auto filled from entitlements
if (url) {
    NSError *error;
    if (![[NSFileManager defaultManager] startDownloadingUbiquitousItemAtURL:url error:&error]) {
        NSLog(@"Error downloading/syncing %@ (%@)",[url path],[error description]);                
    }else{
        NSLog(@"Started downloading/syncing %@",[url path]);              
    }         
}

NSArray *conflicts = [NSFileVersion unresolvedConflictVersionsOfItemAtURL:url];
for (NSFileVersion *conflict in conflicts) {
    NSLog(@"Conflicting %@ at %@ by %@ from %@",[url path],[conflict URL],[conflict localizedNameOfSavingComputer],[conflict modificationDate]);   
}

- (NSURL*)getiCloudURLFor:(NSString*)fileName containerID:(NSString*)containerID
{   
    NSFileManager *fm = [NSFileManager defaultManager];  

    NSURL *rootURL = [fm URLForUbiquityContainerIdentifier:containerID];
    if (rootURL) {
        NSURL *directoryURL = [rootURL URLByAppendingPathComponent:@"Documents"];
        if (![fm fileExistsAtPath:[directoryURL path]]) [fm createDirectoryAtURL:directoryURL withIntermediateDirectories:NO attributes:nil error:NULL];
        NSURL *cloudURL = [directoryURL URLByAppendingPathComponent:fileName];
        if (![fm isUbiquitousItemAtURL:cloudURL]) [self makeUbiquitousItemAtURL:cloudURL];//this only runs once per filename when it is first added to iCloud
        return cloudURL;
    }else{
        return [[[fm URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] objectAtIndex:0] URLByAppendingPathComponent:fileName]; //no cloud
    }     
    return nil;
}

- (void)makeUbiquitousItemAtURL:(NSURL*)cloudURL
{
    NSFileManager *fm = [NSFileManager defaultManager];

    NSURL *localURL = [[[fm URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] objectAtIndex:0] URLByAppendingPathComponent:[cloudURL lastPathComponent]]; 
    if (![fm fileExistsAtPath:[localURL path]]) [fm createFileAtPath:[localURL path] contents:nil attributes:nil];
    NSError *error;            
    if(![fm setUbiquitous:YES itemAtURL:localURL destinationURL:cloudURL error:&error])  {
        NSLog(@"Error making %@ ubiquituous at %@ (%@)",[localURL path],[cloudURL path],[error description]);
    }else{
        NSLog(@"Made %@ ubiquituous at %@",[localURL lastPathComponent],[cloudURL path]);               
    }      
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8375891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档