有没有一种方法可以向 UIAlertController 的 NotNow 按钮添加操作,并在用户选择该选项后 24 小时后触发它?
Is there a way I can add actions to NotNow button of UIAlertController and get it triggered after 24 hours if the user chooses that option?
if !force {
let notNowButton = UIAlertAction(title: "Not Now", style: .cancel) { action in
// Not getting what can be written here to make it trigger after 24 hours with the Logic
}
alertController.addAction(notNowButton)
}
我在 class 中编写了这个函数,我在 AppDelegate class didFinishLaunchingWithOptions launchOptions 函数中调用它。我面临的问题是每次启动应用程序时都会重新出现警报希望它至少在选择此选项时 24 小时内不会出现。
在您的 ViewController 中执行以下操作,请参阅评论中的解释。
class ViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
displayAlert()
}
func displayAlert() {
// get previously stored date from some persistent store (UserDefaults is appropriate)
let alertDate = UserDefaults.standard.object(forKey: "alertDate")
// was a date set previously?
if let alertDate = alertDate as? Date,
// add the desired time interval to get next possible alert date
let nextPossibleAlertDate = Calendar.current.date(byAdding: .day, value: 1, to: alertDate),
// check if we're past that date
nextPossibleAlertDate > Date() {
print("too soon to bother again")
return
}
// let's present the alert
let alertController = UIAlertController(title: "Do the annoying thing now?", message:
nil, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Not now!", style: .cancel, handler: { action in
// store the date when this happens
let date = Date()
print(date)
UserDefaults.standard.set(date, forKey: "alertDate" )
}))
self.present(alertController, animated: true, completion: nil)
}
}
if !force {
let notNowButton = UIAlertAction(title: "Not Now", style: .cancel) { action in
// Not getting what can be written here to make it trigger after 24 hours with the Logic
}
alertController.addAction(notNowButton)
}
我在 class 中编写了这个函数,我在 AppDelegate class didFinishLaunchingWithOptions launchOptions 函数中调用它。我面临的问题是每次启动应用程序时都会重新出现警报希望它至少在选择此选项时 24 小时内不会出现。
在您的 ViewController 中执行以下操作,请参阅评论中的解释。
class ViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
displayAlert()
}
func displayAlert() {
// get previously stored date from some persistent store (UserDefaults is appropriate)
let alertDate = UserDefaults.standard.object(forKey: "alertDate")
// was a date set previously?
if let alertDate = alertDate as? Date,
// add the desired time interval to get next possible alert date
let nextPossibleAlertDate = Calendar.current.date(byAdding: .day, value: 1, to: alertDate),
// check if we're past that date
nextPossibleAlertDate > Date() {
print("too soon to bother again")
return
}
// let's present the alert
let alertController = UIAlertController(title: "Do the annoying thing now?", message:
nil, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Not now!", style: .cancel, handler: { action in
// store the date when this happens
let date = Date()
print(date)
UserDefaults.standard.set(date, forKey: "alertDate" )
}))
self.present(alertController, animated: true, completion: nil)
}
}