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

时间:2015-01-14 23:05:52

标签: swift xcode6

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

(以下代码不起作用)

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

1 个答案:

答案 0 :(得分:1)

以下是来自我自己的一个应用程序的代码,它完成了这件事。它会在用户切换并返回时显示验证屏幕。显然并非所有这些都与你相关,但我会打赌很多。如果您愿意我可以分享更多内容并展示我如何实现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
  }

}