开发者

Reading and Writing to plist (iPhone Programming)

开发者 https://www.devze.com 2023-01-25 09:23 出处:网络
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 fol

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. I am learning how to persist the data now.

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 addObje开发者_如何学JAVAct: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...


applicationWillTerminate is called when the user quits the application (usually by pressing the Home button). Any code in this delegate method will be executed (if it doesn't take too much time). In your case, the Plist file will be saved.

If you are using iOS 4, pressing the Home button may send your application into the background (not quitting). If the application is killed using the debugger or crashes, that method will not be called.

Additional Information:

On iOS 4, multitasking is enabled in Xcode projects by default. This prevents the applicationWillTerminate method from being called. If you do not want to support multitasking, place UIApplicationExitsOnSuspend in your MyAppName-Info.plist file and check the checkbox. If you do want to support it, place any code in the applicationDidEnterBackground delegate method that you want to execute before the application enters an inactive state.


applicationWillTerminate: will not be called on multitasking devices (iOS4) if app is suspended and then killed via the multitasking UI. According to Apple: "apps are not aware of any transitions into or out-of the suspended state". So if you're saving anything inside applicationWillTerminate: try to do it in applicationWillResignActive:.


I might be mistaken here, but doesn't applicationWillTerminate have to be coded into the app's delegate.m file, rather than in some other .m file? Regardless, this might not matter due to the suspended state of apps in iOS 4.

0

精彩评论

暂无评论...
验证码 换一张
取 消