0

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
}
Adrian
  • 21
  • 9
  • you have to save `Date` into `NSUserDefaults` and check if `Date` equals today in `AppDelegate` `didFinishLaunchWithOptions` and change `window.rootViewController` the way you like. You'd better code this yourself, it would be useful. – JuicyFruit Apr 16 '17 at 13:08

2 Answers2

1

I am doing in the Android the same thing: At the launch of the app I will check if has any settings at the SharedPreferences or not. At iOS is an equivalent for this presented here If isn't a "last used date" than is a new install, so must show the introduction whatever. Othervise, just launch the next screen. Easy enough? :)

Community
  • 1
  • 1
matheszabi
  • 594
  • 5
  • 16
1

something like this:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // 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
        // set your rootViewController
        // window.rootViewController = xyz
    } else {
        // app has not been opened today / not been opened at all
        // set your rootViewController
        // window.rootViewController = zyx
    }

    // save the current date for the next check
    userDefaults.set(Date(), forKey: "lastOpened")
    userDefaults.synchronize()

    window?.makeKeyAndVisible()

    return true
}
André Slotta
  • 13,774
  • 2
  • 22
  • 34
  • Thanks, this seems to work (at least when printing out different strings). I am setting my rootViewController like this: `let myViewController = StartViewController() self.window!.rootViewController = myViewController` But the app just shows a black screen. Do you see what's wrong? – Adrian Apr 16 '17 at 14:54
  • you have to initalize the window and "make it key and visible". i updated my answer! try again... – André Slotta Apr 16 '17 at 14:56
  • Now the app crashes: `Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Application windows are expected to have a root view controller at the end of application launch'` – Adrian Apr 16 '17 at 15:25
  • Now I'm getting a black screen again.. I added my code to my initial post, maybe it helps! – Adrian Apr 16 '17 at 16:02
  • what do you expect to see? how do you set up your viewcontroller instances? – André Slotta Apr 17 '17 at 09:00
  • I expect to see my ViewController() or StartViewController() class (depending on whether the app has or has not been opened before). See the added code in my initial question. – Adrian Apr 17 '17 at 14:06