我在XCode 8工作。我的故事板里有三个视图控制器。前两种是介绍屏幕,用于解释应用程序的功能等。
有没有办法检查用户是否已经在某个时间段(例如“今天”)打开了应用程序,如果有,跳过前两个屏幕?
(用户不需要使用帐户或其他东西登录。)
我的应用程序方法如下:

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
}

最佳答案

我在安卓系统里做同样的事情:
在应用程序启动时,我将检查SharedPreferences是否有任何设置。在iOS上,如果不是一个“最后使用日期”而不是一个新的安装,那么它就相当于一个presented here的版本,因此必须显示介绍。其他虎钳,只需启动下一个屏幕。
够容易吗?:)

07-24 09:45
查看更多