2017-04-16 34 views
0

我在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 
} 
+0

你必須拯救'Date'成'NSUserDefaults'並檢查是否'今天Date'等於在''AppDelegate'和didFinishLaunchWithOptions'改變'window.rootViewController'的你喜歡的方式。你最好自己編碼,這會很有用。 – JuicyFruit

回答

1

我在Android做同樣的事情: 在啓動應用程序我將檢查SharedPreferences中是否有任何設置。在iOS上是相當於這個presented here如果不是一個「最後使用日期」比新安裝,所以必須顯示引言。別的,只需啓動下一個屏幕即可。 夠簡單嗎? :)

1

是這樣的:

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 
} 
+0

謝謝,這似乎工作(至少打印出不同的字符串時)。我設置我的rootViewController是這樣的: 'let myViewController = StartViewController() self.window!.rootViewController = myViewController'但是該應用只顯示一個黑屏。你看到有什麼問題嗎? – Adrian

+0

你必須讓窗戶變得新鮮,並「讓它變得重要和可見」。我更新了我的答案!再試一次... –

+0

現在的應用程序崩潰:終止應用程序,由於未捕獲異常'NSInternalInconsistencyException',原因:'應用程序窗口預計將有一個根視圖控制器在應用程序啓動結束' – Adrian

相關問題