计算在xcode中打开应用程序的次数

时间:2012-10-24 07:04:59

标签: iphone objective-c xcode

我想知道是否有人知道如何计算我的应用程序被打开的次数。 NSUserDefaults或者什么...... 我应该把var放在哪里以及它应该从哪里开始?

4 个答案:

答案 0 :(得分:4)

在你的AppDelegate.m课程中你可以这样做:

//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"];
}

答案 1 :(得分:3)

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

 image

来自http://www.cocoanetics.com/2010/07/understanding-ios-4-backgrounding-and-delegate-messaging/的图片

答案 2 :(得分:0)

是的,使用NSUserdefaults是一个简单的解决方案。

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

如果您希望它从背景中恢复它的所有时间,请查看:

-(void) applicationWillEnterForeground:(UIApplication*)application

尝试这样的事情:

// 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"];

答案 3 :(得分:0)

快速版本-我正在将其用于 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)
}

助手类

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()
        }
    }
}