回到应用button处理程序

以编程方式将用户发送到设置屏幕后,左上angular有一个回到应用程序button:

在这里输入图像说明

点击这个button让用户回到我的应用程序。 然而,在这一点上,应用程序调用它的委托与我们从后台取回时调用的方法相同:

applicationWillEnterForeground

applicationDidBecomeActive

同时,我需要通过点击这个特定的“回到应用程序”button来区分用户是否回到了应用程序,或者通过以任何其他方式发送到后台后简单地进入应用程序来区分用户是否回到了应用程序。 这有可能吗?

我相信,默认情况下是无法区分的。

我的build议是,如果您正在关注特定的设置条目更改,则只需将新设置的值与applicationDidBecomeActive的旧设置值进行比较即可。 如果有变化,那么你可以区分stream量。 但是,如果没有变化,你不能。

你开发了两个你想要连接的应用程序吗?

然后你有更多的方式来离开你的应用程序:

  1. 用户点击主页button一次
  2. 用户点击主页button两次
  3. 用户按下电源button,而应用程序仍然在前台
  4. 在支持3D触控的设备上,用户可以在前沿进行3D触控。
  5. 用户使用您描述的“回到应用程序”的东西
  6. 用户得到通知并拣选它
  7. 用户从通知转到其他应用程序
  8. 用户打开通知中心并在那里执行操作
  9. 用户打开控制中心并在那里执行一些操作
  10. 用户在应用程序内部使用共享function或超链接,可以触发其他应用程序。

我可能会想念,但是这个榜单我创造了赞成的performance,区分这个动作可以很难。 即使你处理其中一个动作也不一定处理所有其他动作。

如果你会告诉更多关于你有的用例或者你正在努力解决的问题,将会有所帮助。

我会build议一个通用的解决scheme,以解决类似的问题,检测不同的启动选项(我们的应用程序处于活动状态(运行))

Swift 2.3

在AppDelegate中

  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { if let options = launchOptions{ print(options.description) //These options will give the difference between launching from background or from pressing the back button if (launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] != nil) { //this indicates the app is launched by pressing Push notifications }else if (launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] != nil) { //This indicates the app is launched on tapping the local notifications }else if (launchOptions?[UIApplicationLaunchOptionsSourceApplicationKey] != nil){ //This indicates the App launched from a valid source eg: on tap of Open App from App Store when your App is installed or directly from home screen } } } 

参考:苹果文档提供了所有可用的启动选项,可以检测

https://developer.apple.com/documentation/uikit/uiapplicationdelegate/launch_options_keys

通过添加观察者来使用委托协议方法的强大function

https://developer.apple.com/documentation/uikit/uiapplicationdelegate

Swift 3相当于:

  //adding observer NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive), name: .UIApplicationDidBecomeActive, object: nil) //removing observer NotificationCenter.default.removeObserver(self, name: .UIApplicationDidBecomeActive, object: nil) // callback func applicationDidBecomeActive() { // handle event } 

类似的问题在StackOverFlow中,我帮你:

检测何时按下“回到应用程序”

如何检测用户返回到您的应用程序在iOS 9新的后链接function?

检测应用程序是否从推送通知启动/打开

在Swift 3中检查launchOptions