I want to show an alert textfield only when the app is first installed and the first time it runs.
Where should I write any code?
I want to show an alert textfield only when the app is first installed and the first time it runs.
Where should I write any code?
Store the information (may be a Bool Flag) in NSUserDefaults
that whether Alert is shown or not. If not shown then show and set the value accordingly in NSUserDefaults
let isInfoShown = UserDefaults.standard.string(forKey: "Info")
if (isInfoShown == nil || isInfoShown == "")
{
UserDefaults.standard.setValue("ShownInfo", forKey: "Info")
// Show Alert here
}
When handled With Bool
in UserDefaults
let alertShown = UserDefaults.standard.bool(forKey: "ShownAlert")
if !alertShown {
print("1st time launch, showing info Alert.")
UserDefaults.standard.set(true, forKey: "ShownAlert")
}
Note - UserDefaults.standard.bool(forKey: "ShownAlert")
, won't return nil
, but false
if the value doesn't exist.
SideNote - If App is removed/deleted and re-installed then Alert will show again. If App is updated then Alert will not be shown. This is because UserDefaults
is lost when App is deleted.