I am working in XCode 8. I have three ViewControllers in my Main storyboard. The first two are kind of introduction screens that explain what the application does etc.
Is there a way to check whether the user has already opened the app in a certain time frame (e.g. "today") and if yes, skip the first two screens?
(Users do not need to log in with an account or something.)
My application method looks like this:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]? = nil) -> Bool {
//Requesting Authorization for User Interactions
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
let userDefaults = UserDefaults.standard
let calendar = Calendar.current
if let lastOpened = userDefaults.object(forKey: "lastOpened") as? Date, calendar.isDateInToday(lastOpened) {
// app has been opened today
let myViewController = ViewController()
window?.rootViewController = myViewController
} else {
// app has not been opened today / not been opened at all
print("Alright, opening explanation screens")
let myViewController = StartViewController()
window?.rootViewController = myViewController
}
// save the current date for the next check
userDefaults.set(Date(), forKey: "lastOpened")
userDefaults.synchronize()
window?.makeKeyAndVisible()
return true
}