2017-06-22 88 views
11

后编程方式发送用户设置屏幕,有一个后端到应用程序按钮,在左上角:返回到应用程序按钮处理程序

enter image description here

点击此按钮,使用户回到我的应用程序。然而,在这一点上应用程序调用其委托与当我们从后台返回正在调用的方法相同:

applicationWillEnterForeground

applicationDidBecomeActive

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

+0

看到https://stackoverflow.com/questions/13447101/bring-previous-app-back-to-the-front-when-user-is-done-with-my-ios -app – Burnie777

+1

如果您是“以编程方式将用户发送到设置屏幕”,那么您是否可以在应用程序之前简单地设置一个标志?然后在'applicationDidBecomeActive'中检查。 – dfd

+0

@dfd用户可以随时离开设置并在任何时候回到应用程序。然后该标志仍然设置为true,而它不应该是 – user3581248

回答

3

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

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

+0

不幸的是,这个解决方案不适合我,因为我发送用户自动锁定设置,并且从我所知的那些,无法从代码读取... – user3581248

2

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

有更为方式离开你的应用程序,然后你描述:

  1. 用户点击home键一次
  2. 用户点击home键两次
  3. 用户按下电源按钮,而应用程序仍处于前景
  4. 在支持3D触摸的设备上,用户在前沿进行3D触摸。
  5. 用户使用“回到应用程序”您所描述的事情
  6. 用户得到通知,并挑选-POP它
  7. 用户转到其他应用程序的通知
  8. 用户打开通知中心,并做动作有
  9. 用户打开控制中心并在那里执行一些操作
  10. 用户在您的应用程序内部使用共享功能或超链接可触发其他应用程序。

我可能会错过某些东西,但是我创建的这个列表有利于显示,区分这个动作可能非常困难。即使你处理其中一个动作也不一定处理所有其他动作。

如果您会告诉更多关于您遇到的用例或您正在尝试解决的问题,将会对您有所帮助。

+0

应用程序的要求我开发包括允许用户更改自动锁定时间的流程(它最初是在Android上开发的,显然您可以通过代码实现此目的)。显然,没有任何选择可以像在Android上那样完成此任务,所以我的想法是以编程方式将用户发送到设置屏幕。但还有另一个要求 - 每当我们回到应用程序,我们应该回到开始屏幕,因为其他屏幕可能包含机密数据。但在上面描述的场景中,我想回到起始屏幕,而不是回到应用程序设置的屏幕。 – user3581248

+1

在这种情况下,您可以在用户移至设置屏幕之前将用户移至开始屏幕。这样你就可以实现你的目标。 –

+0

@JayeshSojitra评论+1。 user381248 - Android *端口*绝不是100%。更糟的是,至少对我(也可能是其他人)来说,是你从来没有把这种类型的细节放在你的问题中。请,这属于你的问题的一部分! (不是Android的一部分 - 可以提供帮助 - 但需要在将iOS用户发送到设置后返回主屏幕*这非常重要。) – dfd

1

我建议解决类似问题检测不同的启动选项(我们的应用程序是如何在活动状态(运行))

斯威夫特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 

} 
} 

} 

参考: 苹果文档提供所有这些可以通过添加被检测

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

使用的委托协议方法电源可用的启动选项观察员

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

斯威夫特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 
     } 

类似的问题在计算器其中我帮你出:

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

Checking launchOptions in Swift 3