Swift: How to Implement a Login Storyboard

Swift: How to implement a login storyboard?

Have you tried switching your rootViewController in application(_:didFinishLaunchingWithOptions:)

Assuming Mainstoryboard has your rootViewController:

if userIsNotLoggedIn {
let storyboard = UIStoryboard(name: "Loginstoryboard", bundle: nil)
let loginController = storyboard.instantiateViewControllerWithIdentifier("LoginNavigationController") as UINavigationController
window?.rootViewController = loginController
}

To switch view controllers once logged in you can do this:

func loggedIn() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainController = storyboard.instantiateViewControllerWithIdentifier("MainVC") as UIViewController
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
appDelegate.window?.rootViewController = mainController
}

iOS Swift Storyboard how to show view after Login and before Login

You can set the initial view controller programmatically by unchecking the "Is initial view controller" option from your first view controller like so,
or simply by deleting the arrow pointing to your first view controller.
Then give all view controllers that you want as initial view controllers a different storyboard ID, Like that.

Then set the appropriate view controller to be the initial view controller manually in application:didFinishLaunchingWithOptions method in AppDelegate class:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.mainScreen().bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
window?.rootViewController = storyboard.instantiateViewControllerWithIdentifier((isUserLoggedIn ? "MainVC" : "LoginVC"))
window?.makeKeyAndVisible()
return true
}

Sign in with apple using story board [SWIFT - Xcode11]

Below are the steps to define a custom class for a generic view in Storyboard

Sample Image

Best practices for Storyboard login screen, handling clearing of data upon logout

Here is what I ended up doing to accomplish everything. The only thing you need to consider in addition to this is (a) the login process and (b) where you are storing your app data (in this case, I used a singleton).

Storyboard showing login view controller and main tab controller

As you can see, the root view controller is my Main Tab Controller. I did this because after the user has logged in, I want the app to launch directly to the first tab. (This avoids any "flicker" where the login view shows temporarily.)

AppDelegate.m

In this file, I check whether the user is already logged in. If not, I push the login view controller. I also handle the logout process, where I clear data and show the login view.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

// Show login view if not logged in already
if(![AppData isLoggedIn]) {
[self showLoginScreen:NO];
}

return YES;
}

-(void) showLoginScreen:(BOOL)animated
{

// Get login screen from storyboard and present it
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
LoginViewController *viewController = (LoginViewController *)[storyboard instantiateViewControllerWithIdentifier:@"loginScreen"];
[self.window makeKeyAndVisible];
[self.window.rootViewController presentViewController:viewController
animated:animated
completion:nil];
}

-(void) logout
{
// Remove data from singleton (where all my app data is stored)
[AppData clearData];

// Reset view controller (this will quickly clear all the views)
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
MainTabControllerViewController *viewController = (MainTabControllerViewController *)[storyboard instantiateViewControllerWithIdentifier:@"mainView"];
[self.window setRootViewController:viewController];

// Show login screen
[self showLoginScreen:NO];

}

LoginViewController.m

Here, if the login is successful, I simply dismiss the view and send a notification.

-(void) loginWasSuccessful
{

// Send notification
[[NSNotificationCenter defaultCenter] postNotificationName:@"loginSuccessful" object:self];

// Dismiss login screen
[self dismissViewControllerAnimated:YES completion:nil];

}

iOS Login / Sign Out Implementation in Swift

The < Back button is appearing because you are doing a push segue from the login view controllers to the tab bar controller. A better flow for the app would be to have the tab bar controller be your initial view controller. Then, in its viewDidAppear method, check that the user is logged in. If the user isn't logged in, segue modally WITHOUT animation to your login view controller. This will all happen without the user noticing and will allow the storyboard setup you want

Create page after login xcode

after you check that the user enters the right credentials set function to call the new view controller

func transition() {   
let mapViewController:MapViewController = MapViewController()

self.presentViewController(mapViewController, animated: true, completion: nil)

}

then call it

transition()


Related Topics



Leave a reply



Submit