I want my app to be loaded on the same view every time it is fully loaded out of memory, so when the app is closed and then opened again, it opens the same view (currently it's one of the tabs in UITabBar). I'd like to see code, not just description on how to do this, because I'm not very experienced in the iOS development. Thanks in advance!
1 Answers
If your tab is a main menu, for example, that you always want to be loaded when the app is run, then consider not allowing multitasking by adding the following key in your app's Info.plist
file:
Application does not run in background - (tick the box)
or:
UIApplicationExitsOnSuspend - set to YES (BOOL)
This will ensure that your app quits when the home key is pressed and the UIApplicationDelegate
method applicationwillterminate:
is called.
EDIT:
Okay, I think you need to keep track of your particular tab and/or the tab that the user is on, whenever this changes. Save this value in NSUserDefaults (plist) and check it each time the app starts up or becomes active - then you can switch to that tab if it's not onthe correct one.
Is this what you are trying to do?
EDIT 2:
First step, wherever you listen out for button presses on your tab bar, you need to grab the integer value of the new selected index, like in the accepted answer here:
Switching to a TabBar tab view programmatically?
Then, you need to write this value (store/save it) to a .plist file. First, add a new property list file to your project, then open it in Xcode and add a new row, call it something simple like SelectedTab
with a type of Number and say a value of 1337.
In your app delegate, you need to tell your app that you have a new .plist that you need to be registered with the app, so in application:didFinishLaunchingWithOptions:
you need to do the following:
NSUserDefaults* userPreferences = [NSUserDefaults standardUserDefaults];
[userPreferences registerDefaults: [NSDictionary dictionaryWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"myNewFile" ofType: @"plist"]]];
Here's how you can read and write to your SelectedTab
plist integer:
To get the stored value...
int test = [[NSUserDefaults standardUserDefaults] integerForKey: @"SelectedTab"];
To write a new value to your key...
int newTab = 2;
[[NSUserDefaults standardUserDefaults] setInteger: newTab forKey: @"SelectedTab"];
[[NSUserDefaults standardUserDefaults] synchronize]; // absolutely critical!
You just need to pick the right place in your app delegate to perform this check, and then tell your tab bar to load the tab you want.
Hope this helps!
-
Unfortunately, this is not the case. I want my app to support multitasking, and I also want it to load a specific view, to pop it somehow or something. And this tab is not the main view, not the Initial View Controller. – Sergey Grischyov May 28 '12 at 19:37
-
Yeah, that's right! But I don't quite get where EXACTLY should I write the code to load the certain view, and how this code looks like. – Sergey Grischyov May 28 '12 at 19:41
-
This looks just great! Thank you so much for helping me out! – Sergey Grischyov May 28 '12 at 20:08
-
Feel free to keep me posted with progress :) Happy to help. – Luke May 28 '12 at 20:16