In my application, creating functionality in which require to upload document of type (pdf/xls/doc/image). After uploading the document that document name should show in screen and on click of that document it show the document which i have selected.
How to implement this functionality, which is the best practise to do this.
Plz provide your feedback.
You can create file using below code. Here it is txt file.
NSString *stringToWrite = #"This is my string";
NSError *error;
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:#"filename.txt"];
[stringToWrite writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
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);
I have created one project like news update and i have saved in UITableView using JSON api to retrieving the data. now i'm getting problem like if internet is not there means records will not show to the user. now i'm planning to create data base like .sqlite. friends please help in crating data base. i'm new to this
Why not just simply save the json to a text file:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *filePath = [docDir stringByAppendingPathComponent:filePath];
[jsonString writeToFile:filePath atomically:YES];
and then read it:
NSString *jsonString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
here is the link that will useful for beginners like you..Database with ios
cheers
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.)
How would I go about creating a new text file in my program so that I can write content to it? I already understand how to write content but I haven't been able to find anything (anything easy to understand) on the subject.
Use this method:
+ (id)stringWithContentsOfFile:(NSString *)path
usedEncoding:(NSStringEncoding *)enc
error:(NSError **)error
An example:
NSString* path = [[NSBundle mainBundle] pathForResource:#"Example"
ofType:#"txt"];
NSError *error = nil;
NSString* content = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:&error];
if(error)
{
NSLog(#"ERROR while loading from file: %#", error);
}
Write to a file works this way:
Use this method:
- (BOOL)writeToFile:(NSString *)path
atomically:(BOOL)useAuxiliaryFile
encoding:(NSStringEncoding)enc
error:(NSError **)error
Example:
[text writeToFile:path atomically:NO encoding:NSUTF8StringEncoding error:nil];
Mistake:
Can't just write to bundle path. Need to copy it to Documents Directory.
-(void)copyBundleToDocuments
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
NSString *documentPlistPath = [documentsDirectory stringByAppendingPathComponent:#"Example.txt"];
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSString *bundlePlistPath = [bundlePath stringByAppendingPathComponent:#"Example.txt"];
NSError *error;
BOOL success = [fileManager copyItemAtPath:bundlePlistPath toPath:documentPlistPath error:&error];
if (success)
{
[text writeToFile:documentPlistPath atomically:NO encoding:NSUTF8StringEncoding error:nil];
}
}
Your question is so broad one could drive a truck (or lorry, in UK English) through the middle of it.
But in general, you could add a UITextView to one of your view controllers.
And when you are ready to save, you could take the contents of the text view (which is a NSString), and save it to a file via the NSString writeToFile methods.
And you can load the text view later on via NSString's "initFromFile" method, as long as you know the path to that file.
Here are other questions that people have asked that may help you out.
You can use UITextView to write text
save the file using
[txtView.text writeToFile:filePath atomically:NO encoding:NSUTF8StringEncoding error:nil];
Get the TextFile content using
NSString* content = [NSString stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error:NULL];
So in my app I have a bunch of data that I'd like to write to a log file, and then display it within a UITextView when I click a button. I know how to toggle the UITextView, but I have no idea how to create and update a log file (in the local filesystem). Thanks for any help.
The basic idea is that you create the file, and append to it every time you log a new line. You can do it quite easily like this:
Writing to the file:
NSString *content = #"This is my log";
//Get the file path
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:#"myFileName.txt"];
//create file if it doesn't exist
if(![[NSFileManager defaultManager] fileExistsAtPath:fileName])
[[NSFileManager defaultManager] createFileAtPath:fileName contents:nil attributes:nil];
//append text to file (you'll probably want to add a newline every write)
NSFileHandle *file = [NSFileHandle fileHandleForUpdatingAtPath:fileName];
[file seekToEndOfFile];
[file writeData:[content dataUsingEncoding:NSUTF8StringEncoding]];
[file closeFile];
Reading:
//get file path
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:#"myFileName.txt"];
//read the whole file as a single string
NSString *content = [NSString stringWithContentsOfFile:fileName encoding:NSUTF8StringEncoding error:nil];
I thought was a class out there to do this automatically as after no luck created my own.
NSLogger is a lightweight class for iOS versions 3.0 and above. It allows developers to easily log different 'events' over time which are locally stored as a .txt file.
https://github.com/northernspark/NSLogger