且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何通过单击应用程序图标来检测用户是否主动重新启动了该应用程序?

更新时间:2023-01-25 22:06:57

步骤1:
当应用程序启动并且之前不在后台(挂起)时,application:didFinishLaunchingWithOptions:将首先执行.此方法带有launchOptions参数-当它为nil时,则通过Springboard中的图标点击启动您的应用.否则,launchOptions将指示启动应用程序的原因(URL方案,推送通知等...更多信息,位于

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).

第2步:
到目前为止,一切都很好.现在,让我们开始恢复.恢复(或启动)应用程序时,它有时会在应用程序的委托中调用applicationDidBecomeActive.诀窍是,在可以恢复(启动)应用程序的所有可能原因后,将调用此方法.因此,您需要做的就是引入一个BOOL标志,您将在服务恢复应用程序原因的方法中设置该标志,并稍后在applicationDidBecomeActive中将其与期望值进行比较.

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:
  • 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.

祝你好运!