Hide login view if user logged in - ios

i'm try to never show user login view if he logged in
i do this in viewWillAppear
-(void)viewWillAppear:(BOOL)animated
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"userName"]) {
NSDictionary *d = [defaults objectForKey:#"currentUser"];
UserProfile *userData = [[UserProfile alloc]initWithDictionary:d];
[[NetworkModel sharedManager] setCurrentUser:hh];
[self loginResults];
}
self.navigationController.navigationBarHidden = YES;
}
and loginResults function
-(void)childrenReceived:(NSNotification *) notification
{
[SVProgressHUD dismiss];
NSDictionary *userInfo = notification.userInfo;
NSArray *allKids = [userInfo objectForKey:#"children"];
RootViewController *sideBarRoot= [self.storyboard instantiateViewControllerWithIdentifier:#"root"];
sideBarRoot.children = allKids;
[self dismissViewControllerAnimated:YES completion:nil];
[self presentViewController:sideBarRoot animated:YES completion:nil];
}

check the below coding
#interface AppDelegate : UIResponder <UIApplicationDelegate>
-(void) didFinishLogin
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
CCKFNavDrawer*homeNavController = (CCKFNavDrawer*)[mainStoryboard instantiateViewControllerWithIdentifier:#"NavigationLoginID"];
[self.window makeKeyAndVisible];
self.window.rootViewController = homeNavController;
-(void)didFinishLogout
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UINavigationController *homeNavController = (UINavigationController*)[mainStoryboard instantiateViewControllerWithIdentifier:#"LoginNavigationID"];
LoginVC *objloginVC = (LoginVC*)[homeNavController topViewController];
objloginVC.delegate=self;
[self.window makeKeyAndVisible];
self.window.rootViewController = homeNavController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
if( [[[NSUserDefaults standardUserDefaults]objectforkey:#"loginKey"]isequaltostring:#""])
[self didFinishLogout];
else
[self didFinishLogin];

Related

Get to specific tabView object from App Delegate

Trying to launch a specific tabView object from the app delegate when you receive a notification, however the app crashes on launch.
Here is the code:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *viewController;
CustomTabViewController * tabBar = [[CustomTabViewController alloc] init];
NSDictionary *notificationPayload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
NSString *type = [notificationPayload objectForKey:#"type"];
if([type isEqualToString:#"friend"]){
tabBar.selectedViewController = [tabBar.viewControllers objectAtIndex:2];
self.window.rootViewController = tabBar;
[self.window makeKeyAndVisible];
}
if([type isEqualToString:#"message"]){
tabBar.selectedViewController = [tabBar.viewControllers objectAtIndex:0];
self.window.rootViewController = tabBar;
[self.window makeKeyAndVisible];
}
Any ideas?

View controller won't return to previous view controller programmatically

The scenario is when the app first loads (controller A loads), it checks if an NSDefaultuser exist. If it does not exist then it sends it to (Controller B). In controller B the user enters his/her username then clicks ok. Now, NSDefault is saving the username and it should also redirect it back to the main view controller. Here is the code:
if([Username.text isEqual: #""]) {
[self showOkAlert:#"Username required"];
}
else if([Password.text isEqual:#""]) {
[self showOkAlert:#"Password required"];
}
else {
//suppose that login was successfully.
[self SaveCredentials];
NSString * storyboardName = #"MainStoryboard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"LatestNews"];
[self presentViewController:vc animated:YES completion:nil];
}
Controller A:
- (void)LoadCredentials {
//load username
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *username = [defaults objectForKey:#"User"];
if(!username) {
[self showAlert:#"user does not exist"];
[self dismissViewControllerAnimated:YES completion:nil];
NSString * storyboardName = #"MainStoryboard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"Login"];
[self presentViewController:vc animated:YES completion:nil];
}else{
[self showAlert:#"user exists"];
// for testing purposes I want it to reset when user logged in. Just so I know it resets. Later this method will be called from logout button.
[self resetUsernameWhenLogout];
}
}
-(void)resetUsernameWhenLogout {
//reset username when logout
NSDictionary *defaultsDictionary = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
for (NSString *key in [defaultsDictionary allKeys]) {
[[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
}
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)viewDidLoad
{
[self LoadCredentials];
}
There are a couple of possibilities here; Firstly, iOS8 seems to instantiate the VC's a lot earlier than in iOS7, so the Navigation tree isn't complete in viewDidLoad, You should only use viewDidLoad for additonal initialization. Try moving your code [self LoadCredentials];
} to viewWillAppear.
Secondly, if you have a nav controller, you should be adding the new VC to the nav tree via the nav controller. If this is the case, try the code below.
if([Username.text isEqual: #""]) {
[self showOkAlert:#"Username required"];
}
else if([Password.text isEqual:#""]) {
[self showOkAlert:#"Password required"];
}
else {
//suppose that login was successfully.
[self SaveCredentials];
//This does not work - I receive a thread error not sure how to debug it
UIStoryboard *storyBoard = self.storyboard;
UIViewController *targetViewController = [storyBoard instantiateViewControllerWithIdentifier:#"LatestNews"];
UINavigationController *navController = self.navigationController;
if (navController) {
[navController pushViewController:targetViewController animated:YES];
}
}

Delete all user data in UIViewController's after logout

When I login to my app, my app does push the ViewController XYZMainViewController, XYZMainViewController viewWillAppear:animated call method that makes a request to my API to retrieve the authenticated user data, at this time I update the text of a label to show the user name. When I logout the app, it returns me to the login ViewController, when I do login again with another user, XYZMainViewController label text contains the name of the previous user, without updating the label text.
XYZMainViewController.m
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:NO];
[self homeProfile];
}
- (void)homeProfile
{
[NXOAuth2Request performMethod:#"GET"
onResource:[NSURL URLWithString:#"http://{url}/users/userinfo"]
usingParameters:nil
withAccount:[XYZCommonFunctions user]
sendProgressHandler:nil
responseHandler:^(NSURLResponse *response, NSData *responseData, NSError *error){
NSDictionary *parsedData = [[NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error] objectForKey:#"data"];
_user = [parsedData objectForKey:#"user"];
[self.label setText:[NSString stringWithFormat:#"Welcome %#!", [_user objectForKey:#"username"]]];
}];
}
- (IBAction)logout:(id)sender {
XYZAppDelegate* appDelegate = (XYZAppDelegate*)[[UIApplication sharedApplication] delegate];
[appDelegate logout];
}
XYZAppDelegate.m
- (void)login
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *identifier = [prefs stringForKey:#"accountidentifier"];
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
NSString *viewIdentifier = #"WelcomeView";
if(identifier != nil){
NXOAuth2Account *account = [[NXOAuth2AccountStore sharedStore] accountWithIdentifier:identifier];
if(account != nil) {
viewIdentifier = #"MainView";
}
UIViewController *controller = [mainStoryboard instantiateViewControllerWithIdentifier: viewIdentifier];
[navigationController pushViewController:controller animated:NO];
return;
}
}
- (void)logout
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs removeObjectForKey:#"accountidentifier"];
[prefs synchronize];
for (NXOAuth2Account *a in [[NXOAuth2AccountStore sharedStore] accounts] ){
[[NXOAuth2AccountStore sharedStore] removeAccount:a];
}
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
[navigationController popToRootViewControllerAnimated:YES];
}
I need to reinitialize all data in XYZMainViewController.
Thank you.
Look like problem is related to fetching JSON Object. It is possible that everytime you have send same user to fetch user data. You are not using NSUserdefault object to display name, you are using value, which is return by JSON Object. According to me cause of error is "withAccount:[XYZCommonFunctions user]" line.
I would like to suggest, instead of using
-(void)viewWillAppear:(BOOL)animated {
you can use
- (void)viewDidLoad
so that your login action performed only when your LoginController loads,instead when LoginController appear.
New viewwillAppear look as given below -
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:NO];
[self.label setText:#""];
}
and ViewDidLoad -
- (void)viewDidLoad
{
[super viewDidLoad];
[self homeProfile];
}
Also check your json response, whether you are getting response success or error.According to response need to handle.
Hope this helps.

IOS: Load a View Controller When App is Launched from Browser using url schemes?

I am new to iPhone developing , My app is based on video conferencing, I have a task where an app should launch from browser using URL schemes. I have done it but problem is when app launched from browser it should load particular view controller. I am using Storyboard. Here is the code which I tried.
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
if (!url) { return NO;
}
NSString *URLString = [url absoluteString];
[[NSUserDefaults standardUserDefaults] setObject:URLString forKey:#"url"];
[[NSUserDefaults standardUserDefaults] synchronize];
URLParser *parser = [[URLParser alloc] initWithURLString:URLString];
username = [parser valueForVariable:#"USERNAME"];
NSLog(#"%#", username); //b
sessid = [parser valueForVariable:#"SESSION_ID"];
NSLog(#"%#", sessid); //(null)
tokenid = [parser valueForVariable:#"token"];
NSLog(#"%#", tokenid); //yes
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
ViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"VideoController"];
return YES;
}
This is how I do it..
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
//get parameters
[self goResetPassword:dict];
}
return YES;
}
- (void) goResetPassword:(NSDictionary*) dict{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UINavigationController *root = [[UINavigationController alloc]initWithRootViewController:[storyboard instantiateViewControllerWithIdentifier:#"resetPasswordView"]];
self.window.rootViewController= root;
ResetPasswordViewController *vc = (ResetPasswordViewController*)[[root viewControllers] objectAtIndex:0];
[vc loadData:dict];
}
hope it helps... GL HF
According to your comment your initial ViewController is NavigationController. This is how you can push the required ViewController.
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
// same code
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
// Get instance of initial Viewcontroller from storyboard
UINavigationController *navController = [storyboard instantiateInitialViewController
];
// Get instance of desired viewcontroller
ViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"VideoController"];
// Push ViewController on to NavigationController
[navController pushViewController:viewController animated:NO];
return YES;
}
Check It
UIStoryboard *board = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
UINavigationController *nav = [[UINavigationController alloc] init];
[nav setNavigationBarHidden:NO];
IntroVC *initailView = [board instantiateViewControllerWithIdentifier:#"Intro"];
[nav pushViewController:initailView animated:YES];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
UIApplication *sharedApplication = [UIApplication sharedApplication];
[sharedApplication unregisterForRemoteNotifications];

Can't skip ViewController with storyboard

I get the notification But my app is not running and i open the app through notification so i can't skip ViewController.
That is my code.
AppDelegate.m
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive)
{
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]];
if([[NSUserDefaults standardUserDefaults] valueForKey:#"username"] && [[NSUserDefaults standardUserDefaults] valueForKey:#"password"] && [[NSUserDefaults standardUserDefaults] valueForKey:#"companyid"])
{
HomeViewController *hm= [storyBoard instantiateViewControllerWithIdentifier:#"HomeView"];
self.window.rootViewController = hm;
}
else
{
NSLog(#"LoginView");
}
}
else
{
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]];
if([[NSUserDefaults standardUserDefaults] valueForKey:#"username"] && [[NSUserDefaults standardUserDefaults] valueForKey:#"password"] && [[NSUserDefaults standardUserDefaults] valueForKey:#"companyid"])
{
HomeViewController *hm= [storyBoard instantiateViewControllerWithIdentifier:#"HomeView"];
self.window.rootViewController = hm;
}
else
{
NSLog(#"LoginView");
}
}
}

Resources