It looks like you're new here. If you want to get involved, click one of these buttons!
In .h file #define kFilename @\"data2.plist\"
- (NSString *)dataFilePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:kFilename];
return path;
}
- (void)applicationWillTerminate:(NSNotification *)notification {
NSMutableArray *contactFormArray = [[NSMutableArray alloc] init];
NSLog(@\"App Terminate:%d\",[contactFormArray count]);
[contactFormArray addObject:nameField.text];
[contactFormArray addObject:emailField.text];
[contactFormArray addObject:phoneField.text];
[contactFormArray addObject:companyField.text];
[contactFormArray writeToFile:[self dataFilePath] atomically:YES];
[contactFormArray release];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSArray *contactFormArray = [[NSArray alloc] initWithContentsOfFile:filePath];
NSLog(@\"Did Load:%d\",[contactFormArray count]);
nameField.text = [contactFormArray objectAtIndex:0];
emailField.text = [contactFormArray objectAtIndex:1];
phoneField.text = [contactFormArray objectAtIndex:2];
companyField.text = [contactFormArray objectAtIndex:3];
[contactFormArray release];
}
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:)name:UIApplicationWillTerminateNotification object:app];
[super viewDidLoad];
}
Replies
Several things:
-The applicationWillTerminate method is a method of your application delegate. It won't get called from a view controller.
-Under iOS 4, that method rarely (if ever) gets called. Instead, your app delegate gets the notifications applicationWillResignActive and applicationDidEnterBackground
-It's a little risky to wait until your app is being terminated/sent to the background before saving your data. In these methods you don't have very long to finish what you're doing and return. If you're saving lots of data and it takes a while, the system will terminate you. (this is to keep the system responsive when the user clicks the home button.)
-Bear in mind that there are a limited number of object types that can be written to property lists: arrays, dictionaries, strings, NSNumbers, NSDates, and NSData. (I might have forgotten one or 2.) All other objects must be converted to one of those types. If one of the container objects contains an object that isn't a valid "property list type" the save fails.
-It's pretty easy to tell if a block of code is being called. Click on the line number in the editor to add a breakpoint, and select "build and debug" from the debug menu. The app will stop and display information about the state of the program when it hits the breakpoint. Alternately, you can use NSLog statements to display information to the console.
Duncan C
WareTo
Animated GIF created with Face Dancer, available for free in the app store.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomePlease let me know or please give me some advice...thanks a lot for your time
In my case I want to save the values in a small contact form..so this is not at all complicated
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome