以编程方式将用户发送到设置屏幕后,左上角有一个返回应用按钮:
点按此按钮可让用户返回我的应用。但是,在那时,Application使用与从后台返回时调用的相同方法调用其委托:
applicationDidBecomeActive
和
using (DWH_Entities db = new DWH_Entities())
{
db.Database.CommandTimeout = 300;
...
与此同时,我需要通过点击这个特定的“返回应用”按钮来区分用户是否回到该应用,或者通过以任何其他方式将其发送到后台后简单地输入应用。这有可能吗?
答案 0 :(得分:3)
我相信,默认情况下无法区分。
我的建议是,如果您专注于特定的设置条目更改,只需将新设置的值与applicationDidBecomeActive
中的旧值进行比较。如果有变化,那么您可以区分流量。但是,如果没有变化,则不能。
答案 1 :(得分:2)
您是否开发了两种想要以这种方式连接的应用程序?
离开你的应用程序还有很多方法可以解释:
如果您要详细说明您拥有的用例或您尝试解决的问题,将会有所帮助。
答案 2 :(得分:2)
我建议一个通用的解决方案来解决检测不同启动选项的类似问题(我们的应用程序处于活动状态(运行))
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 e.g: on tap of Open App from App Store when your App is installed or directly from home screen
}
}
}
<强>参考:强> Apple文档提供了可以检测到的所有可用启动选项
https://developer.apple.com/documentation/uikit/uiapplicationdelegate/launch_options_keys
通过添加观察者
来使用委托协议方法的力量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中的类似问题,我帮助您:
Detect when "back to app" is pressed
How to detect user returned to your app in iOS 9 new back link feature?
Detect if the app was launched/opened from a push notification