我对DropBox API有些沮丧。它应该是简单而直接的,但我还没有找到简单而简单的解释来说明如何进行简单的同步。与iPhone DropBox API简单同步
我遵循了所有的说明,你可以在自带的DropBox API中找到它。为了测试整个事情,我创建了两个按钮来从DropBox下载文件并将其上传到DropBox。这些文件位于我的应用程序文档文件夹中。
这工作出色:
-(void) DBupload:(id)sender
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"MyExample.txt"];
// NSError *error;
[self.restClient uploadFile:@"MyExample.txt" toPath:@"/MyExamplePath" fromPath:filePath];
}
-(void) DBdownload:(id)sender
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"MyExample.txt"];
NSError *error;
[self.restClient loadFile:@"/myExamplePath/MyExample.txt" intoPath:filePath];
}
不过,我现在想知道如何实现一个简单的同步。现在,我可以手动上传和下载。但我需要什么,以同步是:
- 发现如果在我的应用程序的文件夹或在我的Dropbox文件夹中的MyExample.txt是最近
- 如果在应用程序的文件夹中的TXT是更近:拖放到Dropbox的(覆盖旧的),即打电话给我DBupload方法
- 如果在下拉框中的TXT是更近的:它下载到应用程序文件夹,即打电话给我DBdownload方法
也许我太愚蠢了,但是在某处如何实现dropBox细节如何实现这一点相当简单直接任务是什么?
我知道有this但它并没有给出任何代码示例。
感谢您的任何建议。
编辑
OK,所以我想,我的第一步是找出哪些是Dropbox的发现MyExample.txt的最后修改日期。
我写了一个叫DBsync奇妙方法中,我只是把这个命令:
-(void) DBsync
{
[self.restClient loadMetadata:@"/myExamplePath"];
}
这调用下面的方法,该方法得到的元数据。这是一个建议的回答了这个帖子,我评论了一点出来,从而使其平原发生了什么(如果有更多的哑人喜欢自己:
- (void)restClient:(DBRestClient*)client loadedMetadata:(DBMetadata*)metadata {
NSLog(@"restClient:loadedMetadata function called");
NSEnumerator *e= [metadata.contents objectEnumerator]; // indexes files in dropbox?
DBMetadata *dbObject; // loads metadate we need, e.g. lastModifiedDated
int numberOfFiles = [metadata.contents count]; // counts files in DropBox - I guess we don't really need this
NSLog(@"numberOfFiles %i", numberOfFiles);
while ((dbObject = [e nextObject])) { // this goes through every single file in the DropBox
if (!dbObject.isDirectory) { // this determines whether we are talking about a file or a folder
NSString *fileName = [dbObject.path lastPathComponent]; // this puts the name of the last component, e.g. in /MyExamplePath/MyExample.txt = MyExample.txt into fileName
NSLog(@"File which is currently being checked: %@", fileName);
if ([fileName isEqualToString:@"MyExample.txt"]) {
NSLog(@"Found it: %@", fileName);
NSLog(@"Date last modified: %@", dbObject.lastModifiedDate);
/* to do: call dbupload if dbObject.lastModifiedDate > than your local file*/
}
}
}
}
我会后,下一步一次我管理这样做......
非常感谢。我相应地更新了我的帖子。在我接受你的答案之前,只是一个问题:它必须是if([fileName ...]而不是if(![fileName ... right?我猜这只是一个错字。而且,如何获取我的日期本地文件?您的帮助将非常感谢,我们距离一个很好的教程如何做一个简单的同步只是几步... – 2011-05-11 10:09:06
不用担心日期比较,只是算出如何做到这一点。将相应地更新我的帖子。 – 2011-05-11 14:00:16