Unsuccessful detection of first app launching , how should I do it?

Ivan

I have an app and a walkthrough screen and I want to display the walkthrough screen if the user's open the app for first time, but I do it wrong. Should I put the code in the AppDelegate or in the 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 a picture of the error:

enter image description here

Any ides how to do it?

vadian

The (Apple) recommended way is to register the default value (as the class UserDefaults implies).

As soon as possible – for example in applicationWillFinishLaunching – insert

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

The earliest moment is the init method in application delegate

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

Then simply write

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)
    }
}

The default value is considered until it's changed the first time.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I remove an unsuccessful installion of Ubuntu

How to track if an app is launching for the first time in Android?

Should I change app IDs before re-launching

How do I start background threads when launching a Flask app through gunicorn?

How do I diagnose "There was an error launching the application"?

Xcode UI Testing: how to test app's first launching

How to reset dmenu app launching preferences in i3

How do I set an xib as the first page in my app in xamarin?

How do I save images with face detection bounding box using the Batch Image Processor app in Matlab

Regex: If the first letter is an X, it should end with either A or B. How do I do this?

I want To reuse the Style block of First Button for others button too, How should I do?

How do I assign WM_CLASS when launching an application

How do I avoid imperva bot detection?

Blank screen first time launching app xcode

Calling didReceiveRemoteNotification when app is launching for the first time

How to restrict "0" should not be placed at the first character in the text box with jquery how can i do

How do I fix multiple Google Talk Plugin "unsuccessful application installation" errors daily?

My code prints "Not Found" for every line, how do i get it to only print "Not Found" once if the search is unsuccessful?

I need to validate first four digits in a phone number which should be "5678" in Node Js. How should i do it? Which Validator library should i use

How to turn off the layer display of addLayersControl first when launching the shiny app

how do i set collision detection to where i want it?

How do I tell flutter that just inputting SPACE should be considered null in my flutter texting app?

is there any other way than expo to run my React app? How should i do it?

How should I split app into angular modules?

How should I setup this "drawer" view app?

How can I change this project to a OpenCV realtime face detection app?

suppose I select the first select option it should show me the list of the second select option. How can I do it in angular?

Node JS - Should i do "return cb(..)" or first cb(..) and then return?

Should I perform Cross Validation first and then do grid search?