ios - 检查用户连续使用应用程序的天数
问题描述
我已经看到其他问题询问应用程序打开了多少次。我想在用户连续31天使用该应用程序时发送本地通知。
这是NSUserDefaults发现方法还是我需要使用分析 API?
解决方案
使用UserDefault
. 在 appdelegate 的didFinishLaunch
方法中检查天数
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let kLastUsed = "LastUsedTime"
let kDaysCount = "DaysCount"
let currentDateTimeInterval = Int(Date().timeIntervalSinceReferenceDate)
var storedDaysCount:Int = UserDefaults.standard.integer(forKey: kDaysCount)
if storedDaysCount >= 31 {
//show pushNotifications
}
else {
let lastDateTimeInterval = UserDefaults.standard.integer(forKey: kLastUsed)
let diff = currentDateTimeInterval - lastDateTimeInterval
if diff > 86400 && diff < 172800 {
//next day. increase day count by one
storedDaysCount = storedDaysCount + 1
UserDefaults.standard.set(storedDaysCount, forKey: kDaysCount)
}
else if diff > 86400 {
//not next day. reset counter to 1
UserDefaults.standard.set(1, forKey: kDaysCount)
}
UserDefaults.standard.set(currentDateTimeInterval, forKey: kLastUsed)
}
return true
}
推荐阅读
- java - RecyclerView 项目不重复,但保留其插槽中先前项目的更改
- python - 在 PyTorch 中屏蔽损失函数的最合适方法是什么
- javascript - 我有与内容安全政策相关的问题
- python - 来自同一对象的 2 个不同引用对来自同一类的两个不同对象 - DJANGO 数据库
- javascript - Javascript Promise - 带返回值的递归 - “记住层次结构”
- ruby-on-rails - React Rails Dokku Heroku 在生产中提供静态文件
- node.js - 错误:无法将参数类型“字符串”分配给参数类型“Uri”。'Uri' 来自 'dart:core'
- rust - 复合特征不起作用:没有为 `[name]` 实现特征`std::io::Read`
- swift - NSImageView 被剪掉
- c# - Asp.Net Core 3.1 - 单击按钮时按 GUID 值删除和重命名 jstree 节点?