Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-17-2010, 03:12 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 60
racharambola5 is on a distinguished road
Default Problem with Reading and Writing to plist

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

Code:
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...

Last edited by racharambola5; 11-17-2010 at 03:28 PM.
racharambola5 is offline   Reply With Quote
Old 11-17-2010, 04:55 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by racharambola5 View Post
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

Code:
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

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 11-18-2010, 10:20 AM   #3 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 60
racharambola5 is on a distinguished road
Default

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
racharambola5 is offline   Reply With Quote
Reply

Bookmarks

Tags
iphone, objective-c, persistence, plist

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 375
7 members and 368 guests
apatsufas, Kirkout, lzwasyc, MarkC, Sami Gh, SamorodovAlex, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,664
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Leslie80
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:44 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0