Reputation: 151
I'd like that each time my app starts up (possibly even when it's restored from background) it makes an action (for example TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];
).
I'd also like that this action (on startup) can be disabled by a switch in the setting bundle of the app. What should I do? Thanks for your attention.
p.s. I apologize if I used incorrect words.. I'm a beginner :)
Upvotes: 1
Views: 251
Reputation: 1070
There are several methods which you can implement in your application delegate which are available in the UIApplicationDelegate
protocol.
When your app is first launched applicationDidFinishLaunching
is called.
When your app is restored from the background applicationWillEnterForeground
is called.
A switch which you add to your setting bundle will have a key, which is an NSString
, associated with it. A switch stores a boolean value encoded as an NSNumber
in the standard NSUserDefaults
under that key. You can read the value of the boolean from the standard user defaults and use it to determine whether to perform the action.
Apples documentation on how to add a settings bundle is here.
In your settings bundle you'll need a toggle switch. The key that you will look up in the standard user defaults is specified by the Key
field. The default value for your toggle switch is specified by the DefaultValue
field. See here
Here is what you need to do in your applicationDidFinishLaunching
method
static NSString *const kTakeActionOnLaunchSettingKey = @"Key";
- (void)applicationDidFinishLaunching
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
BOOL doTakeActionOnLaunch = [userDefaults boolForKey:kTakeActionOnLaunchSettingKey];
if (doTakeActionOnLaunch) {
// Do something
}
}
Upvotes: 2
Reputation: 1128
It's easy and I used it to make a cool transition from the splash screen to the home page. You need to put your code inside the AppDelegate m file.
use
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
to run code at startup.
use the following methods to manage the Backgroud <-> Foreground transitions:
- (void)applicationWillResignActive:(UIApplication *)application
- (void)applicationDidEnterBackground:(UIApplication *)application
- (void)applicationWillEnterForeground:(UIApplication *)application
- (void)applicationDidBecomeActive:(UIApplication *)application
Hope this helps you.
Upvotes: 1
Reputation: 721
Initialise it in voidDidLoad {} method. For disabling it you can use switch from object libary
Upvotes: 1