我希望我的警报只在第一次在新设备上启动应用程序时打开,但每次应用程序在多任务中关闭并重新打开时都会打开。如果有人能够帮忙,我已附上我的代码?每次都会打开警报,而不仅仅是第一次
override func viewDidAppear(_ animated: Bool) {
// alert first time app is opened
// making of alert
let alert = UIAlertController(title: "Navigation", message: "Tap Right Hand Side of Screen For Next Quote, Left Hand Side To Go Back", preferredStyle: UIAlertControllerStyle.alert)
//add ok button
alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default, handler: nil))
// detect if first launch
let launchedBefore = UserDefaults.standard.bool(forKey: "launcedBefore")
if launchedBefore {
}
else {
self.present(alert, animated: true, completion: nil)
UserDefaults.standard.set(true, forKey: "launchedBefore")
}
}
viewDidAppear不是正确的地方。 –
@ElTomato它绝对是正确的地方。 – Sulthan
这应该教会你使用字符串的常量。 ''launchBefore“'vs'”launcedBefore“'。 – Sulthan