I Need to create a splash screen for my app but I can’t detect the first app launch if you know how let me know thanks
Replies
What do you mean first app launch ?
Is it the first time ever the user launches your app ?
Then you could keep this information in UserDefaults, with a property firstLaunch set to true
As soon as you launch, set the property to false.
But the splash screen usually shows each time you launch the app…
I Mean the first time the user launched the app and I have tried to use user defaults but it’s not working I’m probably doing it wrong is there a certain way to do it?
> I have tried to use user defaults but it’s not working
Describe 'not working' - what you did, what you expected, how those differed. Also how you're testing that event.
You should define in Root.plist, a key with firstLaunchKey identifier, set it to false
Then, in application(didFinishLaunching):
let firstLaunchKey = "firstLaunchKey"
let defaults = [firstLaunchKey: true] // To have some value
// connects to Root.pList see: h ttps://makeapppie.com/2016/03/14/using-settings-bundles-with-swift/
UserDefaults.standard.register(defaults: defaults)
UserDefaults.standard.set(false, forKey: firstLaunchKey) // Now, set to false
Try this simple code:
if(![[NSUserDefaults standardUserDefaults] objectForKey:@"FirstLaunch"]) {
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"FirstLaunch"];
// the value doesn't matter - you are just testing to see if there is an object with the key "FirstLaunch"
// execute code for first launch
}else{
// execute code for not first launch
}
If your concern is for the user who deletes the app and then reinstalls it then you will need to use the keychain or the user's iCloud key-value file and both of those are a bit more complex.