本文介绍了如何将内存中的NSDictionary上传到Dropbox,IOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



目前我创建了一个NSDictionary并创建了一个临时文件夹,我写了字典作为一个plist到那个临时文件夹,并将那个.plist上传到那个临时文件夹中的dropbox。



这是我不是一个好的编程解决方案;



这工作正常

  if([title isEqualToString:@Upload to Dropbox] )
{


//将内存中的字符串传递给nsdictionary
NSData * data = [_uploadPlistString dataUsingEncoding:NSUTF8StringEncoding];
NSString * errorDesc = nil;
NSPropertyListFormat格式;
NSDictionary * uploadFile =(NSDictionary *)[NSPropertyListSerialization
propertyListFromData:data
mutabilityOption:NSPropertyListMutableContainersAndLeaves
格式:& format
errorDescription:& errorDesc];


//创建一个临时文件夹
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString * documentsDirectory = [paths objectAtIndex:0];
NSString * dataPath = [documentsDirectory stringByAppendingPathComponent:@temp];
NSLog(@documents datapath:%@,dataPath);
//检查文件夹是否存在
NSError * error = nil;
if(![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:& error]; //创建文件夹



//将字典写入plist
NSString * pathTemp = [dataPath stringByAppendingPathComponent:@agenda.plist];
//写plist到磁盘
[uploadFile writeToFile:pathTemp atomically:YES];


//从单身人士获取上次创建的文件名
SingletonClass * sharedInstance = [SingletonClass sharedInstance];
NSString * destDirectory = sharedInstance.lastCreatedFolderName;

//为dropbox设置文件名
NSString * filename = @agenda.plist;
[[self restClient] uploadFile:filename toPath:destDirectory
withParentRev:nil fromPath:pathTemp];


$ b

但是我怎么上传NSDictionary是在内存中,直接到Dropbox?



没有写入捆绑或类似的东西。 Dropbox SDK的Public API只支持直接从文件上传。

然而,由于SDK是开放的你可以很容易地扩展它。



主要的上传发生在

 <$ c $ (NSString *)filename toPath:(NSString *)path fromPath:(NSString *)sourcePath 
params:(NSDictionary *)params

在DBRestClient.m中。

您可以将一个类别添加到DBRestClient并添加一个类似的方法上传时需要一个NSData而不是一个文件路径。

你可以复制他们大部分的实现,而不是一个文件名,你需要一个NSData。改变Dropbox上文件名的生成方式,并用NSData而不是文件实例化NSInputStream。

从字典中获得NSData你可以使用NSKeyedArchiver。

p>

I have an xml string that I want to upload as a .plist to a dropbox folder.

Currently I create a NSDictionary and create a temp folder, I write dictionary as a plist to that temp folder and upload that .plist in that temp folder to dropbox.

Which is I guess not a good programming solution;

This works ok

if([title isEqualToString:@"Upload to Dropbox"])
    {


        //pass string in memory to nsdictionary
        NSData * data = [_uploadPlistString dataUsingEncoding:NSUTF8StringEncoding];
        NSString *errorDesc = nil;
        NSPropertyListFormat format;
        NSDictionary *uploadFile= (NSDictionary*)[NSPropertyListSerialization
                                         propertyListFromData:data
                                         mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                         format:&format
                                         errorDescription:&errorDesc];


        //create a temp folder
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"temp"];
        NSLog(@"documents datapath: %@",dataPath);
        //check if folder exist
        NSError *error = nil;
        if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
            [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder



        //write dictionary to plist
        NSString *pathTemp = [dataPath stringByAppendingPathComponent:@"agenda.plist"];
        // write plist to disk
        [uploadFile writeToFile:pathTemp atomically:YES];


        //get last created file name from singleton
        SingletonClass *sharedInstance=[SingletonClass sharedInstance];
        NSString *destDirectory= sharedInstance.lastCreatedFolderName;

        //set file name for dropbox
        NSString *filename = @"agenda.plist";
        [[self restClient] uploadFile:filename toPath:destDirectory
                        withParentRev:nil fromPath:pathTemp];


    }

But How can I upload NSDictionary that is in memory, directly to Dropbox?

Without writing it to bundle or anything like that.

解决方案

The Public API of the Dropbox SDK only support uploads directly from files.

However, since the SDK is open source you can easily extend it.

The main upload is happening in

- (void)uploadFile:(NSString*)filename toPath:(NSString*)path fromPath:(NSString *)sourcePath
params:(NSDictionary *)params

in DBRestClient.m.

You could add a category to DBRestClient and add a similar method for the upload that takes an NSData instead of a file path.

You can copy most of thier implementation, but instead of a filename you take an NSData. Change how the filename generation on dropbox works and instantiate the NSInputStream with a NSData instead of a file.

To get an NSData from your dictionary you can use an NSKeyedArchiver.

这篇关于如何将内存中的NSDictionary上传到Dropbox,IOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 03:26