Failure to detect the start of the first application, how can I do this?

I have an application and a pass screen, and I want to show a pass screen if the user first opens the application, but I am doing it wrong. Should I put the code in AppDelegate or in ViewDidLoad inside my first screen. Here is the code I used:

  super.viewDidLoad()

    if UserDefaults.standard.bool(forKey: "isFirstLaunch") {

        UserDefaults.standard.set(true, forKey: "isFirstLaunch")
        UserDefaults.standard.synchronize()
    }
    let isFirstLaunch = UserDefaults.standard.value(forKey: "isFirstLaunch") as? Bool
    if isFirstLaunch! {

            let mainStoryboard = UIStoryboard(name: "ViewController", bundle: Bundle.main)
            let vc : ViewController = mainStoryboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
            self.present(vc, animated: true, completion: nil)


    }

and error image:

enter image description here

How and how to do it?

+4
source share
4 answers

The recommended method (Apple) is to register the default value (as follows from the User Defaults class ).

applicationWillFinishLaunchingInsert as soon as possible - for example, in -

let defaultValues = ["isFirstLaunch" : true]
UserDefaults.standard.register(defaults: defaultValues)

- init

override init()
{
    let defaultValues = ["isFirstLaunch" : true]
    UserDefaults.standard.register(defaults: defaultValues)
    super.init()
}

func viewDidLoad()
    super.viewDidLoad()
    let defaults = UserDefaults.standard
    if defaults.bool(forKey: "isFirstLaunch") {
        defaults.set(false, forKey: "isFirstLaunch")
        let mainStoryboard = UIStoryboard(name: "ViewController", bundle: Bundle.main)
        let vc : ViewController = mainStoryboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
        self.present(vc, animated: true, completion: nil)
    }
}

, .

+1

:

  • true, .
  • synchronize
  • value(forKey:) bool(forKey:)
  • ( ), , , , nil

, , - :

let defaults = UserDefaults()
if !defaults.bool(forKey:"walkthroughShown") {
   defaults.set(true, forKey:"walkthroughShown")
   // Display your Walkthrough
}

viewDidLoad ,

+2

. , , .

if let isFirstLaunch = UserDefaults.standard.bool(forKey: "isFirstLaunch") {
    //execute code for when it isn't the first launch
} else {
    //set value of isFirstLaunch for the first time
}

isFirstLaunch , , .

0
source

First you need to expand the value:

if let isFirstStart = UserDefaults.standard.value(forKey: "isFirstLaunch") as? Bool {
   // the isFirstStart value exists, you can check the value
} else {
   // the isFirstStart value does not exist
}

I think that isFirstStart is a bad wording convention because it cannot be true for the first time (because it has not been set before). You should use, for example, hasStarted, which indicates if the value does not exist, it has not been started before, if it exists and true, it was.

Hope this helps, feel free to ask.

0
source

Source: https://habr.com/ru/post/1659222/


All Articles