iPhone DropBox API: How to load a file?

狂风中的少年 提交于 2019-12-02 18:37:10
n.evermind

Ok, I found this method to save my example.txt file:

-(void) DBupload:(id)sender
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Example.txt"];

    [self.restClient uploadFile:@"NoteBook.txt" toPath:@"/example" fromPath:filePath];
}

Turns out, no need to create a folder, dropbox will do this automatically for you if it doesn't exist.

This is for downloading the same file form dropbox:

-(void) DBdownload:(id)sender
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Example.txt"];
    NSError *error;

    [self.restClient loadFile:@"/example/Example.txt" intoPath:filePath];

    if (filePath) { // check if file exists - if so load it:
        NSString *tempTextOut = [NSString stringWithContentsOfFile:filePath
                                                          encoding:NSUTF8StringEncoding
                                                             error:&error];
    }
}

Hope this helps if you were struggling with a similar question.

Into the DBdownload function you can skip the check by implementing the DBRestClientDelegate method loadedFile and loadFileFailedWithError

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!