How do you save text files permanently to an iPhone? And then be able to open them again in another UITextView?
Thanks in advance.
To write a NSString to a file:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"file.txt"]; NSString *str = @"hello world"; [str writeToFile:filePath atomically:TRUE encoding:NSUTF8StringEncoding error:NULL];
To load NSString from a file:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"file.txt"]; NSString *str = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
And use text
property of UITextView to set and get NSString.
NSString *str = myTextView.text; myAnotherTextView.text = str;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With