Hello everyone,
I have this code that i run on app opening
if NewUser.shared.isNewUser() {
UserDefaults.standard.set(1, forKey: "limitedCampaign")
if let vc = storyboard?.instantiateViewController(identifier: "onboarding") as? OnboardingViewController {
vc.modalPresentationStyle = .fullScreen
present(vc, animated: true)
}
} else {
limitedViewFirstRun()
}
If the user is new i set in user defaults value 1 to limitedCampaing, if is not new i just run limitedViewFirstRun(). And here is the func:
func limitedViewFirstRun() {
if UserDefaults.standard.integer(forKey: "limitedCampaign") != 0 && (limited[0]["a"]! as? String) != "" && (limited[0]["d"]! as? String) != "" {
if (UserDefaults.standard.integer(forKey: "limitedCampaign") < (limited[0]["c"] as! Int)) {
if let vc = storyboard?.instantiateViewController(identifier: "limitedDownload") as? LimitedDownloadViewController {
vc.modalPresentationStyle = .overCurrentContext
vc.imagePreview = limited[0]["a"]! as? String
vc.downloadURL = limited[0]["d"]! as? String
vc.campaign = limited[0]["c"]! as? Int
present(vc, animated: true)
UserDefaults.standard.set(limited[0]["c"] as! Int, forKey: "limitedCampaign")
}
}
} else {
if UserDefaults.standard.integer(forKey: "limitedCampaign") == 0 {
print("Setting limitedCampaign to UserDefaults!")
UserDefaults.standard.set(1, forKey: "limitedCampaign")
} else {
print("limitedCampaing already set to \(UserDefaults.standard.integer(forKey: "limitedCampaign")) in UserDefaults!")
}
}
}
Here i check if limitedCampaign exists by verifying if != 0. And i check if the values in the array are not "". I set those values into firebase, and when i don't want the modal to be shown i just put an empty string as values.
Else i check if the limitedCampaing is set and set it to 1 if not.
In the firebase the start value of limited[0]["c"] as! Int is 2, an then i just +1 on every new campaing, so the code runs only if the id number greater then the local limitedCampaing.
Am i doing something wrong with the Userdefaults??
I know that ther problem is here because it's the only new code of the new release. And the crashes begun here.
Thank you for your help!