0

I have some code that runs every time the person opens the app. It works to determine whether the person logged in for the first time, or whether they are a repeat user. And based on that info, the app behaves a bit differently. But it has not been working for me as I thought it would. Here is the code:

- (void)viewDidAppear:(BOOL)animated
{            
    // Get user data. 
    NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];

    // First arg is name and second is if not found.
    NSString *user_id = [standardUserDefaults objectForKey:@"user_id"]; 
    bool first_time_cookie = [standardUserDefaults boolForKey:@"first_time_cookie"];

    [super viewDidLoad];

    if(!first_time_cookie)
    {
        // First time on the app, so set the user cookie.
        [standardUserDefaults setBool:YES forKey:@"first_time_cookie"];

        [[NSUserDefaults standardUserDefaults] synchronize];  

        // Make new account 

        [standardUserDefaults synchronize];           
    }
    else 
    { 
        // DO REPEAT USER ACTIONS
    }

    [[NSUserDefaults standardUserDefaults] synchronize];    
}

I am getting mixed up in my userDefaults. Would anyone be able to tell where I am going wrong? The problem that is occurring is that the system thinks that many of the repeat users are actually new users.

Thanks!

Genadinik
  • 18,153
  • 63
  • 185
  • 284
  • 1
    i am not sure why you are calling [super viewDidLoad]; in viewDidAppear method. – Vishal Singh Dec 18 '12 at 15:42
  • Your `first_time_cookie` is set independently on `user_id`. `user_id` is never set. `synchronize` is called too many times. – Sulthan Dec 18 '12 at 15:43
  • @Sulthan I am getting confused between using [[NSUserDefaults standardUserDefaults] synchronize]; like I have in the end, or which of these to use [[NSUserDefaults standardUserDefaults] synchronize]; or [standardUserDefaults synchronize]; – Genadinik Dec 18 '12 at 15:45
  • @iVishal I was not sure regarding calling [super viewDidLoad]; ...should I just not do that at all? What does that line do? I am pretty new to ios development. – Genadinik Dec 18 '12 at 15:46
  • They're both the same since `standardUserDefaults = [NSUserDefaults standardUserDefaults]`. You only need to call it once, after you've set the boolean. – sooper Dec 18 '12 at 15:48
  • yes you should not, you should rather write [super ViewDidAppear:animated:animated];, well thats not related to your current problem, but what you have written can create some other issues – Vishal Singh Dec 18 '12 at 15:48
  • iVishal thank you, could you please explain a little bit what the line that you suggest does? I am curious. – Genadinik Dec 18 '12 at 15:51
  • well [super methodName]; is used to tell to compiler to include the implemetation of methodName from parent class also, so if you are overriding viewDidAppear method then you would like to tell the compiler to call the viewDidAppear method from parent class and not the vieDidLoad. – Vishal Singh Dec 18 '12 at 15:57
  • check this http://stackoverflow.com/questions/9234024/why-i-need-to-use-super-methodname-in-ios – Vishal Singh Dec 18 '12 at 15:57
  • @iVishal thank you, but what is the animated:animated part of it? :) – Genadinik Dec 18 '12 at 16:03
  • fisrt animated is the part of method name, viewDidAppear:animated this is the method name, and 2nd animated is the argument you are passing to this method.:) – Vishal Singh Dec 18 '12 at 16:05
  • @iVishal actually I just tried it in my code, and I get a compiler error: no visible interface for 'UIViewInterface' declares the selector 'viewDidApear' ...would you know why that happens? – Genadinik Dec 18 '12 at 16:11
  • its happning cz of spelling mistake..:p viewDidApear==> viewDidAppear [super viewDidAppear:animated]; paste is exactly – Vishal Singh Dec 18 '12 at 16:14

1 Answers1

2

I suspect the problem may have to do with the usage of bool rather than BOOL. See this question here.

bool maps to values of true/false, whereas BOOL maps to YES/NO. You are seting first_time_cookie to YES (a BOOL value). Therefore, try:

BOOL first_time_cookie = [standardUserDefaults boolForKey:@"first_time_cookie"];
Community
  • 1
  • 1
bibo bode
  • 1,150
  • 1
  • 10
  • 17
  • thank you, now I am starting to think about BOOL vs. bool - so when I was setting bool and the comparing if (!first_time_cookie) why would I have been getting it true some of the time and false at other times? – Genadinik Dec 18 '12 at 15:55