I would like to continue processing even if I finish the application by pressing the home button twice.

Asked 2 years ago, Updated 2 years ago, 35 views

We are developing it with Objective-C in Xcode.
I'm creating a simple app that says that when I press the button, the number increases, but when I turn off the app, the number count returns to the initial value.
What should I do to make it continue even if I drop the app?

The source code is listed below.

CounterViewController.h

IBOutlet UILabel* display; 
int count;// variable declaration 

- (IBAction) add;

CounterViewController.m

-(IBAction)add{count=count+1;display.text=[NSString stringWithFormat:@"%d", count]; }

ios objective-c xcode

2022-09-29 22:02

1 Answers

Now, if you want to keep the count as an instance variable, as in your code example:

  • Save the value of the instance variable before the app exits
  • When the app starts, set the saved value to the instance variable, if any.

You will add the processing that says so.
(The current iOS ViewController provides a convenient way to save/restore the state, but does not work if you press the home button twice.Apple's idea is that if a user intentionally shuts down the app, it should start from its initial state next time.If you're going to start creating a full-scale app, you might want to consider it.)

For your code, the count is an instance variable in ViewController, so it's easy to see viewWillDisappear: before the app exits and viewWillAppear: when the app starts.

Add those two methods to CountViewController.m.

#define MyViewControllerCount@"MyViewControllerCount"

- (void) viewWillAppear: (BOOL) animated {
    super viewWillAppear: animated;

    // Check if there are any saved values before ViewController
    NSUserDefaults* defaults = NSUserDefaults standardUserDefaults;
    if([defaults objectForKey:MyViewControllerCount]) {
        count=(int) [defaults integerForKey: MyViewControllerCount];
    } else{
        count = 0;
    }
    display.text = [NSString stringWithFormat: @"%d", count ];
}

- (void) viewWillDisappear: (BOOL) animated {
    super viewWillDisappear: animated;

    // Save value before ViewController disappears
    NSUserDefaults* defaults = NSUserDefaults standardUserDefaults;
    Defaults setInteger: (NSInteger) count for Key: MyViewControllerCount;
}

In addition, we used NSUserDefaults because only one integer value was saved here, but it is not decided that NSUserDefaults should be used separately when saving state in the app.If the application develops and the data structure and amount of data increases, you may want to consider Core Data or proprietary archiving.

Regarding the hidden attribute of UIButton that you mentioned in the comment, as I wrote in the comment, "viewWillDisappear: and viewWillAppear: can be used."If you add a little bit of the code above, for example, you can save the hidden state. What do you think?

#define MyViewControllerCount@"MyViewControllerCount"
#define MyViewControllerButtonHidden@"MyViewControllerButtonHidden"

- (void) viewWillAppear: (BOOL) animated {
    super viewWillAppear: animated;

    // Check if there are any saved values when ViewController is displayed
    NSUserDefaults* defaults = NSUserDefaults standardUserDefaults;
    if([defaults objectForKey:MyViewControllerCount]) {
        count=(int) [defaults integerForKey: MyViewControllerCount];
        // If you have MyViewControllerCount data, you should also have MyViewControllerButtonHidden, so don't check.
        // Restore_countButton.hidden
        _countButton.hidden = [defaults boolForKey: MyViewControllerButtonHidden];
    } else{
        count = 0;
    }
    display.text = [NSString stringWithFormat: @"%d", count ];
}

- (void) viewWillDisappear: (BOOL) animated {
    super viewWillDisappear: animated;

    // Save value when ViewController is hidden
    NSUserDefaults* defaults = NSUserDefaults standardUserDefaults;
    Defaults setInteger: (NSInteger) count for Key: MyViewControllerCount;
    Save //_countButton.hidden
    [defaults setBool:_countButton.hidden for Key: MyViewControllerButtonHidden];
}

As you move the app closer to practical use, you'll have more and more states to save.NSUserDefaults is a place to store tens or fewer simple numbers or short strings, so you can change your mind when the app grows larger.


2022-09-29 22:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.