当应用程序在SWIFT中进入前台时,如何呈现Lock_ViewController?

时间:2015-05-11 09:30:19

标签: ios xcode swift uiapplication

请帮帮我。我有一个具有密码锁定功能的应用程序,每次用户打开应用程序时都会激活此功能。如果用户回家(应用程序转到后台),然后返回应用程序(前台),我想再次显示锁定屏幕(或使用其他视图控制器)。我怎么会这样做?

我知道appDelegate中的WillEnterForeground,我无法弄清楚如何告诉我的应用程序呈现锁定屏幕而不是之前进入后台的屏幕。

此应用与照片库应用程序非常相似。使用锁定屏幕保护照片,每次打开应用程序时都会向用户询问密码

非常感谢你提前

3 个答案:

答案 0 :(得分:9)

有几个库可用

https://github.com/antiraum/THPinViewController

https://github.com/D-32/DMPasscode

https://github.com/tiny2n/JKLLockScreenViewController

如果您希望自己或在实现它的过程中,在willEnterForground方法中获取最顶层的控制器,在该控制器上显示您的锁屏控制器,成功检查引脚,关闭您的锁屏控制器,如果您需要通知别人,推动通知 这就是你如何获得最顶层的控制器

- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
     if ([rootViewController isKindOfClass:[UITabBarController class]]) {
        UITabBarController* tabBarController = (UITabBarController*)rootViewController;
        return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
     } else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController* navigationController = (UINavigationController*)rootViewController;
        return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
     } else if (rootViewController.presentedViewController) {
        UIViewController* presentedViewController = rootViewController.presentedViewController;
        return [self topViewControllerWithRootViewController:presentedViewController];
     } else {
        return rootViewController;
    }
}

<强>夫特

func topViewControllerWithRootViewController(rootViewController: UIViewController!) -> UIViewController! {

    if rootViewController.isKindOfClass(UITabBarController) {
        let tabbarController =  rootViewController as! UITabBarController
        return self.topViewControllerWithRootViewController(tabbarController.selectedViewController)
    }else if (rootViewController.isKindOfClass(UINavigationController)) {
        let navigationController = rootViewController as! UINavigationController
        return self.topViewControllerWithRootViewController(navigationController.visibleViewController)
    }else if ((rootViewController.presentedViewController) != nil){
        let controller = rootViewController.presentedViewController
        return self.topViewControllerWithRootViewController(controller)
    }else {
        return rootViewController
    }

}

<强>用法

    let topController = self.topViewControllerWithRootViewController(UIApplication.sharedApplication().delegate?.window??.rootViewController)

修改并制作扩展程序或按需使用

答案 1 :(得分:1)

除了答案

swift 3.0:

func topViewControllerWithRootViewController(rootViewController: UIViewController!) -> UIViewController! {

    if rootViewController is UITabBarController {
        let tabbarController =  rootViewController as! UITabBarController
        return self.topViewControllerWithRootViewController(rootViewController: tabbarController.selectedViewController)
    } else if rootViewController is UINavigationController {
        let navigationController = rootViewController as! UINavigationController
        return self.topViewControllerWithRootViewController(rootViewController: navigationController.visibleViewController)
    } else if ((rootViewController.presentedViewController) != nil){
        let controller = rootViewController.presentedViewController
        return self.topViewControllerWithRootViewController(rootViewController: controller)
    } else {
        return rootViewController
    }

}

<强>用法:

let topController = self.topViewControllerWithRootViewController(rootViewController: UIApplication.shared.delegate?.window??.rootViewController)

答案 2 :(得分:0)

除了答案

Swift 4(有扩展名)

在您的app delegate -

// Show the pin view controller when app becomes active
func applicationDidBecomeActive(_ application: UIApplication) {

    if shouldAskForPin {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let loginVC = storyboard.instantiateViewController(withIdentifier: "PinViewController")
        let topVC = self.window?.rootViewController?.topViewController()
        topVC!.present(loginVC, animated: true, completion: nil)
    }
}

如果应用在显示引脚视图时进入后台,当应用再次变为活动状态时,引脚视图控制器将再次添加到其上,依此类推。所以在辞职时将其删除。确保在PIN视图控制器的故事板(或代码)中设置了恢复标识符。

func applicationWillResignActive(_ application: UIApplication) {
    if shouldAskForPin {
        let topVC = self.window?.rootViewController?.topViewController()
        if topVC!.restorationIdentifier == "PinViewController" {
            topVC!.dismiss(animated: false, completion: nil)
        }
    }
}

最后,根据上面的答案使其工作的扩展。通常可以放在单独的文件中,例如的UIViewController + Extension.swift

import UIKit

extension UIViewController {

    func topViewController() -> UIViewController! {
        if self.isKind(of: UITabBarController.self) {
            let tabbarController =  self as! UITabBarController
            return tabbarController.selectedViewController!.topViewController()
        } else if (self.isKind(of: UINavigationController.self)) {
            let navigationController = self as! UINavigationController
            return navigationController.visibleViewController!.topViewController()
        } else if ((self.presentedViewController) != nil){
            let controller = self.presentedViewController
            return controller!.topViewController()
        } else {
            return self
        }
    }

}