2

I'm developing an app with initial view controller as "Navigation controller". The app contains 2 ideas to implement.

1) From the RegisterViewController, User has to register the mobile no with verification //from sms or call, After verified it enters into HomeViewController( Condition: when the user install the app for the 1st time it shows the RegisterViewController) enter image description here

2) If the app is already get installed into the iPhone and the user also register the mobile no, then the user opens the same app now the initialViewController must be the HomeViewController

enter image description here

How can I achieve this idea through swift code?

2
  • There are too many ways to implement this. 1. Add a modelview controller for registration in HomeView. In HomeView's viewwillApper check for login. If 'No' present your registration screen. 2. If you do not want to add any ModelView controller. Then in ViewWillApper of RegitrationView set [self.navigationCOntroller pushtoYourHomeScreen] Commented Mar 29, 2016 at 12:35
  • use NSUSerdefault and chek condition Commented Mar 29, 2016 at 12:35

3 Answers 3

2

Implement this logic in your appdelegate class:

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
    }

Then when first time register completed successfully then set bool variable true for the key ALLREADY_REGISTER

NSUserDefaults.standardUserDefaults().setBool(true, forKey: "ALLREADY_REGISTER")`
Sign up to request clarification or add additional context in comments.

1 Comment

how can i make the rootViewController as Navigation controller because now i setted that as initial view controller
1

do like

  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
}

on registration page after the success save like

//Save
NSUserDefaults.standardUserDefaults().setObject("registred", forKey: "notRegistered")

1 Comment

I am not familier with swift, if u need a support I surely help u
0

you have to change rootViewController instead of initialViewController. when user verified and enters into HomeViewController store flag in NSUserDefaults.then every time in AppDelegate didFinishLaunchingWithOptions check user is already entered in HomeViewController using NSUserDefaults.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.