首页 文章

应用程序从后台返回前景时的密码视图

提问于
浏览
0

我正在开发一个应用程序,当应用程序首次启动或从后台返回到前台时需要密码 . 首次发布很容易 . 但是当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)

这不起作用 .

2 回答

  • 0

    您可以使用app delegate的方法来处理此问题 .

    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
    }
    
    }
    

    您可以使用app delegate中的上述方法获取顶视图控制器并显示在其上 .

  • 0

    使用pwview创建一个pwViewController,并从当前视图控制器中显示它

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

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

相关问题