所以我花了最后5-6个小时试图让这一功能起作用,我在实现和理解限制方面遇到了一些陷阱。我尝试了几种方法:
方法1:
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var vc : UIViewController
vc = storyboard.instantiateViewController(withIdentifier: "PasscodeLockVC")
self.window?.rootViewController = vc
self.window?.makeKeyAndVisible()
print("app entered foreground")
}
问题:我无法从PasscodeLockVC中删除屏幕(它是密码VC)。
方法2:
public extension UIViewController {
func show() {
let win = UIWindow(frame: UIScreen.main.bounds)
let vc = UIViewController()
vc.view.backgroundColor = .clear
win.rootViewController = vc
win.windowLevel = UIWindowLevelAlert + 1
win.makeKeyAndVisible()
vc.present(self, animated: true, completion: nil)
}
func hide() {
dismiss(animated: true, completion: nil)
}
}
问题:我解雇时会出现黑屏。
**所以我的问题是**有人可以指导我进入前台时锁定屏幕的最佳方法(我知道它在applicationWillEnterForegroud中),但是当我这样做时,它会保留活动的屏幕当应用程序关闭时(因为它是日记应用程序,它将是所有内容)吗?我整天都难过,真的可以用一些方向。
在最新的Xcode上运行