如何通过单击应用程序图标来检测用户是否主动重新启动了该应用程序? [英] How to detect if user actively relaunched the app by clicking the app icon?

查看:94
本文介绍了如何通过单击应用程序图标来检测用户是否主动重新启动了该应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过用户单击应用程序图标来了解我的应用程序是否即将成为前台(无论是重新启动还是从不活动状态变为活动状态)

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)

但不是来自来电,应用内购买或其他任何内容.
(我发现applicationDidBecomeActive在应用内购买过程中被调用)

But not from incoming call, in-app-purchase or anything else.
(I found applicationDidBecomeActive getting called during in-app-purchase process)

推荐答案

步骤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.

祝你好运!

这篇关于如何通过单击应用程序图标来检测用户是否主动重新启动了该应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆