iOS:在应用程序已被使用之前跳过ViewControllers
我在XCode 8中工作。我的主要故事板中有三个ViewController。前两种是介绍屏幕,可以解释应用程序的功能等等。iOS:在应用程序已被使用之前跳过ViewControllers
是否有方法检查用户是否已经在某个时间范围内打开了应用程序(例如“今天”),如果是,跳过前两个屏幕?
(用户无需与帐户或东西登录。)
我的应用程序的方法是这样的:
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
}
我在Android做同样的事情: 在启动应用程序我将检查SharedPreferences中是否有任何设置。在iOS上是相当于这个presented here如果不是一个“最后使用日期”比新安装,所以必须显示引言。别的,只需启动下一个屏幕即可。 够简单吗? :)
是这样的:
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
}
谢谢,这似乎工作(至少打印出不同的字符串时)。我设置我的rootViewController是这样的: 'let myViewController = StartViewController() self.window!.rootViewController = myViewController'但是该应用只显示一个黑屏。你看到有什么问题吗? – Adrian
你必须让窗户变得新鲜,并“让它变得重要和可见”。我更新了我的答案!再试一次... –
现在的应用程序崩溃:终止应用程序,由于未捕获异常'NSInternalInconsistencyException',原因:'应用程序窗口预计将有一个根视图控制器在应用程序启动结束' – Adrian
你必须拯救'Date'成'NSUserDefaults'并检查是否'今天Date'等于在''AppDelegate'和didFinishLaunchWithOptions'改变'window.rootViewController'的你喜欢的方式。你最好自己编码,这会很有用。 – JuicyFruit