我對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