0

I want to save some texts in textfields whenever the user goes to the background i think i wrote everything correctly since i followed many question/answers. However when I close my app my app doesn't save the text or even create a plist so when i reopen it, the textfields are empty. Here is the code: RootViewController.h:

@interface RootViewController: UIViewController <UITextFieldDelegate> {
UITextField *textField1;
UITextField *textField2;
UITextField *textField3;
UITextField *textField4;
}
@property (nonatomic, retain) UITextField *textField1, *textField2, *textField3, *textField4;
@end

RootViewController.m:

#import "RootViewController.h"
@implementation RootViewController
@synthesize textField1, textField2, textField3, textField4;
...
- (UILabel*)addNewLabel:(NSString*)_text
{
//Initializing TextViews
}
....
- (NSString *) saveFilePathB
{
NSString *path = @"/Applications/AppDissassembler.app/Cache/savefileb.plist";
return path;
}
- (void)loadView {
    self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
self.view.backgroundColor = [UIColor whiteColor];
//creating textfields
self.textField1 = [self addNewTextfield:60:80:175:true:@"Binary Name Here":1];
self.textField2 = [self addNewTextfield:60:145:200:true:@"Offset Here (0xOffset)":2];
self.textField3 = [self addNewTextfield:75:210:175:false:nil:3];
self.textField4 = [self addNewTextfield:75:310:175:false:nil:4];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSArray *values = [[NSArray alloc] initWithObjects:self.textField1.text,self.textField2.text,self.textField3.text,self.textField4.text,nil];
NSFileHandle *fout1;
[[NSFileManager defaultManager] createFileAtPath:[self saveFilePathB] contents:nil attributes:nil];
//open output file for writing
fout1 = [NSFileHandle fileHandleForWritingAtPath:[self saveFilePathB]]; 
[values writeToFile:[self saveFilePathB] atomically:YES];
[values release];

}
- (void)viewDidLoad 
{ 
NSString *myPath = [self saveFilePathB];

BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:myPath];

if (fileExists)
{

    NSArray *values = [[NSArray alloc] initWithContentsOfFile:myPath];
    self.textField1.text = [values objectAtIndex:0];
    self.textField2.text = [values objectAtIndex:1];
    self.textField3.text = [values objectAtIndex:2];
    self.textField4.text = [values objectAtIndex:3];
    [values release];
}

UIApplication *myApp = [UIApplication sharedApplication];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:@"UIApplicationDidEnterBackgroundNotification" object:myApp];
 [super viewDidLoad]; 
} 
- (void)dealloc {
[textField1 release];
[textField2 release];
[textField3 release];
[textField4 release];
[super dealloc];
}
@end

If I change this:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:@"UIApplicationDidEnterBackgroundNotification" object:myApp];

to this:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:myApp];

I get an error which says that UIApplicationDidEnterBackgroundNotification wasn't declared.

Brave Heart
  • 517
  • 3
  • 16
  • `UIApplicationDidEnterBackgroundNotification` is available since iOS 4... That's the only reason it might through an undeclared error, I guess... – Mazyod Nov 02 '12 at 19:24

2 Answers2

0

It wont run in method you try, so in UIApplicationDidEnterBackground delegate. All you have to do is handle your process while going into background with beginBackgroundTaskWithExpirationHandler. See this topic: topic

Community
  • 1
  • 1
edzio27
  • 4,126
  • 7
  • 33
  • 48
  • Problem with code u provided is: since I got no Mac, I make my projects on my iPhone and compile it using mobile terminal. When I compile the code given I get many errors including "UIApplication may not respond to -endBackgroundTask:" – Brave Heart Nov 03 '12 at 12:18
  • You don't need beginBackgroundTaskWithExpirationHandler just to save some values in a file. It is intended for long-running operations and where the app must receive some events, e.g. if it is saving data over the network before going to sleep. – Bryan Nov 03 '12 at 17:12
0

The second version you tried, where UIApplicationDidEnterBackgroundNotification is an identifier not a literal string, is the correct way to use this feature. It should be defined in UIApplication.h.

You say you are compiling on the iPhone itself, so it sounds like the development environment you have there is lacking this definition. I haven't tried that myself, so can't be sure.

Once you have got past the compilation error I would recommend to use NSLog to see where your code has got to, because this is easier than looking for the file to be created.

Bryan
  • 11,398
  • 3
  • 53
  • 78
  • Where would I find the nslog results? – Brave Heart Nov 03 '12 at 17:54
  • It goes in the console log of the device. Various suggestions on how to read that here: http://stackoverflow.com/questions/2634929/objective-c-iphone-can-we-view-console-log-on-device – Bryan Nov 03 '12 at 19:56