Tracking basic app use

This post introduces the BNAppStats class which acts as a very light weight, extremely easy-to-use class for detecting how many times your app has run. Suppose you want to display a message only the first time your app is opened, or the first n times. Or perhaps you’d like to ask the user to rate your app after opening it five times, but you don’t want to integrate a full-blown analytics engine. This solves that problem.

This isn’t an insanely hard problem to solve, but what this code does nicely is require basically zero overhead. In particular, you don’t have to call it for it to work. Seriously. Just link it into your project, and it keeps track of number of app opens and inits automatically, using the [NSObject load] method, which is called by the system at app startup.

Here’s how you can use the class, as an example:

- (void)myStartupMethod {
  if ([BNAppStats numAppOpens] < 2) {
    [self doFirstTimeStuff];
  } else if ([BNAppStats numAppOpens] == 5) {
    [self requestAppStoreRating];

The class tracks both number of app opens, and number of inits. The number of opens is the number of times the app enters the foreground, which includes blank-slate startups and users switching between multiple apps that are resident in memory. The number of inits just includes blank-slate startups. Users tend to think in terms of opens.

This code is available for free, open-sourced under the Apache 2 license, as part of the moriarty library. To use it, just download from the moriarty github page (click the big “Downloads” button), copy the files into your project directory, and add them to your project in Xcode.

Here’s the header:

//  BNAppStats.h
//  MadTiles
//  Created by Tyler Neylon on 10/18/10.
//  Copyleft 2010 Bynomial.
//  A helper class to easily determine how
//  many times an app has been opened or initialized.
//  An "open" is any time anyone clicks on your
//  app's icon to start it.
//  An "init" is any time your app's icon is clicked,
//  _and_ it is not already present in memory.
//  We have #inits <= #opens, and it can be < due to
//  multitasking; specifically your app may only be
//  hidden and not closed between some sessions.

#import <Foundation/Foundation.h>

@interface BNAppStats : NSObject {


+ (int)numAppInits;
+ (int)numAppOpens;


Post a Comment

Your email is never published nor shared. Required fields are marked *