计算使用Swift启动应用程序的次数? [英] Count number of times app has been launched using Swift?

查看:84
本文介绍了计算使用Swift启动应用程序的次数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算一下我的iOS应用程序使用Swift启动的次数。

I would like to count the number of times my iOS application has been launched using Swift.

然后我想取数字并使用<$显示它c $ c> NSLog 每次。

I would then like to take the number and display it using NSLog each time.

推荐答案

AppDelegate中添加此项 in applicationDidFinishLaunching method。

Add this in AppDelegate in applicationDidFinishLaunching method.

Swift 3和Swift 4:

// get current number of times app has been launched
let currentCount = UserDefaults.standard.integer(forKey: "launchCount")

// increment received number by one
UserDefaults.standard.set(currentCount+1, forKey:"launchCount")

// save changes to disk
UserDefaults.standard.synchronize()

Swift 2 :

// get current number of times app has been launched
let currentCount = NSUserDefaults.standardUserDefaults().integerForKey("launchCount")

// increment received number by one
NSUserDefaults.standardUserDefaults().setInteger(currentCount+1, forKey:"launchCount")

// save changes to disk 
NSUserDefaults.standardUserDefaults().synchronize()

这篇关于计算使用Swift启动应用程序的次数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆