3

我想知道是否有人知道我如何计算我的应用程序被打开的次数。NSUserDefalte 什么的......我应该把 var 放在哪里,它应该在哪里启动到 0?

4

4 回答 4

5

在您的课程 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"];
}
于 2012-10-24T07:12:23.413 回答
3
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
     //add 1
}

图片

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

于 2012-10-24T07:08:25.223 回答
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"];
于 2012-10-24T07:11:22.030 回答
0

Swift 版本 - 我将它用于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()
        }
    }
}
于 2020-02-19T07:49:42.103 回答