I created a file on ios and add it to dropbox sync api to sync with the dropbox account.
Later when I edit the file, the file I edited did not show on dropbox. Here is what I did.
I created a file using ios FileManager like this:
if(![ABUtil fileExist:FILENAME]) {
NSString *st = #"This is a Test";
NSData *file = [st dataUsingEncoding:NSUTF8StringEncoding];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *file = [[paths objectAtIndex:0] stringByAppendingPathComponent:FILENAME];
[[NSFileManager defaultManager] createFileAtPath:
contents:file
attributes:nil];
}
Then I create a dropbox file for it like this:
DBAccount *account = [[DBAccountManager sharedManager] linkedAccount];
if (account) {
DBFilesystem *filesystem = [[DBFilesystem alloc] initWithAccount:account];
[DBFilesystem setSharedFilesystem:filesystem];
if([ABUtil fileExist:FILENAME]) {
DBPath *path = [[DBPath root] childPath:FILENAME];
DBFile *file = [filesystem createFile:path error:nil];
[file addObserver:self block:^(){
NSLog(#"FILE CHANGED");
}];
}
}
Later when I modify the file using on ios like this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *file = [[paths objectAtIndex:0] stringByAppendingPathComponent:FILENAME];
NSString *string = #"Tis is a test";
[string writeToFile:file atomically:YES encoding:NSUTF8StringEncoding error:nil];
The file did not change at dropbox directory!! Please help me how to make the file change sync with the dropbox!!!
EDIT I was mistaken. There is a method on NSString to write its contents to a file. So that part is presumably working. The next step would be to copy that file into Dropbox.
When you finish modifying the local file, you'll want to call writeContentsOfFile to upload that file to Dropbox.
Or you could skip the local file altogether, which is the more common pattern with the Dropbox Sync API. (Take a look at the Notes example that ships with the Sync SDK to see how to edit Dropbox files directly.)
Related
I want to create a .json file and .text file public so that it can be read by NSItemProvider. I want to create file programmatically.
Can you refer this link, that will be helpful to play around file creation, deletion etc...
http://www.ios-developer.net/iphone-ipad-programmer/development/file-saving-and-loading/using-the-document-directory-to-store-files
Use the following code to write/create a .txt file in your app's Documents directory :
NSError *error;
NSString *stringToWrite = #"hello>!!new file created..";
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:#"myfile.txt"];
[stringToWrite writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
To read/fetch the text file:
NSString *str = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
NSLog(#"%#", str);
Hey guys a quick question!
I am making an iPad app which uses the dropbox sync api. Now I am wondering how to play movie files within my dropbox filesystem in the MPMovieplayer. Since the dropbox filesystem is not the same as the local sandbox and I don't know how to retrieve a nsurl of the file I can't play the video.
This is something I tried (my last hope) but it doesn't work:
+(NSString*)loadMovie : (DBPath*)path
{
DBFile *file = [self openFileAtPath:path];
NSData *data = [[NSData alloc] initWithData:[file readData:nil]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *vpath = [documentsDirectory stringByAppendingPathComponent:file.info.path.name];
[[NSFileManager defaultManager] createFileAtPath:vpath contents:data attributes:nil];
return vpath;
}
You'll need to write the file out to local storage first. See https://forums.dropbox.com/topic.php?id=110085 on the Dropbox forum.
So I am using this library: https://github.com/flyingdolphinstudio/Objective-Zip
I implemented it and am trying to take a UIImage and NSString and make it a .png and .txt in the .zip file, respectively.
Now these are my 2 concerns, I am trying to save the *zipFile below to the documents directory.
Now with the dropbox API, how come I can't just provide the file itself and skip the path. It seems like I HAVE to save the .zip to the documents directory first and then get the path so I can then upload it to dropbox. Do I have to do that?
In the ...writeToFile line, I am getting a warning that ZipFile may not respond to writeToFile so how would I properly save it to the documents directory?
Anyway this is the code I have so far:
NSString *filename = [NSString stringWithFormat:#"%#.zip", textField.text];
ZipFile *zipFile= [[ZipFile alloc] initWithFileName:filename mode:ZipFileModeCreate];
//Image
NSString *nameImage = #"Image.png";
NSMutableDictionary *theDictionary = [Singleton sharedSingleton].dictionary;
NSData *data = [theDictionary objectForKey:#"image"];
ZipWriteStream *writeImage = [zipFile writeFileInZipWithName:nameImage compressionLevel:ZipCompressionLevelBest];
[writeImage writeData:data];
[writeImage finishedWriting];
//Text
NSString *nameText = #"Text.txt";
NSData *dataText = [textView.text dataUsingEncoding:NSUTF8StringEncoding];
ZipWriteStream *writeText = [zipFile writeFileInZipWithName:nameText compressionLevel:ZipCompressionLevelBest];
[writeText writeData:dataText];
[writeText finishedWriting];
//Now we HAVE to save it to the documents directory to get it to work with dropbox
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *filePath = [documentsPath stringByAppendingPathComponent:filename]; //Add the file name
[zipFile writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:NULL];
//Save to Dropbox
NSString *zipPath = [[NSBundle mainBundle] pathForResource:textField.text ofType:#"zip"];
[[self restClient] uploadFile:filename toPath:#"/" withParentRev:nil fromPath:zipPath];
So what am I doing wrong here?
Thanks!
It looks to me like ZipFile already writes to a file, so there's no need for something like writeToFile. Just initialize zipFile with the path you want, be sure to close the file at the end ([zipFile close]), and then upload to Dropbox as you would any other file.
I am pretty new to working with Plists and IOS. I am trying to load a tableview of records, and I am trying to load the Tableview from my documents folder in the project. It used to load from the bundle and then I pointed to the documents folder. I removed the plist from my project, and kept it in the documents folder, but I received a copy error: So, please look over my code and give me some idea of how to load from the documents folder in my application. I ran it in debug and it still loading only the 5 records from the plist in the bundle, not the 7 records that reside in my documents folder. Just to note, after I load the initial screen of tableView records, I take the option "+" at the top to load a blank screen to load another record, this I am writing to the document folder and it works, now I want to display those changes on the Master. I also am not certain how to refresh the screen. I have read a bit about "ViewDidLoad", but I am not certain how to use this method. Thank you in advance.
Regards,
NSLog(#"loading data");
self.navigationItem.title=#"Accounts";
// NSString *accountFile = [[NSBundle mainBundle] pathForResource:#"Accounts2" ofType:#"plist"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [paths objectAtIndex:0];
NSString *plistPath = [documentPath stringByAppendingPathComponent:#"Accounts2.plist"];
accounts = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
account = [accounts objectForKey:#"Account"];
number = [accounts objectForKey:#"Number"];
dueDay = [accounts objectForKey:#"DayDue"];
minAmount = [accounts objectForKey:#"MinAmount"];
balance = [accounts objectForKey:#"Balance"];
NSLog(#"data loaded");
First common things that without having plist in bundle how to copy them in to document directory.? so please keep your plist file it to Bundle then copy in to document directory like bellow code:-
BOOL success;
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"yourPlistName.plist"];
success = [fileManager fileExistsAtPath:filePath];
if (success) return;
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingFormat:#"yourPlistName.plist"];
success = [fileManager copyItemAtPath:path toPath:filePath error:&error];
if (!success) {
NSAssert1(0, #"Failed to copy Plist. Error %#", [error localizedDescription]);
}
and you can get this plist file using this bellow line of code:-
NSString *path = filePath;
NSMutableDictionary *plistdictionary = [[NSMutableDictionary alloc]initWithContentsOfFile:path];
you can directly get plist file from Resource (NSBundle) like bellow code:-
NSString* plistPath = [[NSBundle mainBundle] pathForResource:#"yourPlistName" ofType:#"plist"];
NSMutableDictionary *plistdictionary = [[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];
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.