iPhone DropBox API:如何加载文件? [英] iPhone DropBox API: How to load a file?

查看:133
本文介绍了iPhone DropBox API:如何加载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于dropBox集成到iPhone应用程序的一个非常基本的问题。



我按照DropBoxSDK的设置,一切正常。我可以登录到我的帐户,并获得链接。所以我正确设置了一切。



现在我想使用它来从dropBox加载一个文件,并再次保存。考虑你只想同步一个文件(为了简单起见),名为'example.txt',它位于我的DropBox中的'Example'文件夹中。同样的'example.txt'保存在我的应用程序的Documents目录中的本地iPhone上。



dropBox自述文件暗示以下代码,我发现高度神秘并且无法真正看到如何加载或保存文件:

  2。在其余客户端上发出请求:

[[self restClient] loadMetadata:@/];

3.实现DBRestClientDelegate方法,以获取
特定调用的结果:

- (void)restClient:(DBRestClient *)client
loadedMetadata:(DBMetadata *)metadata {

NSLog(@Loaded metadata!
}

- (void)restClient:(DBRestClient *)client
metadataUnchangedAtPath:(NSString *)path {

NSLog !);
}

- (void)restClient:(DBRestClient *)client
loadMetadataFailedWithError:(NSError *)error {

NSLog metadata:%@,error);所以我的(有希望的)简单的问题是我可以:




  • 检查我的收存箱中是否有示例文件夹

  • 如果没有,请创建一个文件夹并保存example.txt从程序文档到此示例文件夹

  • 加载example.txt

  • 一旦程序退出:将example.txt保存到DropBox



我在网站上的DropBox文档中找不到这些相当基本步骤的答案。他们提供的例子我发现太混乱了,特别是因为它只是加载文件,而不是保存到我可以看到。



解决方案

好的,我找到这个方法来保存我的例子。 txt文件:

   - (void)DBupload:(id)sender 
{
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString * documentsDirectory = [paths objectAtIndex:0]; //获取文档目录
NSString * filePath = [documentsDirectory stringByAppendingPathComponent:@Example.txt];

[self.restClient uploadFile:@NoteBook.txttoPath:@/ examplefromPath:filePath];
}

原来,无需创建文件夹,Dropbox会自动为



这是用于下载相同文件形式的dropbox:

   - (void)DBdownload:(id)sender 
{
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString * documentsDirectory = [paths objectAtIndex:0]; //获取文档目录
NSString * filePath = [documentsDirectory stringByAppendingPathComponent:@Example.txt];
NSError * error;

[self.restClient loadFile:@/ example / Example.txtintoPath:filePath];

if(filePath){//检查文件是否存在 - 如果加载它:
NSString * tempTextOut = [NSString stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error :& error];
}
}

希望这有助于如果你在一个类似的问题。


A very basic question concerning dropBox integration into an iPhone app.

I followed the setup of the DropBoxSDK and everything works fine. I can log on to my account and get it linked. So I set up everything correctly.

Now I would like to use it to simply load a file from the dropBox and save it again. Consider that you only want to sync ONE FILE (for the sake of simplicity), called 'example.txt' which is located in the 'Example' folder in my DropBox. The same 'example.txt' is saved locally on the iPhone in the Documents directory of my app.

The dropBox readme file suggest vaguely the following code which I find highly cryptic and can't really see how to load or save a file:

2. Make an request on the rest client:

   [[self restClient] loadMetadata:@"/"];

3. Implement the DBRestClientDelegate methods needed to get the results of the
   particular call you made:

   - (void)restClient:(DBRestClient*)client 
       loadedMetadata:(DBMetadata*)metadata {

       NSLog(@"Loaded metadata!");
   }

   - (void)restClient:(DBRestClient*)client 
       metadataUnchangedAtPath:(NSString*)path {

       NSLog(@"Metadata unchanged!");
   }

   - (void)restClient:(DBRestClient*)client 
       loadMetadataFailedWithError:(NSError*)error {

       NSLog(@"Error loading metadata: %@", error);
   }

So my (hopefully) simple question is how I can:

  • check if there is an example folder in my dropbox
  • if not, create one and save the example.txt from app documents into this example folder
  • load example.txt
  • once programme quits: save example.txt to DropBox

I can't really find an answer to these quite basic steps in the DropBox docs on the website. The example they've provided I find too confusing... especially as it is only about loading files and not saving them as far as I can see.

I'd be grateful for any help or suggestions of how to go about this.

解决方案

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.

这篇关于iPhone DropBox API:如何加载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆