[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 665
  • Last Modified:

using viewdidload

I have a login view i want to display if the user isn't logged in.  For testing, when some closes the application, it clears the NSDefault value and on the viewDidLoad, it checks to see if the NSDefault is there or not with (!user) and is suppose to return (null) if it's not there but it doesn't work after the first time.  Please help.

0
BTMExpert
Asked:
BTMExpert
  • 6
  • 5
  • 2
1 Solution
 
mad_macCommented:
since iOS 4 applications do not fully close when a user hits the home button, iOS freezes the state.  so although you change the values in your NSUserDefaults object the viewDidLoad may not be getting called.

would help if you could show some or all of you code
0
 
darbid73Commented:
Code would help.  But otherwise I am guessing what you need to do is to know is what happens when "some closes the application".

In you app delegate you need to implement these

- (void)applicationWillResignActive:(UIApplication *)application
{
    NSLog(@"%s", __func__);
    /*
     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     */
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    NSLog(@"%s", __func__);
    /*
     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
     If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
     */
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    NSLog(@"%s", __func__);
    /*
     Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
     */
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    NSLog(@"%s", __func__);
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    NSLog(@"%s", __func__);
    // Saves changes in the application's managed object context before the application terminates.
    [self saveContext];
}

Open in new window


now you can deal with these events how you like.
0
 
BTMExpertAuthor Commented:
AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    
    return YES;
}
							
- (void)applicationWillResignActive:(UIApplication *)application
{
    
    /*
     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     */
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs removeObjectForKey:@"Username"];
    /*
     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
     If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
     */
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    /*
     Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
     */
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs removeObjectForKey:@"Username"];
    /*
     Called when the application is about to terminate.
     Save data if appropriate.
     See also applicationDidEnterBackground:.
     */
}

Open in new window


One ViewController
-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSString *myuser = [prefs stringForKey:@"Username"];
    
    if(!myuser) {
        LoginViewController *login = [[LoginViewController alloc] init];
        [self presentModalViewController:login animated:YES];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Login" message:[NSString stringWithFormat: @"Your username is %@", myuser] delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
        
        [alert show];
    }
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSString *myuser = [prefs stringForKey:@"Username"];
    
    if(!myuser) {
        LoginViewController *login = [[LoginViewController alloc] init];
        [self presentModalViewController:login animated:YES];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Login" message:[NSString stringWithFormat: @"Your username is %@", myuser] delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
        
        [alert show];
    }
}

Open in new window

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
mad_macCommented:
viewDidLoad will only fire when the application first loads the view from code or Nib file, do not expect this to fire after the application has gone into background and come back to active.

also if the application goes into background with your view loaded viewDidAppear will not fire either.

Have just tested both of these.

Why not register you viewController to receive UIApplicationDidBecomeActiveNotification notifications via notification centre you will be guaranteed to be notified when the application comes back into foreground.
0
 
BTMExpertAuthor Commented:
how do you do that?
0
 
darbid73Commented:
If your appdelegate implements what I showed you then when you hit the home button you will see from the NSlog the events firing off.  This would be how you would manage your problem generally irrespective of where the user currently is.  You will also see the events firing when the app is reopened.

Notifications is more specific but uses the exact same method or idea.

See here from Apple on notifications.

You can "register" any class for notifications. When you create (or load) your view controller, you'll want to register it as an observer for the UIApplicationDidBecomeActiveNotification and set the method that you want called when that notification gets sent to your application and now registered class.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(yourMethodName:) name: UIApplicationDidBecomeActiveNotification object:nil];

In dealloc, remember to remove the registration.

[[NSNotificationCenter defaultCenter] removeObserver:self];
0
 
BTMExpertAuthor Commented:
I'm just trying to find a way to show a modal view when the app is opened.  I can't use or is there another way to write [self presentModalViewController:login animated:YES]; in the appdelegate?

It's suppose to check for a NSDefault value and if that value isn't there then show the modal view but i can't use self in the appdelegate.
0
 
darbid73Commented:
self refers to the class you are in.

You can replace self with a reference to the viewcontroller that you want self to be.
0
 
BTMExpertAuthor Commented:
so i just add in the viewcontroller

Import "View2.h"

View2 *view = [[View2 alloc] init];

[view presentModalViewController:login animated:YES];

in the appdeleget should work?
0
 
BTMExpertAuthor Commented:
#import "AppDelegate.h"
#import "ViewController.h"
#import "LoginViewController.h"

- (void)applicationWillResignActive:(UIApplication *)application
{
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSString *myuser = [prefs stringForKey:@"Username"];
    
    if(!myuser) {
        LoginViewController *login = [[LoginViewController alloc] init];
        ViewController *view = [[ViewController alloc] init];
        
        [view presentModalViewController:login animated:YES];
    }
}

Open in new window


Not working.  Please help.  I just want a certain view to show up every time the app is loaded from the background or just starting up.
0
 
darbid73Commented:
never gonna work in
applicationWillResignActive
0
 
BTMExpertAuthor Commented:
can it work at all?
0
 
darbid73Commented:
Buddy I think you need to take a break. That name means that the app is "stopping" or becoming inactive. You need the ones to become active.  When all else fails read my post where I suggested to put a nslog in ALL methods and then see when they are called. My first post.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now