Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Problem with Reading and Writing to plist

racharambola5racharambola5 Posts: 60Registered Users
Hey All,

I am trying to implement a simple plist example from "Beginning iPhone 3 Development book". I looked into the code but my data was never saved to a plist file. Actually my project site map is as follows: Whenever you launch the app it fires in TestViewController. On the TestViewController, there is a button. When you click on the button it pushes another view controller which is PersistenceViewController and here is the code I wrote in PersistenceViewController. My doubt: is the applicationWillTerminate being called in this method? I don't think so..please help guys. I am learning how to persist the data now. I am using iOS4

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];

}

Thanks for any valuable suggestions...
Post edited by racharambola5 on

Replies

  • Duncan CDuncan C Posts: 8,032Tutorial Authors, Registered Users
    racharambola5;266748 said:
    Hey All,

    I am trying to implement a simple plist example from "Beginning iPhone 3 Development book". I looked into the code but my data was never saved to a plist file. Actually my project site map is as follows: Whenever you launch the app it fires in TestViewController. On the TestViewController, there is a button. When you click on the button it pushes another view controller which is PersistenceViewController and here is the code I wrote in PersistenceViewController. My doubt: is the applicationWillTerminate being called in this method? I don't think so..please help guys. I am learning how to persist the data now. I am using iOS4

    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];

    }

    Thanks for any valuable suggestions...

    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.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • racharambola5racharambola5 Posts: 60Registered Users
    Thanks for the reply Duncan.. What I did is in my info.plist file I checked Application doesnot run in background option and in my view controller, I wrote the applicationWillTerminate logic in applicationDidEnterBackground method. It worked but I am not sure whether the approach is correct.
    Please 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
Sign In or Register to comment.