我们可以使用NSUserdefaults检测我们的应用程序的首次启动。但有没有办法检测iOS每日首次发布?我需要每天一个弹出一个弹出窗口。
答案 0 :(得分:3)
谢谢你们!有一个简单的解决方案感谢您的支持
let today = NSDate().formatted
if (NSUserDefaults.standardUserDefaults().stringForKey(Constants.FIRSTLAUNCH) == today)
{
//Already Launched today
}
else
{
//Today's First Launch
NSUserDefaults.standardUserDefaults().setValue(today, forKey:Constants.FIRSTLAUNCH)
}
extension NSDate {
var formatted: String {
let formatter = NSDateFormatter()
formatter.dateFormat = "MM/dd/yyyy"
return formatter.stringFromDate(self)
}
答案 1 :(得分:1)
在applicationDidFinishLaunchingWithOptions
,
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// get the time the app last launched
let lastLaunch = NSUserDefaults.standardUserDefaults().doubleForKey("lastLaunch")
let lastLaunchDate = NSDate(timeIntervalSince1970: lastLaunch)
// check to see if lastLaunchDate is today.
let lastLaunchIsToday = NSCalendar.currentCalendar().isDateInToday(lastLaunchDate)
if !lastLaunchIsToday {
// do your stuff, show a popup or whatever
}
// update the last launch value
NSUserDefaults.setDouble(NSDate().timeIntervalSince1970(), forKey: "lastLaunch")
return true
}
答案 2 :(得分:0)
在appDelegate首次启动时启动将时间戳保存到userdefaults或某处,然后如果有时间戳,只检查今天的日期是否与时间戳的此日期不同,如果它是替换旧时间戳并显示弹出窗口
答案 3 :(得分:0)
我希望它可以帮到你:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let timestamp = NSUserDefaults.standardUserDefaults().doubleForKey(forKey: "lastLaunch")
if !NSDate.init(timeIntervalSinceNow: timestamp).isToday() {
// Show popup
NSUserDefaults.standardUserDefaults().setDouble(NSDate().timeIntervalSince1970, forKey: "lastLaunch")
NSUserDefaults.standardUserDefaults().synchronize()
}
return false
}
extension NSDate {
func isToday() -> Bool {
let today = NSDate()
self.compare(today)
let todayCalendar: NSDateComponents = NSCalendar.currentCalendar().components([.Era, .Year, .Month, .Day], fromDate: today)
let selfCalendar: NSDateComponents = NSCalendar.currentCalendar().components([.Era, .Year, .Month, .Day], fromDate: self)
return todayCalendar.day == selfCalendar.day && todayCalendar.month == selfCalendar.month && todayCalendar.year == selfCalendar.year && todayCalendar.era == selfCalendar.era
}
}