LoginSignup
1
3

More than 3 years have passed since last update.

初回起動時のみ画面遷移する方法

Posted at

備忘録。ログインしていない状態の時には登録画面に遷移するなどにも応用できます。

application(_:didFinishLaunchingWithOptions:)にUserDefaultsを設定する

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        let userDefaults = UserDefaults.standard
        let firstLunchKey = "firstLunchKey"
        let firstLunch = [firstLunchKey: true]
        userDefaults.register(defaults: firstLunch)
        return true
    }

起動画面のViewDidAppeearにUserDefaultsを設定する

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        let userDefaults = UserDefaults.standard
        let firstLunchKey = "firstLunchKey"
        if userDefaults.bool(forKey: firstLunchKey) {
        performSegue(withIdentifier: "遷移させたい画面へのsegue.identifier", sender: self)
    }
}
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3