Reputation: 103
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
Reputation: 2328
+(AppDelegate *)sharedDelegate {
return (AppDelegate *) [UIApplication sharedApplication].delegate;
}
+ (AppDelegate *)sharedDelegate;
@property (nonatomic, strong) NSString *currentViewContoller;
YourViewController *vc=[[YourViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
[AppDelegate sharedDelegate].currentViewContoller = NSStringFromClass([YourViewController class]);
- (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"];
}
- (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
Reputation: 4859
If you're using Storyboards
you can use the Restoration Identifier
to communicate the App which controller to launch as first
Upvotes: 0
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