Every time my application starts (whether from a cold start or background), I want it to do something, how to do it?

I want to check every time the application launches if there is a URL in the clipboard, and if so, do something with it. Which method works, I can redefine whenever the application starts, whether it is a cold start (for example, it was killed in the background), or if I just press the home button, copy the URL and go back.

Is this one of them?

- (void)applicationDidBecomeActive:(UIApplication *)application - (void)applicationWillEnterForeground:(UIApplication *)application - (void)applicationDidBecomeActive - (void)applicationDidFinishLaunching:(UIApplication *)application 

Confused

+4
source share
4 answers

As @rmaddy says, the correct method to use after starting the application is applicationWillEnterForeground: from your application delegate. This method will be called when the user jumps back, but NOT in other circumstances that you do not need to respond to (for example, the user receives a text message and rejects it).

However, from my testing, applicationWillEnterForeground: not called when the application starts from a cold; you must catch this in applicationDidFinishLaunchingWithOptions:

So basically, your application delegate should include this code:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [self checkForURL]; ... } - (void)applicationWillEnterForeground:(UIApplication *)application { [self checkForURL]; ... } - (void)checkForURL{ //code for checking for URL goes here } 

Hope this helps.

+13
source

- (void)applicationDidBecomeActive is called when the application starts or is activated from the background.

Everything is well said in this document: http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html

+1
source

As for the UIApplicationDelegate protocol, there are two ways to handle application launches:

  • application:willFinishLaunchingWithOptions:
  • application:didFinishLaunchingWithOptions:

And control of launching applications from the background can be handled using the method: applicationDidBecomeActive:

Based on the above call, you can process your application.

0
source

In your application delegate, add it to methods that suggested other answers (applicationDidFinishLaunchingWithOptions :). Register with your root controller for the next notification. This will always be called when your application starts after it is already running.

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(bringingItBack) name:UIApplicationWillEnterForegroundNotification object:nil]; 

This will cover both instances when the application starts and when you just return it from the background.

0
source

All Articles