issue while changing Navigation Root View Controller - ios

I am making an app. In which First User have to sign In then He allowed to use the app.
After successful login I want the user to directly switch to my HomeViewController.
Here is my code tho change navigation root view but it is not working
TermsConditionsController *firstViewController = [[TermsConditionsController alloc]init];
FirstPage *secondViewController = [[FirstPage alloc]init];
firstViewController.navigationController.viewControllers = [NSArray arrayWithObject: secondViewController];
FirstPage *nextScr = (FirstPage *) [self.storyboard instantiateViewControllerWithIdentifier:#"FirstPage"];
[self.navigationController pushViewController:nextScr animated:YES];

Connect your LoginViewController with HomeViewController by using segue.
give the identifier for the segue for ex name:successLogin.
and use this method:
[self performSegueIdentifier:#"successLogin" sender:nil];

Try this code
Note: Change controller name as per your controllers
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UINavigationController *navController;
if ([[NSUserDefaults standardUserDefaults] objectForKey:#"loginUserDetails"]) {
// Already logged in
ProfileViewController *profileVC = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"ProfileVC"];
navController = [[UINavigationController alloc]initWithRootViewController:profileVC];
}else{
// Not Login Yet
LoginViewController *loginVC = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"LoginVC"];
navController = [[UINavigationController alloc]initWithRootViewController:loginVC];
}
self.window.rootViewController = navController;
}
// Write this code when user login successfully
if (/*Login Successfully*/) {
// Then add loginUserDetails in userdefaults.
[[NSUserDefaults standardUserDefaults] setObject:nil forKey:#"loginUserDetails"];// Instead of nil pass here your object
[[NSUserDefaults standardUserDefaults] synchronize];
}

Related

Set rootViewController inside a class of UINavigationController

I have a UINavigationController in my storyboard and two viewControllers which perform the following function:
InitialViewController: this would be the application's home screen.
FirstTimeViewController: is the screen that appears when the user open
the app for the first time.
My UINavigationController has a class that have the following code:
- (void)viewDidLoad {
[super viewDidLoad];
if ([[ReadPlist initWithPlist:#"Configuration.plist" key:#"initialConfiguration"] boolValue]){
FirstTimeViewController *firstTimeController = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"firstTimeView"]; //or the homeController
[self.navigationController pushViewController:firstTimeController animated:NO];
}else{
InitialViewController *initialController = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"initialView"]; //or the homeController
[self.navigationController pushViewController:initialController animated:NO];
}
}
Basically this code verify the .plist file if a particular field is active, if YES means that the application is running for the first time, in this case it calls the corresponding viewController.
But this code is not working and I see a NavigationController with a black view. All I would do is the same thing we do in the interface builder, simply drag a line from the UINavigationController inside a UIViewController and set as "Root View Controller", but in my case I'm trying to do this programmatically.
How I can do this?
When you push to FirstTimeViewController Set Bool (User Default) in controller ViewDidload Or in your Success Code.then after set in your AppDelegate below code.
if(Bool value = Yes)
{
FirstTimeViewController *FS=[[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"FirstTimeViewController"];
UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:FS];
[navController setNavigationBarHidden:YES];
self.window.rootViewController=navController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
}
My answer is
- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)options
{
UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
FirstTimeViewController *firstTimeVC = [navController.storyboard instantiateViewControllerWithIdentifier:#" FirstTimeViewController"];
InitialViewController *initialVC = [navController.storyboard instantiateViewControllerWithIdentifier:#" InitialViewController"];
if ([[ReadPlist initWithPlist:#"Configuration.plist" key:#"initialConfiguration"] boolValue])
{
// FirstTime
navController.viewControllers = [NSArray arrayWithObject:firstTimeVC];
}
else
{
// Initial
navController.viewControllers = [NSArray arrayWithObject:initialVC];
}
[self.window makeKeyAndVisible];
return YES;
}

Implement a login view controller

i'm developing an app in Objective-C with Xcode 7 for iOS 9 compatible devices, and I'm finding some difficulty in the login section. How should my storyboard be to show the login view controller as root view if the user is not already logged in?
In this app, the root view controller is a simple view controller with a segue to a navigation controller that holds two simple view controllers.
The way that I handle login is to have the login view controller embedded in a navigation controller. It is the initial view controller shown when the app runs.
I set a BOOL variable in NSUserDefaults called LoggedIn and check it every time the app is opened in viewDidLoad of the login view controller. If the variable = NO show the login view controller, and if the variable = YES push to the next view controller (animated:NO) so the first thing the user sees is the screen you want them on.
In your login view controller viewDidLoad:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([[defaults valueForKey:#"LoggedIn"] isEqualToString:#"YES"]) {
ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"viewController"];
[self presentViewController:vc animated:NO completion:nil];
}
Set a Bool to NSUserdefaults after a login was successful. Put this Code in your method which is called after a successful login:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
defaults setBool:YES forKey:#"login.state"];
[defaults synchronize];
// go to your next ViewController
Then check in your Appdelegate.m in the didFinishLaunchWithOptions method:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([defaults boolForKey:#"login.state"] == YES) {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
SWRevealViewController *swrvc = [storyboard instantiateViewControllerWithIdentifier:#"revealVC"];
self.window.rootViewController = swrvc;
[self.window.rootViewController.navigationController popViewControllerAnimated:YES];
}
else {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
LoginViewController *lvc = [storyboard instantiateViewControllerWithIdentifier:#"loginVC"];
UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:lvc];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
}
}
If you need any help please ask!

How to push viewcontroller from appdelegate in storyboard inside navigation controller

I am using SWRevealViewController in my project, and I want to open a particular controller when the app receives a notification. I have tried so many solutions but nothing works.
I follow this http://www.appcoda.com/ios-programming-sidebar-navigation-menu/ by using storyboard. My storyboard is designed as below:
When the application receives a notification I want to load Photo view controller within its navigation controller. I tried with the following code in the AppDelegate:
UIStoryboard *st = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
photoViewController *descController = (PhotoViewController*)[st instantiateViewControllerWithIdentifier: #"photoView"];
UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:descController];
SidebarTableViewController *rearViewController = (SidebarTableViewController*)[st instantiateViewControllerWithIdentifier: #"menuController"];
SWRevealViewController *mainRevealController = [[SWRevealViewController alloc] init];
mainRevealController.rearViewController = rearViewController;
mainRevealController.frontViewController= frontNavigationController;
self.window.rootViewController =nil;
self.window.rootViewController = mainRevealController;
[self.window makeKeyAndVisible];
This works, but creates a new Navigtion controller, and what I need is to use the one already defined in the storyboard, since it has specific properties.
Any idea?
Thanks
actually SwLRevalViewController Taken the ownership of Root, so do like this
in Appdelegate.m
//initially navigate to your Main controller on SWL
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
{
NSString *alert = userInfo[#"aps"][#"redirect_url"];
[[NSUserDefaults standardUserDefaults]setObject:alert forKey:#"itemType"];
[[NSUserDefaults standardUserDefaults]synchronize];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
SWRevealViewController *main = (SWRevealViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:#"SWRevealViewController"];
self.window.rootViewController = main;
}
//it automatically redirect to your root controller
on that main view controller ViewDidLoad check / Navigate to your photo view controller
NSString *getType=[[NSUserDefaults standardUserDefaults]objectForKey:#"itemType"];
if ([gettypeofItem isEqualToString:#"something"])
{
yourPhotoViewController *ivc=[self.storyboard instantiateViewControllerWithIdentifier:#"yourPhotoViewController"];
[self.navigationController pushViewController:ivc animated:YES];
}
else
{
// do nothing
}
You can use following method
NSString *storyBoardName=#"Main_iPad";
if (isPhone)
storyBoardName=#"Main_iPhone";
UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:storyBoardName bundle:nil];
PlayVideoBySocketsScene *controller=[storyBoard instantiateViewControllerWithIdentifier:#"playVideoUsingSockets"];
float disptachTime=2.0;
if (value)
{
disptachTime=1.0;
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(disptachTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:controller animated:YES completion:nil];
please try this it may help you. in this when you click on notification this method us called first insted for appdelegate
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
NotificationViewController *notificationViewController = [[NotificationViewController alloc] init];
[navController.visibleViewController.navigationController pushViewController:notificationViewController];
}

Changing root view controller after iOS app has loaded.

In order to show my login screen when the app loads, and not after the user logs in, I have decided to add an auth object in NSUserDefaults when the user logs in successfully. When the app is launched that auth parameter is checked, and the view controller is set accordingly (if the user is auth it'll show a feed, if not it'll show a login screen) In the latter case, I have the app delegate reset the root view controller to the feed after the user has logged in. Is this bad practice or is there a better way of doing this?
In the app delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
IIViewDeckController* deckController = [self generateControllerStack];
self.rightController = deckController.rightController;
self.centerController = deckController.centerController;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if([[defaults objectForKey:#"auth"] isEqualToNumber:[NSNumber numberWithInt:1]]){
self.window.rootViewController = deckController;
}else{
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MainStoryboard"
bundle:nil];
UIViewController* vc = [sb instantiateViewControllerWithIdentifier:#"loginViewController"];
self.window.rootViewController = vc;
}
[self.window makeKeyAndVisible];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque animated:NO];
return YES;
}
- (void) setRoots
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
IIViewDeckController* deckController = [self generateControllerStack];
self.rightController = deckController.rightController;
self.centerController = deckController.centerController;
self.window.rootViewController = deckController;
[self.window makeKeyAndVisible];
}
In the login view controller:
- (IBAction)loginClick:(id)sender {
if([_emailField.text length]>0&&[_passField.text length]>0){
NSString *user = _emailField.text;
NSString *pass = _passField.text;
[[API sharedInstance] login:user andPass:pass onCompletion:^(NSDictionary *json){
NSLog(#"%#", json);
if(![json objectForKey:#"error"]){
[API sharedInstance].authorized = 1;
NSNumber *auth = [NSNumber numberWithInt:1];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:auth forKey:#"auth"];
[defaults synchronize];
captureYouAppDelegate *app = [[UIApplication sharedApplication] delegate];
[app setRoots];
}else{
[API sharedInstance].authorized = 0;
}
}];
}else{
if([_emailField.text length]<1){
[_emailField becomeFirstResponder];
}else{
[_passField becomeFirstResponder];
}
}
}
I'm wondering if there is a better or easier way than doing that. Thank you!
Just to clarify. I had reset UIWindow's rootViewController before without any problem but when attempting to do so while allowing device rotation I ran into some issues — Rotation just stopped working.
I found the following directly from Apple's docs while trying to debug. The link below has a number of guidelines about working with UIWindow. These are all related to device rotation but still good to know.
Short answer, use a root controller and add child view controllers. You can then swap out the child VCs with no problem.
• You have added your view controller's UIView property to UIWindow as a subview.
Developers are discouraged from adding the view property of any view controller as a subview of UIWindow. Your application's root view controller should be assigned to the app window's rootViewController property either in Interface Builder, or at runtime before returning from application:didFinishLaunchingWithOptions:. If you need to display content from more than one view controller simultaneously, you should define your own container view controller and use it as the root view controller. See Creating Custom Container View Controllers.
Check this technical Q&A for more details.
I don't think reset the window.rootViewController is a bad practice. However, there is no need to recreate a window.
If you don't want to use the previous view controller, just replace the window's rootViewController with the new view controller. If you do want to switch back to your previous view controller, use -presentViewController: animated: completion: to present your view controller may be a better alternative.
There is no need to alloc window again you can directly set this
window.rootViewController = yourVC;
Step By Step i am showing the Good Practice to use of rootviewcontroller with the help of UINavigationController
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.splash = [[SplashViewController alloc] initWithNibName: #"SplashViewController" bundle: nil];
self.window.rootViewController = self.splash;
[self.window makeKeyAndVisible];
DLog(#"finished initializing .....");
[self setRootViewController];
return YES;
}
- (void) setRootViewController
{
DLog(#"setRootViewController");
if (self.sessionManager.isLoggedIn)
{
[self navigateToHomeController];
} else {
[self navigateToLoginController];
}
}
- (void) navigateToHomeController
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"kStoryBoard" bundle: nil];
UINavigationController* homeNavigationController = [storyboard instantiateViewControllerWithIdentifier:#"kHomeNavController"];
NSArray* controllers = [homeNavigationController viewControllers];
if([controllers lastObject])
{
if ([[controllers objectAtIndex:0] isKindOfClass:[HomeViewController class]]) {
HomeViewController* homeController = (HomeViewController*) [controllers objectAtIndex:0];
self.window.rootViewController = [[OLNavigationViewController alloc] initWithRootViewController: homeController];
}
}
}
- (void) navigateToLoginController
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"kStoryBoard" bundle: nil];
UINavigationController* loginNavigationController = [storyboard instantiateViewControllerWithIdentifier:#"kLoginNavController"];
if ([loginNavigationController isKindOfClass:[OLLoginViewController class]]) {
OLLoginViewController* loginController = (OLLoginViewController*) loginNavigationController;
loginController.shouldHideToolBar = YES;
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController: loginController];
}
}

appdelegate how to pushViewController with storyboard

In my app delegate I am pushing the user to a view controller if the user details are saved
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
// cached user
PFUser *currentUser = [PFUser currentUser];
if (currentUser)
{
NSLog(#"current user signing and looking for VC %#", currentUser);
// A user was cached, so skip straight to the main view
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
TaskSelectionViewController*viewController = [[TaskSelectionViewController alloc] initWithUser:currentUser];
[navigationController pushViewController:viewController animated:YES];
NSLog(#"VC found");
} else {
NSLog(#"VC not found");
}
TaskSelectionViewController.m
-(id)initWithUser:(PFUser *)currentUser
{
NSLog(#"current user %#", currentUser);
NSLog(#"task Selection initwithuser");
return self;
}
The push is working but all I get is the NavigationController bar at the top and a black Screen.
what is missing ??
thanks for your help.
OK done using instantiateViewControllerWithIdentifier:#"ID"
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle: nil];
TaskSelectionViewController *controller = (TaskSelectionViewController*)[mainStoryboard instantiateViewControllerWithIdentifier: #"task"];
[navigationController pushViewController:controller animated:YES];
In the -(id)initWithUser:(PFUser *)currentUser of TaskSelectionViewController you should call [super init] or something similar to have your ViewController initialized properly. This solved a similar problem for me at least.
Look at Apple's examples for reference implementations of initialization methods of view controllers.

Resources