No, you don't have to enable a setting to use the documents directory. There might or might not be an error with your code, in any case it's hard to read and not very clean (apologies for saying this.)
Try to do everything just once. I took the liberty of rewriting your code a bit, to clean it up. I then tested it with a sample app on my phone, and it works perfectly.
Code rewrite:
-(void) saveFile:(NSString*)filename{
if (![filename isEqual:@"Empty"]) {
NSString *fullFilePath = [self getFullFilePath:filename];
[[NSFileManager defaultManager] removeItemAtPath: fullFilePath error: nil];
NSLog(@"File: %@", fullFilePath);
//save content to the documents directory
NSLog(@"Saving: %@", self.codeView.text);
// Logs the data i want to store
[[self.codeView.text dataUsingEncoding:NSASCIIStringEncoding] writeToFile:fullFilePath atomically:YES];
}
}
The above code is fine, after adding the helper function.
-(void) loadFile:(NSString*)filename{
NSString *fullFilePath = [self getFullFilePath:filename];
if ([[NSFileManager defaultManager] fileExistsAtPath:fullFilePath]) {
NSLog(@"File found");
//Yes the file is found
NSLog(@"Data:%@",[[NSString alloc] initWithData:[NSData dataWithContentsOfFile:fullFilePath] encoding:NSASCIIStringEncoding]);
// On device and simulator data is found
[theDelegate.codeView setText:[[NSString alloc] initWithData:[NSData dataWithContentsOfFile:fullFilePath] encoding:NSASCIIStringEncoding]];
[theDelegate setTitle:filename];
[theDelegate setHasOpenFile:YES];
[theDelegate.codeView setEditable:theDelegate.hasOpenFile];
[theDelegate.codeView setNeedsDisplay];
//[self setLanguage:filename];
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"File error!" message:@"An error occured when trying to load the selected file." delegate:self cancelButtonTitle:@"OK!" otherButtonTitles:nil, nil];
[alert show];
}
}
In your old code, there might have been a problem with theDelegate.fileData = [NSData dataWithContentsOfFile:file];
, if the reference is weak. Since I presume you'll always have the code in codeView, it seems unnecessary to first store it in a member variable. Also, this might lead to more bugs.
The following is a helper function, so that you don't do the exact same thing in both functions, as it might lead to bugs.
-(NSString*) getFullFilePath:(NSString*)filename {
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullFilePath = [documentsDirectory stringByAppendingPathComponent:filename];
return fullFilePath;
}