当应用程序从后台返回前台时查看密码

password view when app come back foreground from background

我正在开发一个应用程序,当应用程序首次启动或从后台返回到前台时需要密码。 首次启动很容易。但是当app从后台回到前台,输入密码后,如何回到上次访问的视图? 请帮帮我! 我想在 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)

这行不通。

您可以使用应用委托的方法来处理这个问题。

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
}

}

您可以使用上述方法从应用程序委托中获取顶视图控制器并在其上呈现。

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

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

登录后再次关闭