我正在开发一个带有初始视图控制器的应用程序作为"导航控制器"。该应用程序包含2个实现的想法。
1)从RegisterViewController,用户必须通过sms或调用来验证移动号码//验证后进入HomeViewController(条件:用户第一次安装应用程序时显示RegisterViewController)
2)如果应用程序已经安装到iPhone并且用户也注册了手机号码,那么用户现在打开相同的应用程序,而initialViewController必须是HomeViewController
如何通过快速代码实现这一想法?
答案 0 :(得分:2)
在appdelegate类中实现此逻辑:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let isRegistered = NSUserDefaults.standardUserDefaults().boolForKey("ALLREADY_REGISTER")
if isRegistered == true{
// implement home view controller
let homeViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("HomeVC") as! HomeViewController
self.window?.rootViewController = homeViewController
self.window?.makeKeyAndVisible()
}else{
// implement register view controller
let registerViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("RegisterVC") as! RegisterViewController
self.window?.rootViewController = registerViewController
self.window?.makeKeyAndVisible()
}
return true
}
然后,当第一次成功注册成功完成后,为键ALLREADY_REGISTER
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "ALLREADY_REGISTER")`
答案 1 :(得分:1)
喜欢
var viewController: UIViewController!
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
var storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
if NSUserDefaults.standardUserDefaults().objectForKey("notRegistered") == nil
{
viewController = storyboard.instantiateViewControllerWithIdentifier("RegisterViewController")
}
else {
if NSUserDefaults.standardUserDefaults().objectForKey("notRegistered") as! String == "registred"
{
viewController = storyboard.instantiateViewControllerWithIdentifier("HomeViewController")
}
else
{
viewController = storyboard.instantiateViewControllerWithIdentifier("RegisterViewController")
}
// if you want to create the UINavigationController use this
let nav = UINavigationController(rootViewController: viewController)
self.window!.rootViewController = nav
else directly access use this
self.window!.rootViewController = viewController
self.window.makeKeyAndVisible()
// Override point for customization after application launch.
return true
}
成功保存后在注册页面上
//Save
NSUserDefaults.standardUserDefaults().setObject("registred", forKey: "notRegistered")
答案 2 :(得分:0)
您必须更改rootViewController
而不是initialViewController。当用户验证并在NSUserDefaults
中输入HomeViewController商店标志时,每次AppDelegate
didFinishLaunchingWithOptions
检查用户都已使用NSUserDefaults
在HomeViewController中输入。
let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var homeViewController = mainStoryboard.instantiateViewControllerWithIdentifier("HomeViewController") as! HomeViewController
let nav = UINavigationController(rootViewController: homeViewController)
appdelegate.window!.rootViewController = nav