问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/13296648/how-to-upload-nsdictionary-in-memory-to-dropbox-ios