Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Detecting second and third launch using "prevStartupVersions"

gwelmartengwelmarten Posts: 15Registered Users
Hi
I'm detecting the number of times my Mac application has being opened using the following code:

NSString *currentVersion = (NSString*)[[NSBundle mainBundle] objectForInfoDictionaryKey:@\"CFBundleVersion\"];
NSArray *prevStartupVersions = [[NSUserDefaults standardUserDefaults] arrayForKey:@\"prevStartupVersions\"];
if (prevStartupVersions == nil)
{
}
else {
}
[[NSUserDefaults standardUserDefaults] synchronize];

However, this only works for detecting the first launch. How would I detect a second and third launch? I did try:

if (prevStartupVersions == 1)

However, I am comparing an NSArray with an Interger there. How should adapt this to detect a second launch?

Thanks in advance for any help,

Sam
Post edited by gwelmarten on

Replies

  • givensurgivensur Posts: 307Registered Users
    You already know how to get arrays in and out of NSUserDefaults, right? How about an int? Look at the documentation for NSUserDefaults and see if you can find what methods to use to get an int from or put an int into the user defaults.

    Once you figure that out, you can then use the int to count.

    Also, are you really intending to increase the size of the array you're storing the previously run versions in every time the user starts the app? Or is that array supposed to be used to keep track of what upgrades the user has done?
    I make cake apps.
    Some of my code can be found on GitHub.
  • gwelmartengwelmarten Posts: 15Registered Users
    Hi
    Thanks for the reply.
    My current method doesn't actually add or change things in the NSUserDefaults does it? I was under the impression that I was using a function of OS X that monitored when an application was opened. I wanted to avoid writing my own data - is there a way of doing that?
    Sam
  • givensurgivensur Posts: 307Registered Users
    gwelmarten;429985 said:
    Hi
    Thanks for the reply.
    My current method doesn't actually add or change things in the NSUserDefaults does it? I was under the impression that I was using a function of OS X that monitored when an application was opened. I wanted to avoid writing my own data - is there a way of doing that?
    Sam
    Wait... so is that code that you posted earlier verbatim? I thought you just left parts out for simplification.

    If that's actually your code, then no, you never write anything to NSUserDefaults. Which means, you never keep track of whether or not the app has been open. So prevStartupVersions would always be nil. But, since you do nothing if it is nil, and you do nothing if it is not nil, then I suppose it doesn't matter. Essentially your code is doing nothing except executing worthless instructions.

    Anyways... there is no built in way to keep track of how many times an app has been opened. You have to keep track of it yourself.

    I'm guessing you just copied and pasted this code from somewhere? Don't do that, unless you understand what the code is doing.
    I make cake apps.
    Some of my code can be found on GitHub.
  • gwelmartengwelmarten Posts: 15Registered Users
    Hi
    You were correct - when I said I wasn't saving to disk, I was referring to what I posted :(
    My partner added the code - I believe he wrote it for the purpose. I do understand almost all of it, just not the bit about writing arrays to the NSUserDefaults, and I've found it difficult to interpret the Apple Documentation on it. The complete code for the section is:

    NSString *currentVersion = (NSString*)[[NSBundle mainBundle] objectForInfoDictionaryKey:@\"CFBundleVersion\"];
    NSArray *prevStartupVersions = [[NSUserDefaults standardUserDefaults] arrayForKey:@\"prevStartupVersions\"];
    if (prevStartupVersions == nil)
    {
    // Starting up for first time with NO pre-existing installs (e.g., fresh
    // install of some version)
    NSLog(@\"New User\");

    NSAlert *newUserFirstLoadMessage = [NSAlert alertWithMessageText:@\"Information for New Users\" defaultButton:@\"OK\" alternateButton:nil otherButton:nil informativeTextWithFormat:@\"*****\"];

    [newUserFirstLoadMessage runModal];

    [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObject:currentVersion] forKey:@\"prevStartupVersions\"];
    }
    else if (prevStartupVersions == 1)
    {
    NSAlert *newUserSecondLoadMessage = [NSAlert alertWithMessageText:@\"Information for New Users\" defaultButton:@\"OK\" alternateButton:nil otherButton:nil informativeTextWithFormat:@\"*****\"];

    [newUserSecondLoadMessage runModal];

    }
    else
    {
    if (![prevStartupVersions containsObject:currentVersion])
    {
    // Starting up for first time with this version of the app. This
    // means a different version of the app was alread installed once
    // and started.
    NSLog(@\"User upgraded to new version - first load of this new version.\");

    NSAlert *newVersionFirstLoadMessage = [NSAlert alertWithMessageText:@\"Thanks for updating.\" defaultButton:@\"OK\" alternateButton:nil otherButton:nil informativeTextWithFormat:@\"*****\"];

    [newVersionFirstLoadMessage runModal];

    NSMutableArray *updatedPrevStartVersions = [NSMutableArray arrayWithArray:prevStartupVersions];
    [updatedPrevStartVersions addObject:currentVersion];
    [[NSUserDefaults standardUserDefaults] setObject:updatedPrevStartVersions forKey:@\"prevStartupVersions\"];
    }
    else {
    NSLog(@\"This version has being used before.\");
    }
    }

    // Save changes to disk
    [[NSUserDefaults standardUserDefaults] synchronize];
  • givensurgivensur Posts: 307Registered Users
    Okay, good :)

    You are correct in your assumption that you cannot compare an array to an integer. So, obviously, you need to just scrap the whole else if section in the middle of that code where you are trying to do that.

    Your partner's code is not keeping track of every time the app is run. It is only keeping track of every update. Which is a good thing, in that the array holding the previously run versions won't grow every time the app is run. But, it also means you can't simply use the array to find out how many times the app has been run.

    To track that you will need to use an NSInteger and you will need to save that NSInteger to NSUserDefaults.

    Using NSUserDefaults really isn't very difficult. It, pretty much, works the same as an NSDictionary, with the added benefit of not having to turn BOOLs, integers, floats, and doubles into NSNumbers before adding them to the defaults (and not having to retrieve them as NSNumbers when getting them out of the defaults).

    So, if you wanted to add an NSInteger to the user defaults you'd do something like:
    [[NSUserDefaults standardUserDefaults] setInteger:42 forKey:@\"myIntegerKey\"];


    And you could get it back like so:
    NSInteger fourtyTwo = [[NSUserDefaults standardUserDefaults] integerForKey:@\"myIntegerKey\"];


    If you're going to be doing a lot of things in the NSUserDefaults, you can save yourself some typing/screen space by just assigning the standardUserDefaults to a pointer.

    #define SAVED_INTEGER_KEY    @\"myIntegerKey\"

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [defaults setInteger:42 forKey:SAVED_INTEGER_KEY];

    // do some stuff

    NSInteger retrievedInteger = [defaults integerForKey:SAVED_INTEGER_KEY];

    // do something that changes retrievedInteger

    [defaults setInteger:retrievedInteger forKey:SAVED_INTEGER_KEY];

    // make sure changes are written
    [defaults synchronize];
    I make cake apps.
    Some of my code can be found on GitHub.
Sign In or Register to comment.