问题
I'd like to know if my app is coming foreground by user's clicking the app icon.(doesn't matter whether it's fresh start or becoming active from inactive)
But not from incoming call, in-app-purchase or anything else.
(I found applicationDidBecomeActive getting called during in-app-purchase process)
回答1:
Step 1:
When application is started and wasn't in the background before (suspended), application:didFinishLaunchingWithOptions:
will execute first. This method carries launchOptions
parameter - when it's nil
, then your app was launched via icon tap in Springboard. Otherwise launchOptions
will indicate the reason app was started (URL-scheme, Push Notification etc... more in documentation).
Step 2:
So far, so good. Now let's take care of resuming. When an app is resumed (or started), at some point it will call applicationDidBecomeActive
in app's delegate. The trick is that this method is called after all possible reasons due to application can be resumed (started) were serviced. So all you need to do is introduce a BOOL
flag that you'll set in methods servicing the reason your app was resumed and check it later in applicationDidBecomeActive
against expected value.
A list (incomplete, I guess) of methods where your flag needs to be set:
application:handleOpenURL:
application:openURL:sourceApplication:annotation:
application:didReceiveLocalNotification:
application:didReceiveRemoteNotification:
The rest of methods you'll find in documentation mentioned above. And remember that applicationDidBecomeActive
for Step 1 will also be called.
Good luck!
来源:https://stackoverflow.com/questions/7785722/how-to-detect-if-user-actively-relaunched-the-app-by-clicking-the-app-icon