我知道iOS有应用程序集成iCloud的应用程序接口。我可以在苹果应用程序中集成iCloud吗?Mac应用程序集成iCloud的实现方式会有所不同吗?如果是,是否有任何教程等或参考网站?
发布于 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的手动实例化以使样本正常工作(演讲者提到协调员“未来可能不再需要”):
- (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文档存储正常工作所需的非代码任务的检查表:
中的Xcode & System code signing中
发布于 2012-01-31 07:03:58
有一份苹果公司的文档在all aspects http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/iCloud/iCloud.html中有非常详细的介绍
这里发布的代码的一个重要问题是,即使在检查表中提到了URLForUbiquityContainerIdentifier,它在给定的标识符中也没有团队,让它完全为空,从授权中自动填充似乎是最好的方法。
就我个人而言,为了让iCloud在我的应用程序中运行,我必须做的唯一改变是:
我的应用程序id的开发者网站上的
中选中"enable enable entitlements”
以上就是所有内容,下面是一个更清晰的示例代码(应该可以同时适用于iOS和OSX):
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]);
}
}https://stackoverflow.com/questions/8375891
复制相似问题