【问题标题】:password view when app come back foreground from background应用程序从后台返回前台时的密码视图
【发布时间】:2018-05-16 05:45:25
【问题描述】:

我正在开发一个应用程序,当应用程序首次启动或从后台返回到前台时需要密码。 第一次启动很容易。但是当应用程序从后台返回到前台时,输入密码后,如何回到上次访问的视图? 请帮帮我! 我想在 appDelegate 中添加一个密码 UIView ,但我不知道该怎么做。这是正确的方法吗?

let pwview = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
pwview.backgroundColor = UIColor.black
UIApplication.shared.keyWindow?.addSubview(pwview)

这不起作用。

【问题讨论】:

  • 进入后台后打开app应用时是否显示密码视图供用户进入?
  • @rajtharan-g 是的。并且输入正确的密码后,应用可以显示用户上次访问的视图。

标签: ios swift uiview background foreground


【解决方案1】:

您可以使用应用代理的方法来处理此问题。

func applicationWillEnterForeground(_ application: UIApplication) {
   // Called when app comes to foreground
}

extension UIApplication {

class func topViewController(base: UIViewController? = (UIApplication.sharedApplication().delegate as! AppDelegate).window?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
  return topViewController(base: nav.visibleViewController)
}
if let tab = base as? UITabBarController {
  if let selected = tab.selectedViewController {
    return topViewController(base: selected)
  }
}
if let presented = base?.presentedViewController {
  return topViewController(base: presented)
}
return base
}

}

您可以从应用委托中使用上述方法来获取顶视图控制器并在其上方显示。

【讨论】:

  • 是的,我知道我可以用这种方法处理它,但是我不知道如何在所有其他视图的顶部添加 UIView,以及如何返回到用户上次访问的 viewContrllor。
【解决方案2】:

使用 pwview 创建一个 pwViewController 并从当前视图控制器呈现它

viewController.present(pwViewController, animated: true, completion: nil)

然后在你登录时再次关闭它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-22
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    相关资源
    最近更新 更多