EBRAHIM
EBRAHIM

Reputation: 43

How To Save Appllication State And Restore It AGAIN

i use navigation controller , i have 6 navigation controller i want the app to save state and restore the last screen the application terminate on . to open it when it launch again
what is the code i must use to do that in any view .

Upvotes: 1

Views: 3177

Answers (1)

jjxtra
jjxtra

Reputation: 21170

Apple provides mechanisms to do this: https://developer.apple.com/documentation/uikit/view_controllers/preserving_your_app_s_ui_across_launches?language=objc

The important bits from the link:

State preservation and restoration is not an automatic feature and apps must opt-in to use it. Apps indicate their support for the feature by implementing the following methods in their app delegate:

application:shouldSaveApplicationState:
application:shouldRestoreApplicationState:

Normally, your implementations of these methods just return YES to indicate that state preservation and restoration can occur. However, apps that want to preserve and restore their state conditionally can return NO in situations where the operations should not occur. For example, after releasing an update to your app, you might want to return NO from your application:shouldRestoreApplicationState: method if your app is unable to usefully restore the state from a previous version.

Preserving the State of Your View Controllers

Preserving the state of your app’s view controllers should be your main goal. View controllers define the structure of your user interface. They manage the views needed to present that interface and they coordinate the getting and setting of the data that backs those views. To preserve the state of a single view controller, you must do the following:

(Required) Assign a restoration identifier to the view controller; see “Marking Your View Controllers for Preservation.” (Required) Provide code to create or locate new view controller objects at launch time; see “Restoring Your View Controllers at Launch Time.” (Optional) Implement the encodeRestorableStateWithCoder: and decodeRestorableStateWithCoder: methods to encode and restore any state information that cannot be recreated during a subsequent launch; see “Encoding and Decoding Your View Controller’s State.”

In addition to the data preserved by your app’s view controllers and views, UIKit provides hooks for you to save any miscellaneous data needed by your app. Specifically, the UIApplicationDelegate protocol includes the following methods for you to override:

application:willEncodeRestorableStateWithCoder:
application:didDecodeRestorableStateWithCoder:

Upvotes: 5

Related Questions