Domanda

I'm saving my UITextView text into a file, test.txt

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"test.txt"];
[self.textBox.text writeToFile:path atomically:YES];
NSLog(path);

This works fine on the mac in the simulator, as it provides a library and I can just click on it to open the txt file. However, on the iPad, I"m not sure how to do this.

/var/mobile/Applications/10AC319A-D567-4DC6-B7C9-E53E71B82B89/Documents/test.txt

is where i'ts stored. I'm guessing the documents folder can't be accessed via ipad?

How do I pull this document within the app? Or save it in the app so that the user of the app can access it ?

Aside from the save button, I have another button called Read which should pull up test.txt somehow

I've started with

 -(IBAction)readAssignment:(id)sender{

    NSString* GetApplicationDocumentsDirectory(); {
        static NSString* documentsDirectory = nil;
        if (documentsDirectory == nil) {
            documentsDirectory = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"test.txt"];}

        self.textBox.text= [documentsDirectory stringByAppendingPathComponent:@"text.txt"];
    }

}




self.textBox.text= [documentsDirectory stringByAppendingPathComponent:@"text.txt"];

returns the file path, but how do i get it so that it displays the text.txt text that is saved into the textview?

But I know this isn't working/right.

È stato utile?

Soluzione 2

Using NSString's stringWithContentsOfFile method, you can read back the text from a file.

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"test.txt"];
NSError *error = nil;
[self.textBox.text writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
NSString *myText = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
NSLog(@"myText: %@", myText);

Altri suggerimenti

If you are looking to get the files, outside your application, you can use the below solution.

If you are storing the file in documents directory, you can access the file via iTunes.

For that you need to enable the UIFileSharingEnabled in the info.plist of your application.

Then connect to iTunes -> Sync the application -> Click on your app in iTunes, you can see the files

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top