swift - 从后台恢复应用程序时如何返回应用程序登录屏幕?

标签 swift xcode6

我有一个具有多个 View 的应用程序。每当应用程序从后台恢复时,我希望该应用程序显示在登录屏幕上。我怎样才能做到这一点? 我试图修改 AppDelegate.swift,但我不知道应该添加哪些代码来切换 View 。

(以下代码无效)

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.
    let vc: AnyObject? = self.storyboard?.instantiateViewControllerWithIdentifier("loginSID")
    self.presentViewController(vc as UIViewController, animated: true, completion: nil)
}

最佳答案

下面是我自己的一个应用程序中的一些代码,它就是做这件事的。每当用户退出并返回时,它都会显示一个身份验证屏幕。显然,并非所有内容都与您相关,但我敢打赌其中大部分内容都与您相关。如果您愿意,我可以分享更多内容并展示我如何实现 AuthViewController 等。

import UIKit
import SQLite

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate
{
  var window: UIWindow?
  var database: Database
  var authenticated: Bool = false
  var password: String = ""

  override init()
  {
    let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as String

    database = Database("\(path)/db.sqlite3")
  }

  class func shared() -> AppDelegate!
  {
    return UIApplication.sharedApplication().delegate as AppDelegate
  }

  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
  {
    return true
  }

  func applicationWillResignActive(application: UIApplication)
  {
    authenticated = false
  }

  func applicationDidEnterBackground(application: UIApplication) {}

  func applicationWillEnterForeground(application: UIApplication)
  {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let rootController = storyboard.instantiateViewControllerWithIdentifier("authView") as AuthController

    self.window?.rootViewController?.dismissViewControllerAnimated(false, completion: {
      if self.window != nil
      {
        self.window!.rootViewController = rootController
      }
    })
  }

  func applicationDidBecomeActive(application: UIApplication) {}

  func applicationWillTerminate(application: UIApplication)
  {
    authenticated = false
  }

}

关于swift - 从后台恢复应用程序时如何返回应用程序登录屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27954126/

相关文章:

ios - 将相机胶卷图像导入 UIView 背景

swift - {(Int)} 与 UInt8 不同

xcode - 未找到 RCT 链接管理器文件

ios - UITableViewDiffableDataSource - 项目移动到另一个部分时的动画转换

ios - 在 Swift 中按名称获取实例变量

ios - 从 png 文件中读取(自定义)EXIF 数据

应用程序中的 ios 路径问题缺少 libidn-1.15.tar.gz

ios - Apple Watch Simulator 完全无法启动或加载应用程序,消息为 "Waiting to attach"

objective-c - 带有 iPhone5(8.0) 模拟器的 xcode 6 - 当我运行项目时显示 Clang 错误

ios - 如何在 IOS 中访问下载的文件?