Sosily
Sosily

Reputation: 726

Count the number of times the app was opened in xcode

I want to know if someone know how I can count the number of times that my app has been opened. NSUserDefalte or something... Where should I put the var and where should it be initiated to 0?

Upvotes: 3

Views: 1960

Answers (4)

sabiland
sabiland

Reputation: 2614

Swift version - I am using this for SKStoreReviewController.requestReview()

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    setupStart()
    return true
}

private func setupStart()
{
    setAppStartsInfos()
}

private func setAppStartsInfos()
{
    Helper.checkForStoreReviewAppStarted = Helper.uds.integer(forKey: Helper.udKeyAppRunned) + 1
    Helper.udStoreValue(key: Helper.udKeyAppRunned, value: Helper.checkForStoreReviewAppStarted)
}

Helper class

static var checkForStoreReviewUse = true
static let checkForStoreReviewAppStartedLimit: Int = 5
static var checkForStoreReviewAppStarted: Int = 0
static let uds = UserDefaults.standard
static var udKeyAppRunned: String
{
    get
    {
        return "\(getAppVersionClean())_udKeyAppRunned"
    }
}

static func udStoreValue(key: String, value: Any)
{
    return uds.set(value, forKey: key)
}

static func udStoredValue(key: String) -> Any?
{
    return uds.value(forKey: key)
}

static func udStoredValueExists(key: String) -> Bool
{
    return uds.value(forKey: key) != nil
}

static func getAppVersionClean() -> String
{
    if let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
    {
        return version
    }
    else
    {
        return String()
    }
}

static func checkForStoreReview()
{
    if checkForStoreReviewUse
    {
        if checkForStoreReviewAppStarted < checkForStoreReviewAppStartedLimit
        {
            // STOP checking for this app-run
            checkForStoreReviewUse = false
        }
        else
        {
            // STOP checking for this app-run
            checkForStoreReviewUse = false
            // Request review
            SKStoreReviewController.requestReview()
        }
    }
}

Upvotes: 0

Ashbay
Ashbay

Reputation: 1661

In your class AppDelegate.m you can do this :

//Application did launch
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  int count = [[NSUserDefaults standardUserDefaults] integerForKey:@"LaunchCount"];
  if(count < 0) count = 0;
  [[NSUserDefaults standardUserDefaults] setInteger:count+1 forKey:@"LaunchCount"];
}

//The application was in background and become active
- (void)applicationWillEnterForeground:(UIApplication *)application
{
  int count = [[NSUserDefaults standardUserDefaults] integerForKey:@"LaunchCount"];
  if(count < 0) count = 0;
  [[NSUserDefaults standardUserDefaults] setInteger:count+1 forKey:@"LaunchCount"];
}

Upvotes: 5

janusfidel
janusfidel

Reputation: 8106

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
     //add 1
}

 image

image from http://www.cocoanetics.com/2010/07/understanding-ios-4-backgrounding-and-delegate-messaging/

Upvotes: 3

oscar
oscar

Reputation: 647

Yes, using NSUserdefaults is an easy solution.

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary     *)launchOptions{
    // Get the NSUserDefault number here, if not available, create a new.
}

If you want it to track all the times it is resumed from the background, look at:

-(void) applicationWillEnterForeground:(UIApplication*)application

Try something like this:

// Get the number of starts:
NSNumber *starts = [[NSUserDefaults standardUserDefaults] objectForKey:@"starts"];

// increase by one
NSNumber *number = [NSNumber numberWithInt:([starts intValue] + 1)];

// store the number of starts
[[NSUserDefaults standardUserDefaults] setObject:starts forKey:@"starts"];

Upvotes: 0

Related Questions