Kiran Bhoraniya
Kiran Bhoraniya

Reputation: 103

How to manage state in ios objective c

I’m new in iOS development. My question is, I’ve two view controllers.

viewController - A viewController - B

Now, if i killed the app from the viewController - A and than relaunch the app. than app must be open the viewController - A. and if i killed the app from the viewController - B and than relaunch the app. than app must be open the viewController - B.

Can anyone help me, I’ve done the RND but can not find the proper solution.

Thanks

Upvotes: 1

Views: 549

Answers (3)

Jagveer Singh
Jagveer Singh

Reputation: 2328

  1. Create a sharedDelegate in AppDelegate.m file
+(AppDelegate *)sharedDelegate {
    return (AppDelegate *) [UIApplication sharedApplication].delegate;
}
  1. in AppDelegate.h
+ (AppDelegate *)sharedDelegate;
@property (nonatomic, strong) NSString *currentViewContoller;
  1. when push to any contoller then set AppDelegate's currentViewContoller to new VC
YourViewController *vc=[[YourViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];

[AppDelegate sharedDelegate].currentViewContoller = NSStringFromClass([YourViewController class]);
  1. now when app is terminated
- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    [[NSUserDefaults standardUserDefaults]setObject:[AppDelegate sharedDelegate].currentViewContoller forKey:@"currentVC"];
}
  1. now when app launched first time check previous controller when app terminated
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSString *string=[[NSUserDefaults standardUserDefaults] valueForKey:@"currentVC"];

and push this class

    UIViewController *object = [[NSClassFromString(string) alloc] init...];
}

Upvotes: 1

Luca D'Alberti
Luca D'Alberti

Reputation: 4859

If you're using Storyboards you can use the Restoration Identifier to communicate the App which controller to launch as first

https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/PreservingandRestoringState.html

Upvotes: 0

Ketan Parmar
Ketan Parmar

Reputation: 27448

applicationWillTerminate you can use but it will only get called if user quit app from forground If your app is in background and then user quit app then applicationWillTerminate will not get called.

So, you have to take care of applicationDidEnterBackground.

So, when app enter in background (i.e call applicationDidEnterBackground ) or call applicationWillTerminate save state(your current VC) in your user defaults.

Now in your didFinishLaunchingWithOptions set that view controller as rootviewcontroller or whatever way that you want to manage it.

reference : Apple documentation for applicationWillTerminate

PS : You should not manage app like this. It is horrible way!! If possible then run your app as normal flow!

Upvotes: 0

Related Questions