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
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).
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
How to Speed Up Updating Relationship Among Tables, After One or Both Tables Are Already Saved
Save & Retrieve Tableviewcell Checkmark Using Nsuserdefaults in Swift
Firebase Uid VS Document-Id and Firestore Rules
Swift - Get File Path of Currently Opened Document in Another Application
Swiftui Present Alert with Input Field
Swift: How to Get Everything After a Certain Set of Characters
How to Compare Just the Time of a Date in Swift
Swiftui - in Sheet Have a Fixed Continue Button That Is Not Scrollable
Session.Datataskwithurl Completionhandler Never Called
Swiftui: How to Make Entire Shape Recognize Gestures When Stroked
Making Swift Generics Play with Overloaded Functions
Why Can't I Mutate a Variable Initially Set to a Certain Parameter When the Func Was Called
Why Is the Alert Triggering Out of Order If Value Is Not Updated